mysql中,root用户密码被遗忘,该如何进行重置?

需求描述

  在mysql的测试环境中,有时候会遇到一段时间之后root用户的密码被遗忘的情况,

  这个时候,就是需要对root密码进行重置,不过,在生产环境中,这种情况还是很少见。

环境描述

  操作系统版本:Red Hat Enterprise Linux Server release 6.6 (Santiago)

  数据库版本:5.5.57-log

操作过程

1.使用操作系统用户(mysql)登录系统(这个用户一般为启动mysql的用户,比如:mysql)

[mysql@redhat6 data]$ 

备注:mysql server是以mysql这个系统用户运行的,所以要登录到操作系统,mysql用户下。

2.通过kill命令,关闭mysql数据库

kill `cat redhat6.pid`

备注:redhat.pid文件存放的mysqld的进程ID,进入到pid所在的目录中,执行以上的命令,kill掉mysqld的进程。pid文件的一般都是.pid为后缀,主机名为前缀。

3.创建初始化文件

echo "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('chuangzhang503');" > mysql-init

备注:

--1)初始化文件的名字叫做mysql-init,这个名字,可自行修改,就是一个文本文件。

--2)密码处填写自己要使用的新的密码。

4.执行启动mysqld服务,使用--init-file参数,执行文件中的命令

mysqld --init-file=/mysql/data/mysql-init &

 执行过程

[mysql@redhat6 data]$ mysqld --init-file=/mysql/data/mysql-init &
[1] 2963
[mysql@redhat6 data]$ 180320 10:11:03 [Warning] The syntax '--log-slow-queries' is deprecated and will be removed in a future release. Please use '--slow-query-log'/'--slow-query-log-file' instead.
180320 10:11:03 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
180320 10:11:03 [Note] mysqld (mysqld 5.5.57-log) starting as process 2963 ...

备注:通过使用--init-file参数启动mysql数据库,已经执行了mysql-init文件中的内容,重置了root@localhost的密码。

5.通过重置后的root密码进行登录

[mysql@redhat6 data]$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.5.57-log MySQL Community Server (GPL)

Copyright (c) 2000, 2013, 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密码已经被重置,可以进行正常的登录。

6.删除创建的初始化文件mysql-init

rm mysql-init

文档创建时间:2018年3月20日10:14:27

原文地址:https://www.cnblogs.com/chuanzhang053/p/8607657.html