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

posted @   Zhai_David  阅读(340)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示