Help us understand the problem. What is going on with this article?

WSL2のUbuntu18.04にMySQL5.7をインストールする

Ubuntu18.04にMySQLをインストールした際の備忘録的な奴です。
環境はWSL2なんだけど、たぶんあんまり関係ないと思う
WSLはsystemctlでの自動起動設定が効かなかった。。。

インストール

普通にパッケージマネージャからインストールする。

$ sudo apt install mysql-server -y

MySQLサーバ起動

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

なんかMySQLの実行ユーザーのhomeディレクトリがないっぽい?

とりあえず放置して、後で調べる。

セキュリティ設定

MySQLユーザーのパスワードのセキュリティレベル設定とかrootパスワードの再設定とかをやる。
(MySQL5.7はインストール時にrootパスワードを勝手に設定しちゃうっぽい)
※ 設定されたrootパスワードは/var/log/mysql/error.logで確認できる。

$ 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: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
Please set the password for root here.

New password: 

Re-enter new password: 

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
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
Enter password: 
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

あれ?アクセスできない...

調べてみたら、Linuxのログインユーザー(名)とMySQLのログインユーザー(名)が一緒じゃないとダメっぽい。(ちゃんと調べてないけど、たぶん)

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

Copyright (c) 2000, 2019, 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.

mysql> 

もちろん、これでもログインできる。

$ sudo mysql -p

ユーザーの作成

ローカルの開発環境として立ち上げたRailsとかからアクセスするのに必要なので、Linuxのログインユーザーと同名のユーザーを作っておく。

mysql> CREATE USER 'yourname'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourpasswd';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'yourname'@'localhost';
Query OK, 0 rows affected (0.01 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

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 FROM user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| debian-sys-maint | localhost |
| yourname             | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+

5 rows in set (0.00 sec)

mysql> SHOW GRANTS for 'yourname'@'localhost';
+---------------------------------------------------+
| Grants for yourname@localhost                         |
+---------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'yourname'@'localhost' |
+---------------------------------------------------+
1 row in set (0.01 sec)

作成したユーザー(Linuxのログインユーザー)でログインする

$ mysql -u yourname -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.27-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2019, 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.

mysql>

MySQLの実行ユーザーのhomeディレクトリを作成する

最初のMySQL起動時にhomeディレクトリが無い的なエラーが表示されたので、それを解消する。

mysqlの実行ユーザーを調べてみる

$ less /etc/mysql/mysql.conf.d/mysqld.cnf
...
[mysqld]
#
# * Basic Settings
#
user            = mysql
...

mysqlユーザーのhomeディレクトリを確認する

$ grep mysql /etc/passwd
mysql:x:111:116:MySQL Server,,,:/nonexistent:/bin/false

無いので作成する

$ sudo mkdir /home/mysql
$ sudo chown mysql /home/mysql/
$ sudo usermod -d /home/mysql/ mysql
usermod: user mysql is currently used by process 22846

ほかのプロセスにmysqlユーザーが使用されていると言われたので、どのプロセスかを確認

$ ps aux | grep 22846
mysql    22846  0.0  0.0   4628  1784 ?        S    21:55   0:00 /bin/sh /usr/bin/mysqld_safe

(あたりまえだけど)MySQLのプロセスだったので、それを停止して、再度homeディレクトリの設定を行う

$ sudo service mysql stop
 * Stopping MySQL database server mysqld                                         [ OK ]
$ sudo usermod -d /home/mysql/ mysql

ちゃんと設定できてるか確認

$ grep mysql /etc/passwd
mysql:x:111:116:MySQL Server,,,:/home/mysql/:/bin/false

MySQLを起動してみる

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

さっきのhomeディレクトリ関連のエラーメッセージは表示されなくなった。

その他

自動起動設定はされてるっぽいけど、されてなければsystemctlコマンドで確認・設定できる。
WSL上のUbuntuはsystemctlコマンドが使えない(設定が反映されない?)みたいなので、必要なサービスをservice "servicename" startとかで手動起動するか、ログインシェルの設定ファイル(僕の場合はzshrcとか)にスクリプトを書いて自動起動を実現する感じかと思われる。

方法は後で考える。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away