1、停止 MySQL Server (on Linux):
sudo systemctl stop mysql
2、安全模式启动数据库:
sudo mysqld_safe --skip-grant-tables --skip-networking &
如果出现如下错误:
2018-02-12T08:57:39.826071Z mysqld_safe Directory '/var/run/mysqld' for UNIX
socket file don't exists. mysql -u root ERROR 2002 (HY000): Can't connect to local MySQL server through socket
'/var/run/mysqld/mysqld.sock' (2) [1]+ Exit 1
创建mysqld路径
sudo mkdir /var/run/mysqld
提供mysql访问权限
sudo chown mysql: /var/run/mysqld
重新运行步骤2
3、进入mysql不需要密码
mysql -u root
4、运行如下mysql命令
FLUSH PRIVILEGES;
如果是 MySQL 5.7.6及更新的版本,设置新密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
如果是 MySQL 5.7.5及更旧的版本
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('新密码');
如果 ALTER USER 命令无效,尝试以下命令:
UPDATE mysql.user SET authentication_string = PASSWORD('新密码') WHERE User = 'root' AND Host = 'localhost';
5、退出mysql
exit;
6、重启mysql
sudo kill `cat /var/run/mysqld/mysqld.pid`
sudo systemctl start mysql
7、登入mysql使用新密码
mysql -u root -p
网上尝试了很多方法无效,按如上流程完成了mysql重置密码。
原文请见https://stackoverflow.com/questions/21944936/error-1045-28000-access-denied-for-user-rootlocalhost-using-password-y/48748685#48748685
网友评论