WSLにMySQLをインストール - dev-kiyoharu/test GitHub Wiki

WSLのUbuntu18.04.1LTSにMySQL5.7を入れたときに正常に動作しなかったので、
MySQLインストール後に次のサイトを参考にして対処した。
 参考サイト https://qiita.com/nyu___nS/items/1ee02999d2032d81b498

MySQLの最初のインストール

$ sudo apt install mysql-server
  ・
  ・
  ・
  ◆インストール中にエラーが出ていた!!
/proc/net/unix が開けません: そのようなファイルやディレクトリはありません
ファイルをstatできません /proc/1/fd/5: 許可されていない操作です
ファイルをstatできません /proc/1/fd/10: 許可されていない操作です
ファイルをstatできません /proc/1/fd/6: 許可されていない操作です
ファイルをstatできません /proc/8258/fd/7: 許可されていない操作です
ファイルをstatできません /proc/8258/fd/10: 許可されていない操作です
ファイルをstatできません /proc/8258/fd/5: 許可されていない操作です
ファイルをstatできません /proc/8345/fd/7: 許可されていない操作です
ファイルをstatできません /proc/8345/fd/10: 許可されていない操作です
ファイルをstatできません /proc/8345/fd/5: 許可されていない操作です
ファイルをstatできません /proc/8935/fd/47: 許可されていない操作です
ファイルをstatできません /proc/8935/fd/51: 許可されていない操作です
  ・

mysqldの起動(既に起動していたかもしれないが)

$ sudo service mysql start
 * Starting MySQL database server mysqld                                                                                No directory, logging in with HOME=/
mkdir: ディレクトリ `//.cache' を作成できません: 許可がありません
-su: 19: /etc/profile.d/wsl-integration.sh: cannot create //.cache/wslu/integration: Directory nonexistent
                                                                                                                 [ OK ]

起動時にエラーが出るが、起動されている・・・?
ネットで調べたら、mysqlユーザのホームディレクトリがないので出ているらしい!
(なぜこうなったかは?  インストール時にエラーが出ていたからか・・・)

以下に解決方法があったので試す。
https://askubuntu.com/questions/737903/mysql-5-7-no-directory-logging-in-with-home

# mysqldの停止
$ sudo service mysql stop
$ 
# mysqlユーザのホームディレクトリを /var/lib/mysql/ にする
$ sudo usermod -d /var/lib/mysql/ mysql
$ 
# mysqldの起動
$ sudo service mysql start

エラーが出なくなった!

mysqlコマンドでDBに接続できない

次のコマンドでDBに接続しようとするが、接続できない!!

$ mysql -u root -p
Enter password:   ◆パスワードがわからないので何も入れずにEnter
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

MySQLインストール時に、初期rootパスが生成されるらしい
/var/log/mysqld.logに書いてあるらしいけど、存在しない。

インストール時に mysqld --initialize が実行されるようだが、うまく行ってないらしい・・・
ので以下を行う。

インストール時(最初のmysqld起動時かも)に作成された MySQL が使用するディレクトリを削除

# MySQLの停止(既に停止しているかもしれませんが)
$ sudo service mysql stop
# MySQLが初期化されたときに作成したディレクトリ以下を削除
$ sudo rm -rf /var/lib/mysql/

初期化をやり直す

$ sudo mysqld --initialize

後で必要になるので、初期ルートパスを取得

$ grep 'temporary password' /var/log/mysql/error.log

初期設定を行う

$ sudo mysql_secure_installation
Securing the MySQL server deployment.

Enter password for user root:   ◆ルートのパスワードを聞かれるので↑で見つけた初期ルートパスを入力

The existing password for the user account root has expired. Please set a new password.

New password:   ◆新しいrootのパスワードを入力

Re-enter new 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   ◆n を入力してプラグインは入れないで置く
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y   ◆

New password:     ◆もう一度新しいrootのパスワードを入力

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   ◆リモートからの root ユーザでのログイン禁止
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   ◆test データベースの削除
 - 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コマンドでDBに接続

次のコマンドでDBに接続できた!!

$ mysql -u root -p
Enter password:   ◆初期設定で設定したパスワード
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.7.30-0ubuntu0.18.04.1 (Ubuntu)

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

DBに接続できるユーザの追加

mysql>  select host,user from mysql.user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| localhost | debian-sys-maint |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
4 rows in set (0.01 sec)

mysql> # 接続できるユーザの追加(ローカルなrootの接続設定)
mysql> grant all privileges on *.* to 'root'@'127.0.0.1' identified by 'ないしょ';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> # 接続できるユーザの追加(ooshimaがどこからでも接続できる設定)
mysql> grant all privileges on *.* to 'ooshima' identified by 'ないしょ';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select host,user from mysql.user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | ooshima       |
| 127.0.0.1 | root          |
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
5 rows in set (0.00 sec)

mysql> quit
Bye

登録したユーザでDB接続してみる

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

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