1.問題の説明
ショートカットキーの誤用によりMySQLが終了し、再度ログインするとエラーが表示されます:エラー1045(28000):ユーザー 'root' @ 'localhost'のアクセスが拒否されました(パスワードを使用:YES)
2.問題解決プロセス
1./etc/mysql/mysql.conf.d/mysqld.cnfファイルを開きます。コマンドは次のとおりです。
$ sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
2. [mysqld]セクションを見つけて、「skip-grant-tables」という行を追加します
3. mysqlサービスを再起動し、空のパスワードを使用してmysql管理コマンドラインを入力し、mysqlライブラリに切り替えます。操作コマンドは、次のとおりです。
$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update mysql.user set authentication_string=password('newpass') where user='root' and Host ='localhost';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> update user set plugin="mysql_native_password";
Query OK, 0 rows affected (0.00 sec)
Rows matched: 3 Changed: 0 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> quit;
Bye
4. sudo vi /etc/mysql/mysql.conf.d/mysqld.cnfに戻り、追加した「skip-grant-tables」行にコメントを付けるか削除します。
5. mysql service sudo service mysql restartを再起動し、新しいパスワードでログインすると、変更は成功します。
$ mysql -u root -p new_pass
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql>