MYSQLの初期パスワードの調べ方

mysqlをインストールした際に初期DBが勝手に作成されるのだが、rootの初期パスワードも勝手に作成されているので、その調べ方と変更方法についてまとめ

※本記事の環境はCENTOS7の環境のケースです。OS、ディストリビューションが異なる場合パスが違っていたりするので、その環境に適合した記事をさがして下さい
 どんなOS・ディストリビューションか不明な場合は /etcの中に  xxxxx-release  というファイルがあるので (centos場合はcentos-release)
 cat /etc/xxxxx-release とやってみると情報が得られる可能性があります

cat /var/log/mysqld.log | grep root
2020-10-13T09:03:00.674264Z 1 [Note] A temporary password is generated for root@localhost: >dAXIVHBD0e% 
2021-05-28T03:14:56.251967Z 2 [Note] Access denied for user 'root'@'localhost' (using password: NO) 
2021-05-28T03:15:05.229814Z 3 [Note] Access denied for user 'root'@'localhost' (using password: NO) 
2021-05-28T03:18:10.687484Z 4 [Note] Access denied for user 'root'@'localhost' (using password: NO) 
2021-05-28T03:18:19.472018Z 5 [Note] Access denied for user 'root'@'localhost' (using password: NO) 
2021-05-28T04:34:31.842797Z 7 [Note] Access denied for user 'root'@'localhost' (using password: YES) 
・・・・

A temporary password is generated for root@localhost:  に続く>dAXIVHBD0e%  の部分が初期パスワード
何故か mysql -uroot -p>dAXIVHBD0e% とやっても接続出来ないので下記のような手順で接続する

mysql -uroot -p
password:         <-ここで入力

で、このままだと使い悪いので初期パスワードから変更しておく

set password for root@localhost=password('新しいパスワード');

こうすると普通に接続できるようになる

mysql -uroot -p新しいパスワード
mysql: [Warning] Using a password on the command line interface can be insecure. 
Welcome to the MySQL monitor.  Commands end with ; or \g. 
Your MySQL connection id is 22 
Server version: 5.7.31 MySQL Community Server (GPL)

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>


この記事が気に入ったらサポートをしてみませんか?