じゃあ、おうちで学べる

本能を呼び覚ますこのコードに、君は抗えるか

centos7 でmysqlのrootのパスワードを忘れた君へ

はじめに

centos7ではmysqld_safeはインストールできないから--skip-grant-tablesオプションで起動する必要があるぞ

1. mysqldを止める
systemctl stop mysqld
2. MYSQLD_OPTSに変数をセットする
systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
3. mysqldを再び起動
systemctl start mysqld
4. パスワード無しでログイン
mysql -u root
5. パスワードの更新
mysql> UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPassword')
    -> WHERE User = 'root' AND Host = 'localhost';
mysql> FLUSH PRIVILEGES;
mysql> quit
6. 再度,mysqldを停止
systemctl stop mysqld
7. MYSQLD_OPTSに変数を解除
systemctl unset-environment MYSQLD_OPTS
8. 普通にmysqldを起動させます
systemctl start mysqld
7.先ほどセットしたパスワードでログイン
 mysql -u root -p

参照

MySQL :: MySQL 5.7 Reference Manual :: 4.3.2 mysqld_safe — MySQL Server Startup Script