问题描述
我正在尝试重置MysqlPasswordReset之后的root密码,但是当我尝试使用–skip-grant-tables启动服务器时,服务器无法启动
-
Ubuntu 16.04.1 LTS(GNU /Linux 4.4.0-59-generic x86_64)
-
mysql Ver 14.14 Distrib 5.7.17,适用于Linux(x86_64)
服务器正在运行
$ mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
停止服务器
$ sudo /etc/init.d/mysql stop
[ ok ] Stopping mysql (via systemctl): mysql.service.
试着从–skip-grant-tables开始
sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
[1] 9856
没有密码连接
$ mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
[1]+ Exit 1 sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking
我也尝试从mysql_safe开始(error.log为空)
sudo mysqld_safe --skip-grant-tables
2017-02-01T16:33:31.382105Z mysqld_safe Logging to syslog.
2017-02-01T16:33:31.383942Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-02-01T16:33:31.386058Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-02-01T16:33:31.388009Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
最佳解决思路
我发现当mysql服务停止并且mysqld_safe无法创建它时我删除了mysql.sock(我找不到原因),所以我的解决方案是备份sock文件夹并在启动之前恢复mysqld_safe
启动服务器
$ sudo service mysql start
去袜子文件夹
$ cd /var/run
备份袜子
$ sudo cp -rp ./mysqld ./mysqld.bak
停止服务器
$ sudo service mysql stop
恢复袜子
$ sudo mv ./mysqld.bak ./mysqld
启动mysqld_safe
$ sudo mysqld_safe --skip-grant-tables --skip-networking &
初始化mysql shell
mysql -u root
更改密码
FLUSH PRIVILEGES;
SET PASSWORD FOR root@'localhost' = PASSWORD('my_new_password');
次佳解决思路
上面的pedronalbert’s答案对我有用,但最后一步现在已弃用并抛出以下警告:
Warning | 1287 | ‘SET PASSWORD FOR = PASSWORD(”)’ is deprecated and will be removed in a future release. Please use SET PASSWORD FOR = ” instead
请改用此命令:
SET PASSWORD FOR root = '<plaintext_password>';