如何在MySQL中重置root密码?

时间:2017-01-26 20:25:58

标签: mysql

我无法使用Ubuntu 16.04连接到MySQL:

mysql -u root -p

返回错误:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

当我尝试使用以下命令启动服务器时

sudo service mysql start
mysqld_safe --skip-grant-tables &

输出是:

# 2017-01-26T20:11:45.329764Z mysqld_safe Logging to syslog.
2017-01-26T20:11:45.333031Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-01-26T20:11:45.336684Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-01-26T20:11:45.358956Z mysqld_safe A mysqld process already exists

如果我用以下方法终止进程:

ps aux | grep "msyql"
kill -9 pid

然后运行:

mysqld_safe --skip-grant-tables &

我明白了:

2017-01-26T20:10:07.979813Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-01-26T20:10:07.984114Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-01-26T20:10:07.987546Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.

这是一个无限循环。那么如果我从头开始重新安装会怎么样?

首先删除MySQL:

sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo apt-get autoremove
sudo apt-get autoclean

然后重新安装:

sudo apt-get update
sudo apt-get install mysql-server
sudo mysql_install_db   // (deprecated command)
sudo /usr/bin/mysql_secure_installation 

就此而言:

sudo /usr/bin/mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:
Error: Access denied for user 'root'@'localhost' (using password: YES)

或:

mysqladmin -u root password [newpass]
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: NO)'

然后它倒下了。

我尝试过这里的所有答案 - ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

有时会根据其他人存储临时密码,但它不会按照建议存储在/var/log/mysqld.log中

有人能帮忙吗?

2 个答案:

答案 0 :(得分:0)

如果您正在运行char并收到以下错误:

mysqld_safe --skip-grant-tables &

可能是因为MySQL 5.7及以上版本在运行时动态创建了 / var / run / mysqld 目录。当您使用2017-01-26T20:10:07.984114Z mysqld_safe Logging to '/var/log/mysql/error.log'. 2017-01-26T20:10:07.987546Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists 停止运行service mysql stop时,它将无效。

您可以在运行mysqld_safe --skip-grant-tables之前手动创建它:

mysqld_safe

然后您可以更改root密码或您尝试执行的任何操作。

答案 1 :(得分:-1)

您如何阅读此网页并尝试了其中描述的方法?

Resetting the Root Password: Unix and Unix-Like Systems