如何在Linux上恢复或重置丢失的MySQL密码?

我有一些用MySQL数据库服务器运行的PHP应用程序。我已经建立了MySQL数据库很久以前,我已经忘记了 MySQL服务器的密码。如何恢复和重置MySQL服务器密码?重置MySQL密码的一种方法是停止MySQL守护进程。在没有身份验证的情况下以恢复模式启动。使用root登录并进行所需的密码更改。

null

停止MySQL服务器守护程序

MySQL服务器在运行期间锁定数据库。所以我们需要通过停止MySQL服务来释放锁。

$ sudo /etc/init.d/mysql stop

启动MySQL服务器守护程序恢复模式

我们将在恢复模式下启动MySQL服务器守护程序,这样就可以通过身份验证来连接MySQL服务器守护程序。我们需要root权限来完成这些操作。

$ sudo mysqld_safe --skip-grant-tables &

无密码连接MySQL服务器

我们可以连接到MySQL服务器,不需要密码,只需提供用户名 就像下面一样。

$ mysql -u root Welcome to the MySQL monitor.  Commands end with ; or g. Your MySQL connection id is 2 Server version: 5.7.16-0ubuntu0.16.10.1 (Ubuntu)  Copyright (c) 2000, 2016, 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>

为根用户设置新密码

我们将通过更新users tables password列来设置密码

USE mysql;UPDATE USER SET password=PASSWORD("Thisismypassword") WHERE USER='root';FLUSH privileges;quit

我们将为根用户设置新密码。

重新正常启动MySQL服务器守护进程

我们将再次启动MySQL守护程序。

$ /etc/init.d/mysql restart

相关文章: chage命令教程,带有在Linux中管理用户帐户的示例

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