このエラーの対処法について紹介したページは(丸同じの内容が)いくつもあるのですが、
どれも~MySQL5.x用の内容で、使えませんでした。
MySQL8.x〜の対処法。
背景
ローカルでMySQLを使おうと思ったら、すでにインストールしてあった。
インストールした覚えがないのでパスワードも覚えてなく、rootでログインができない。
$ mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
ネットに載っている対処法を試してもエラーが出ちゃう。
https://qiita.com/dev-masaki/items/76946c76923f99b85f8e
mysql> grant all privileges on *.* to root@localhost identified by 'password' with grant option;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by 'root' with grant option' at line 1
解決策
ネットで出回っている対処法はMySQL5.xのものらしいので、
MySQL8.xの場合は以下の手順で対処。
(今考えたら、すでにrootユーザーが存在する場合には
もしかしたらパスワードの変更だけでも行けたのかもしれない。笑)
1. (既にMySQLが稼働している場合には停止)
$ mysql.server stop
Shutting down MySQL
.. SUCCESS!
2. 権限なしに立ち上げられるように設定
$ mysqld_safe --skip-grant-tables &
[1] 24997
$ 2018-07-30T05:37:18.6NZ mysqld_safe Logging to '/usr/local/var/mysql/mypc.local.err'.
2018-07-30T05:37:18.6NZ mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
mysqld_safe
を実行したら、
勝手にLogging to***
も実行されて、Starting mysqld daemon with databases from ***
でEnterしたら、できた
3. ログインする
$ mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.11 Homebrew
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
4. rootユーザーを一旦削除
mysql> DROP USER root@localhost;
ネットに出回っている対策にしたがいuserテーブルごと削除するだけだとこの後のユーザー作成のステップでエラーになるため、
rootのユーザー情報はちゃんと一旦削除する。
// mysql> truncate table user;
Query OK, 0 rows affected (0.04 sec)
mysql> CREATE USER 'root'@'localhost' IDENTIFIED BY 'password';
ERROR 1396 (HY000): Operation CREATE USER failed for 'root'@'localhost'
truncateはやらなくていいかも!!!!!!!
これをやったせいで必要なユーザーが消えたようで、今度はSHOW TABLEでエラーが出るようになった笑
ERROR 1449 (HY000): The user specified as a definer (‘mysql.infoschema’@’localhost’) does not exist
5. キャッシュを削除
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
(やらなくても平気かも)
6. rootユーザーを作成
mysql> CREATE USER 'root'@'localhost' IDENTIFIED BY '{password}';
7. rootユーザーにすべての権限を付与
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost';
8. ちゃんとできたか確認
$ mysql -u root -p
Enter password:
9. サーバーを再起動
$ mysql.server start
Starting MySQL
SUCCESS!
以下が出て来てしまう時は、既存のプロセスをkillする
(もしかしたらしなくても再起動できてたのかもしれないけど)
$ 2018-07-30T06:25:59.6NZ mysqld_safe A mysqld process already exists
まず、既存のプロセスを探して
$ ps aux| grep mysqld
kuro 25088 0.4 0.6 4881628 52492 s002 S 2:37PM 0:19.70 /usr/local/Cellar/mysql/8.0.11/bin/mysqld --basedir=/usr/local/Cellar/mysql/8.0.11 --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/Cellar/mysql/8.0.11/lib/plugin --skip-grant-tables --log-error=kurosawanoMacBook-puro.local.err --pid-file=kurosawanoMacBook-puro.local.pid
kuro 27813 0.0 0.0 4285160 912 s002 S+ 3:27PM 0:00.01 grep mysqld
kuro 24997 0.0 0.0 4279600 980 s002 S 2:37PM 0:00.03 /bin/sh /usr/local/bin/mysqld_safe --skip-grant-tables
この場合は、「25088」と「24997」をKillする
kurosawanoMacBook-puro:puutal kuro$ kill -9 24997
[1]+ Killed: 9 mysqld_safe --skip-grant-tables
kurosawanoMacBook-puro:puutal kuro$ kill -9 25088
kurosawanoMacBook-puro:puutal kuro$ mysql.server start
Starting MySQL
SUCCESS!
以上でOKでした。
(MySQL詳しくないから、余分な手順とか入ってるかも…。)
→以上の手順を実施したものの、最終的には結局再インストールしました。
参考
http://q.hatena.ne.jp/1526617474
http://d.hatena.ne.jp/kobapan/20090910/1252552185
http://matomater.com/17913/
コメント