如何在Ubuntu中重置MySQL Root密码

如何在Ubuntu中重置MySQL Root密码

如何在Ubuntu上重置MySQL Root密码

最后更新 2019年2月13日

在本文中,我们将通过使用该--skip-grant-tables选项启动MySQL来重置Ubuntu中的MySQL root密码

1.确认MySQL版本(http://www.1nt6.com

首先,您必须确认您正在运行的Ubuntu上的哪个版本的MySQL,因为命令会有所不同。

mysql -V
mysql Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using EditLine wrapper

记下你的“Distrib”。在上面的例子中,我们使用的是MySQL 5.7。请稍后注意这一点。

2.使用skip-grant-table重新启动MySQL()

http://www.1nt6.com


为了跳过授权表并重置root密码,我们必须先停止MySQL服务。

sudo /etc/init.d/mysql stop

确保目录  /var/run/mysqld存在并更正所有者设置。

sudo mkdir /var/run/mysqld
sudo chown mysql /var/run/mysqld

现在用--skip-grant-tables选项启动MySQL  &是必需的。

sudo mysqld_safe --skip-grant-tables&

你应该看到类似的东西:

[1] 1283
user@server:~$ 2019-02-12T11:15:59.872516Z mysqld_safe Logging to syslog.
2019-02-12T11:15:59.879527Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2019-02-12T11:15:59.922502Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

您可能需要按ENTER才能返回Linux BASH提示符。

3.更改MySQL Root密码

您现在可以在没有密码的情况下登录MySQL root帐户。

sudo mysql --user=root mysql

登录后,您将看到  mysql>提示。

对于Ubuntu上的MySQL 5.7或更高版本,请运行此命令以更改root密码。替换  your_password_here为您自己的。

mysql> update user set authentication_string=PASSWORD('your_password_here') where user='root';

对于Ubuntu上的MySQL 5.6或更低版本,请运行此命令以更改root密码。替换  your_password_here 为您自己的。

mysql> update user set Password=PASSWORD('your_password_here') where user='root';

重置root密码时,还必须将auth插件更改为 mysql_native_password

mysql> update user set plugin="mysql_native_password" where User='root';

刷新特权。

mysql>  flush privileges;

退出MySQL。

mysql> exit

确保在再次启动服务之前停止所有MySQL进程。

sudo killall -u mysql

再次启动MySQL。

sudo /etc/init.d/mysql start

4.测试新密码

再次登录MySQL,现在应该提示您输入密码http://www.1nt6.com

sudo mysql -p -u root

输入您的密码。如果正确,你应该看到

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.25-0ubuntu0.18.04.2 (Ubuntu)

你们都完成了!


猜你喜欢

转载自blog.51cto.com/14202983/2351493
今日推荐