LoginSignup
68
57

More than 3 years have passed since last update.

WSLのUbuntu 18.04でMySQL 5.7をセットアップ

Posted at

WSL(Windows Subsystem for Linux)でMySQLを使おうとした際に、セットアップ(パスワードまわり)で手間取ったのでその備忘になります。

  • WSLのUbuntu 18.04 LTS
  • MySQL 5.7

MySQL 5.7 のインストール

Ubuntu 18.04にaptコマンドでMySQLをインストールしようとすると、現時点(2019/07/23)ではバージョン5.7になります。
なので、普通にaptコマンドを使用してインストールします。

$ sudo apt install mysql-server mysql-client

インストールしたらサービスを起動してみます。

$ sudo service mysql start
No directory, logging in with HOME=/
 * Starting MySQL database server mysqld    [OK]

No directory, logging in with HOME=/と出ると思います。
これは起動に使用するmysqlユーザーのホームディレクトリが存在しない為に起きます。

$ cat /etc/passwd
mysql:x:111:115:MySQL Server,,,:/nonexistent:/bin/false

確認すると、このようにホームディレクトリが/nonexistentとなっているのが分かるかと思います。
なので、設定してあげます。

$ sudo service mysql stop

一旦、先ほど起動したMySQLのサービスを停止します。
※[fail] となって起動に失敗するケースもあるようなので、その際には不要です。

$ sudo usermod -d /var/lib/mysql mysql

MySQLのインストールで/var/lib/mysqlが作成されているので、そこを指定してあげます。

$ cat /etc/passwd
mysql:x:111:115:MySQL Server,,,:/var/lib/mysql:/bin/false

この通り設定できたので、再度起動してみます。

$ sudo service mysql start
 * Starting MySQL database server mysqld    [OK]

この通り、正常に起動できました。

root のパスワードを設定する

次にrootユーザーのパスワードの設定です。
※ここが自分が手間取った個所になります。

まず、パスワードの初期設定をする前に認証の方式を変更しておきます。
なんでも、Ubuntu特有の初期設定で初期ではrootユーザーの認証方式がauth_socketとなっており、この場合はパスワードではなく、OSのrootユーザーの情報で認証するのだとか。(つまりrootユーザーしかrootとしてログインできない。
rootユーザー以外からログインしようとすると、下記のようにエラーになります。(パスワードが合っていてもです。

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

自分としてはそれは困るので、パスワード認証方式に変更しました。
パスワード認証に変更するには、下記を実行します。

$ sudo mysql
mysql> USE mysql;
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit;

これで前準備は完了です。
あとはパスワード含めた初期設定を実行します。

$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: n  ←ローカル環境ではパスワードの強化は不要なので無効にしています。
Please set the password for root here.

New password:

Re-enter new password:
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

これで完了です。
あとは各ユーザーから下記コマンドで接続できるはずです。

$ mysql -u root -p
68
57
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
68
57