rootユーザーのパスワードを度忘れ
方法1: mycnfにskip-grant-tablesを追加→パスワード変更
方法2: mysqld_safe –skip-grant-tables→パスワード変更
でパスワードを変更可能。
mysql -u root -p Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
方法1- my.cnfの書き換え
設定ファイルをバックアップし、[mysqld]セクション内に
skip-grant-tables を追加すると、パスワードなしでログインできる。
# my.cnfを探す sudo find / -name '*my.cnf*' | sort find: ‘/proc/867/task/867/net’: 無効な引数です find: ‘/proc/867/net’: 無効な引数です find: ‘/run/user/1000/gvfs’: 許可がありません /etc/alternatives/my.cnf /etc/mysql/my.cnf /etc/mysql/my.cnf.bak /etc/mysql/my.cnf.fallback /etc/mysql/my.cnf.migrated /var/lib/dpkg/alternatives/my.cnf # シンボリックリンク先を辿る ls -l /etc/mysql/my.cnf lrwxrwxrwx 1 root root 24 10月 24 01:15 /etc/mysql/my.cnf -> /etc/alternatives/my.cnf ls -l /etc/alternatives/my.cnf lrwxrwxrwx 1 root root 26 10月 24 01:15 /etc/alternatives/my.cnf -> /etc/mysql/my.cnf.migrated ls -l /etc/mysql/my.cnf.migrated -rw-r--r-- 1 root root 4251 10月 24 01:15 /etc/mysql/my.cnf.migrated # シンボリック先のファイル実体をコピー cp -L /etc/mysql/my.cnf /etc/mysql/my.cnf.bak ls -l /etc/mysql/my.cnf.bak -rw-r--r-- 1 root root 4251 12月 9 13:21 /etc/mysql/my.cnf.bak diff /etc/mysql/my.cnf /etc/mysql/my.cnf.bak # my.cnfに skip-grant-tables を追加 vim /etc/mysql/my.cnf # [mysqld]セクション内に skip-grant-tables を追加 # 追加できてるか確認-1 diff /etc/mysql/my.cnf /etc/mysql/my.cnf.bak 45d44 < skip-grant-tables # 追加できてるか確認-2 cat /etc/mysql/my.cnf | grep '\[mysqld\]' -A 1 [mysqld] skip-grant-tables
方法1- MySQLを再起動し、パスワード変更
# mysqlを再起動
sudo /etc/init.d/mysql restart
[ ok ] Restarting mysql (via systemctl): mysql.service.
# パスワードなしでログイン
mysql -u root
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 10.1.23-MariaDB-9+deb9u1 Raspbian 9.0
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
# パスワード変更
MariaDB [(none)]> 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
MariaDB [mysql]> UPDATE mysql.user SET Password=PASSWORD('お好みのパスワード') WHERE User='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [mysql]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
MariaDB [mysql]> exit;
Bye
方法1- my.cnfの戻し
sudo vim /etc/mysql/my.cnf # skip-grant-tables の行を削除 # 消えてるか確認-1 diff /etc/mysql/my.cnf /etc/mysql/my.cnf.bak # 差分なし # 消えてるか確認-2 cat /etc/mysql/my.cnf | grep '\[mysqld\]' -A 1 [mysqld] #
方法1- 変更後のパスワードでログイン
mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 4 Server version: 10.1.23-MariaDB-9+deb9u1 Raspbian 9.0 Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> Bye
方法2- mysqld_safe –skip-grant-tables
多分、こっちを使う人が多いと思う。
オプションに感謝し、MySQLを再起動し、パスワード変更する。
# MySQLを停止
sudo /etc/init.d/mysql stop
# mysqld_safeの確認
which mysqld_safe
/usr/bin/mysqld_safe
# MySQLを起動
# 操作不能になるので、バックグラウンドで起動
sudo mysqld_safe --skip-grant-tables &
[1] 28920
# パスワードを変更
mysql -u root
USE mysql;
UPDATE mysql.user SET Password=PASSWORD('<お好きなパス>') WHERE User='root'; # MySQL5.6以前
UPDATE mysql.user SET authentication_string=PASSWORD('<お好きなパス>') WHERE User='root'; # MySQL5.7以降
FLUSH PRIVILEGES;
exit;
# mysqld_safe のプロセスを探して、killしていく
ps auxf | grep mysql | grep -v grep
root 28920 0.0 0.1 53716 3832 pts/0 S 17:05 0:00
\_ sudo mysqld_safe --skip-grant-tables
root 28922 0.0 0.0 4504 1872 pts/0 S 17:05 0:00
| \_ /bin/sh /usr/bin/mysqld_safe --skip-grant-tables
mysql 29294 0.0 9.7 1152316 199132 pts/0 Sl 17:05 0:00
| \_ /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --skip-grant-tables --log-error=/var/log/mysql/error.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306 --log-syslog=1 --log-syslog-facility=daemon --log-syslog-tag=
sudo kill -9 28920 28922 29294
# MySQLを起動
sudo /etc/init.d/mysql start
ハマりポイント
`mysqld.sock`がない
なければ、作ってからmysqld_safeする。
mysql -u root ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) sudo mkdir -p /var/run/mysqld sudo chown mysql:mysql /var/run/mysqld/ cd /var/run/mysqld/ sudo touch mysqld.sock sudo chown mysql:mysql mysqld.sock sudo chmod 777 mysqld.sock sudo mysqld_safe --skip-grant-tables &
cf. MySQL 5.7でrootユーザのパスワードを再設定
https://qiita.com/is0me/items/91a0af0342c307b94a16
cf. Mysqlのrootユーザのpasswordを初期化(変更)する
https://qiita.com/is0me/items/91a0af0342c307b94a16


コメント