EC2のAmazon Linux2では、mysqld_safe
コマンドはないようなので、--skip-grant-tables
オプションで起動する必要がある。
--skip-grant-tables
オプションを有効化
sudo systemctl set-environment MYSQLD_OPTS="--skip-grand-tables" sudo systemctl restart mysqld
--skip-grant-tables
オプションのおかげでMySQLにパスワードなしでログインできる
mysql -u root
--skip-grant-tables
オプションが有効だとALTER USER でパスワード変更できないので、一度パスワードを空にする
mysql > UPDATE mysql.user SET authentication_string=null WHERE User='root'; mysql > exit;
--skip-grant-tables
オプションを無効にする
sudo systemctl set-environment MYSQLD_OPTS="" sudo systemctl restart mysqld
ALTER USER
でrootユーザのパスワードを変更する
USE mysql; ALTER USER 'root'@'localhost' identified BY 'new password';