如何在Linux中重置MySQL root密码

mysql是一种广泛用于数据存储的开源数据库软件。有时我们忘记了mysql root密码,但是不需要紧张,本篇文章将介绍关于如何通过简单的步骤重置mysql root密码。

如何在Linux中重置MySQL root密码

(相关推荐:MySQL教程

步骤1:在安全模式下启动MySQL

首先,需要停止运行mysql服务器。我们使用以下命令之一在linux系统上停止MySQL服务器。

# service mysql stop           //对于基于SysVinit的系统 # systemctl stop mysql.service     //对于基于Systemd的系统

现在在安全模式下使用–skip grant tables选项启动mysql服务器。使用以下命令以安全模式启动MySQL。在安全模式下,MySQL不提示输入登录密码。

# mysqld_safe --skip-grant-tables &

步骤2:重置mysql root密码

现在以root用户身份登录到mysql服务器,并使用以下命令更改密码。这将重置系统上的mysql root密码。

对于MySQL5.6或更低版本

# mysql -u root  mysql>USE mysql;  mysql>UPDATE user SET password=PASSWORD("NEW-PASSWORD") WHERE User='root';  mysql>FLUSH PRIVILEGES;  mysql>quit

对于MySQL5.7或更高版本

# mysql -u root mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD("NEW-PASSWORD"); mysql>FLUSH PRIVILEGES; mysql>quit

步骤3:重启mysql服务器

更改密码后,停止mysql(在安全模式下运行)服务,并使用下面的命令重新启动它。

//基于SysVinit的系统 # service mysql stop  # service mysql start  //基于Systemd的系统  # systemctl stop mysql.service # systemctl start mysql.service

步骤4:验证新密码

重新设置mysql root账号密码并重启后,只需登录验证新密码即可。

# mysql -u root -p  Enter password: ********** Welcome to the MySQL monitor.  Commands end with ; or g. Your MySQL connection id is 29 Server version: 5.5.57 MySQL Community Server (GPL)  Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.  Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.  Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.  mysql>

本篇文章到这里就已经全部结束了,更多精彩内容大家可以关注php中文网的其他相关栏目教程!!!

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享