LoginSignup
23
14

More than 5 years have passed since last update.

Ubuntu16.04にaptでMySQL5.7を入れる時にrootを空パスワードにするとどうなるか

Last updated at Posted at 2017-02-08

確認した日は 2017/02/08 , MySQLのバージョンは MySQL 5.7.17, ubuntuはVirtualBox上のvm。

ubuntu16.04で以下のコマンドを打ってaptでMySQL5.7をインストールすると、途中でrootユーザーのパスワードを聞かれる。ただのお試しで面倒なので空のままエンターを押し、インストールを完了した。

$ sudo apt install mysql-server

よしよし mysql -uroot でログインできるだろう、と思ったが以下のようなエラーがでる。ちなみにlinuxにはubuntuという名前のユーザーでログインしている。(ubuntuにubuntuというユーザー名でログインしてて分かりにくいけど勘弁してください)

ERROR 1045 (28000): Access denied for user 'ubuntu'@'localhost' (using password: NO)

なにやら、インストールでrootのパスワードを空にすると、rootの認証にはauth_socket pluginを使うように設定されるらしい。

it is configured with the auth_socket plugin

https://www.percona.com/blog/2016/05/18/where-is-the-mysql-5-7-root-password/

Access denied のエラーメッセージからも推測できるように、auth_socket pluginは、MySQLクライアントを実行したlinuxユーザーを、MySQLのユーザーとして認証しようとするらしい。

The auth_socket authentication plugin authenticates clients that connect from the local host through the Unix socket file. The plugin uses the SO_PEERCRED socket option to obtain information about the user running the client program. Thus, the plugin can be built only on systems that support the SO_PEERCRED option, such as Linux.

https://dev.mysql.com/doc/refman/5.7/en/socket-authentication-plugin.html

ということで、アクセスしたい場合は、sudoでlinuxのrootとしてMySQLにアクセスすれば良さそう。(想像通り、-urootを指定する必要はない)

$ sudo mysql

この状態で、rootにMySQLのパスワードを設定するには、通常のパスワード変更操作に加え、認証用のpluginを変更する必要がある。試しに空パスワードを指定してみた。

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

結果、以下のコマンドでアクセスできるようになる。

$ mysql -uroot

空パスワードを受け入れるかどうかはMySQL Serverの設定次第だと思うけど、このインストール方法でのデフォルトだと受け入れるみたい。validate_password pluginがインストールされてないから、きっとそういうものなんだろう。

# https://dev.mysql.com/doc/refman/5.7/en/validate-password-plugin-installation.html

mysql> SELECT PLUGIN_NAME, PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS  WHERE PLUGIN_NAME LIKE 'validate%';
Empty set (0.00 sec)
23
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
14