MySQL 8.0以降は、ユーザ認証方式を変更しましょう Laradock on Ubuntu @大人プログラミング部

(こんばんは。前回の投稿から間が空いてしまったので、要点だけ書きますね。)

MySQL 8.0以降は、ユーザ認証方式を mysql_native_password に変更しましょう 

理由:mysql 8.0以降はユーザ認証の方式がcaching_sha2_password に変わったが、現行のphp 7.3.9 ではその認証方式がサポートされていないため。

まずは、workspaceコンテナに接続して、PHPバージョン確認します。

$ docker-compose exec workspace bash
 root@dee160787a84:/var/www# php -v
 PHP 7.3.9-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Sep  2 2019 12:54:04) ( NTS )
 Copyright (c) 1997-2018 The PHP Group
 Zend Engine v3.3.9, Copyright (c) 1998-2018 Zend Technologies
     with Zend OPcache v7.3.9-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
 root@dee160787a84:/var/www# exit
 exit

MySQLコンテナに接続し、rootでログインします。

$ docker-compose exec mysql bash
root@403e510891bb:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.17 MySQL Community Server - GPL

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 DATABASE(設定用DB)に入り、ユーザの認証方法を変更します。

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 |
+------------------+-----------+-----------------------+
| default | % | caching_sha2_password |
| root | % | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
| root | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
6 rows in set (0.00 sec)

mysql> alter user 'default'@'%' identified with mysql_native_password by 'secret';
Query OK, 0 rows affected (0.11 sec)

mysql> select user, host, plugin from user;
+------------------+-----------+-----------------------+
| user | host | plugin |
+------------------+-----------+-----------------------+
| default | % | mysql_native_password |
| root | % | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
| root | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
6 rows in set (0.00 sec)

mysql> exit
Bye

mysql に default user でログイン(パスワードは先程設定した “secret”)して、default DATABASEを確認できたらOKです。

root@403e510891bb:/# mysql -u default -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.17 MySQL Community Server - GPL

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> show databases;
+--------------------+
| Database |
+--------------------+
| default |
| information_schema |
+--------------------+
2 rows in set (0.00 sec)

mysql> use default
Database changed
mysql> show tables;
Empty set (0.01 sec)

mysql> exit
Bye
root@403e510891bb:/# exit
exit

ちなみに、認証方式が caching_sha2_password のままだとシンタックスエラーが出ます。

mysql> show tables in default;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'default' at line 1

ブラウザで http://localhost:8080 から phpMyAdminを表示させ、mysql, default, secretでログインして、mysqlの内容を表示させることができれば成功です。