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

I have some PHP applications that run with the MySQL database server. I have set up MySQL database a long time ago and I have forgotten root password of MySQL server. How can I recover and reset the MySQL Server password? One way to reset MySQL password is stopping MySQL daemon. Starting it with recovery mode without authentication. Login with root and make required password changes.

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

停止MySQL服务器守护程序 (Stop MySQL Server Daemon)

MySQL server locks databases during running. So we need to release the lock by stopping MySQL service.

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

$ sudo /etc/init.d/mysql stop

启动MySQL服务器后台程序恢复模式 (Start MySQL Server Daemon Recovery Mode)

We will start MySQL server daemon in recovery mode so there will be on authentication to connect MySQL server daemon. We need root privileges to accomplish these operations.

我们将以恢复模式启动MySQL服务器守护程序,因此将进行身份验证以连接MySQL服务器守护程序。 我们需要root特权才能完成这些操作。

$ sudo mysqld_safe --skip-grant-tables &

不使用密码连接MySQL服务器 (Connect MySQL Server Without Password)

We can connect to MySQL server without password just providing user name root like below.

只需提供如下所示的root用户名,我们就可以不用密码连接到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>

为root用户设置新密码 (Set New Password For Root User)

We will set the password by updating users tables password column

我们将通过更新用户表的密码列来设置密码

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

We will set a new password for the root user.

我们将为root用户设置一个新密码。

再次正常启动MySQL Server守护程序 (Start MySQL Server Daemon Normally Again)

We will start MySQL daemon again.

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

$ /etc/init.d/mysql restart
LEARN MORE  chage Command Tutorial With Examples To Manage User Accounts In Linux
了解更多chage命令教程,以及在Linux中管理用户帐户的示例

翻译自: https://www.poftut.com/how-to-recover-or-reset-lost-mysql-password-on-linux/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值