MySQL8のインストール時のエラー対応方法&備忘録
MySQL8をインストールする際、前バージョンと異なる点がいくつかあったので備忘録です。
GRANT構文でユーザを作成できない
以前は下記クエリ実行でユーザー作成できましたが、MySQL8系ではエラーになります。
mysql> grant all on *.* to 'test'@'localhost' identified by 'password';
ユーザー作成と権限付与を別々に実行しなくてはいけなくなりました。
参考:https://www7390uo.sakura.ne.jp/wordpress/archives/456
デフォルトの認証方法を変更する
Laravelでマイグレーションコマンドを実行した際、下記のエラーが表示されました。
[root@hostname test]# php artisan migrate
Illuminate\Database\QueryException : SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client (SQL: select * from information_schema.tables where table_schema = achieve_test and table_name = migratio ns and table_type = 'BASE TABLE')
at /appname/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
665| // If an exception occurs when attempting to run a query, we'll format the error
666| // message to include the bindings with SQL, which will make this exception a
667| // lot more helpful to the developer instead of just the database's errors.
668| catch (Exception $e) {
> 669| throw new QueryException(
670| $query, $this->prepareBindings($bindings), $e
671| );
672| }
673|
Exception trace:
1 PDOException::("PDO::__construct(): The server requested authentication method unknown to the client [caching_s ha2_password]")
/home/app/public_html/test/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
2 PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=achieve_test", "app", "app@Mongol800", [])
/home/app/public_html/test/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
Please use the argument -v to see more details.
原因はログイン認証方法が変わったからとのこと。
MySQL8.0.4以降のログイン認証方式は
caching_sha2_password
がデフォルト
私は下記クエリ実行でパスワード認証方法を変更しました。
パスワードを入力しないとパスワードリセットされるので注意。
mysql> ALTER USER 'vagrant'@'127.0.0.1' IDENTIFIED WITH mysql_native_password BY 'P@ssw0rd';
参考:https://qiita.com/ucan-lab/items/3ae911b7e13287a5b917
DBの文字化けを直す
DBのデフォルト文字コードがlatin1だったので、日本語のレコードを挿入しても全て"?"になってしまっていました。
mysql> show variables like "chara%";
+--------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.01 sec)
設定ファイル修正では直らなかったのでクエリ実行で修正。
mysql> SET character_set_client=utf8mb4;