MySQLに「'Access denied for user 'root'@'localhost'」で接続できない場合の対処法

カテゴリ:データベース

CeontOS 7やUbuntuなどのSystemd環境でMySQLに接続時に「mysqladmin: connect to server at 'localhost' failed error: 'Access denied for user 'root'@'localhost'」のエラーとなる場合があります。

原因1:sudoがない

まず OS に root ユーザー以外でログインしている場合は、sudo を忘れていないかを確認しましょう。

sudo がない場合:

$ mysql -u root
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

sudo を付けた場合:

$ sudo mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.33-0ubuntu0.22.04.2 (Ubuntu)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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.

mysql>

注意sudo を付けてログインできた場合、後述するように認証プラグインが auth_socket になっています。パスワード認証をさせたい場合は原因2の手順を実施してください。

原因2:認証プラグインがauth_socketになっている

認証プラグインが mysql_native_password ではない可能性があります。

次のコマンドで MySQL にログインします。(パスワードなしでログインできるはずです)

$ sudo mysql

以下のコマンドを実行して認証プラグインを確認します。auth_socket になっているはずです。

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> select user, host, plugin from user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| debian-sys-maint | localhost | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | auth_socket           |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)

NoteMySQL 8.0 からはデフォルトの認証プラグインが mysql_native_password ではなく auth_socket に変更されています。auth_socket はパスワードではなく UNIX ソケットを使用して認証するため、MySQL のユーザ名と OS のユーザ名が一致していればログインが許可されます。sudo は root ユーザーによる実行を意味しますので、 sudo mysql だけで MySQL に root ユーザーでログインできたのです。

パスワードと共に認証プラグインを mysql_native_password に変更します。

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

念のため、root ユーザーの認証プラグインが mysql_native_password に変更されたことを確認します。

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> select user, host, plugin from user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| debian-sys-maint | localhost | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
6 rows in set (0.00 sec)

注意上記コマンドを実行した場合、認証方式が変わるため、sudo mysql では MySQL にログインできなくなります。変更後は mysql -u root -p と入力して実行し、パスワード入力が求められるため設定したパスワードを入力してログインします。

原因3:パスワードが違う

上記に該当しない場合はパスワードが違う可能性があるため、以下の手順でMySQLのrootユーザーのパスワードを再設定してみてください。

CentOS の場合の例:

  1. /etc/my.cnfをviで開きます。
  2. [mysqld]の下に skip-grant-tables を追加します。
    [mysqld]
    skip-grant-tables
  3. MySQLを再起動します。
    # systemctl restart mysqld
  4. MySQLに接続します。
    # mysql -u root
  5. 以下を実行します。
    mysql> flush privileges;
  6. rootユーザーの新しいパスワードを設定します。
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';

  7. quitでデータベースから切断します。
    mysql> quit
  8. 再度/etc/my.cnfをviで開き先ほど追加した skip-grant-tables を削除するかコメントにします。
  9. MySQLを再起動します。
    # systemctl restart mysqld
  10. 改めて新しいパスワードで接続できることを確認してください。
    # mysql -u root -p
以上です。

公開日時:2019年06月01日 02:54:25
最終更新日時:2023年07月29日 16:58:22

なお、VPS選びで迷ったらこちらの記事で主要VPSのメモリ容量ごとの月額、年額料金を比較していますので、是非参考にしてみてください。

データベースに戻る

このページのトップに戻る