MySQL忘记root密码后修改

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介:

MySQL忘记root密码后可以使用下面的方法修改。

1、登录MySQL所在的服务器,手工kill掉MySQL进程

    kill `cat $mysql_data_dir/hostname.pid`

    $mysql_data_dir/hostname.pid为MySQL数据目录,它记录了MySQL服务的进程号。

[root@rhel6 ~]# ps -ef |grep mysql

root      6602     1  0 21:39 ?        00:00:00 /bin/sh ./mysqld_safe --skip-grant-tables --user=mysql

mysql     6754  6602  0 21:39 ?        00:00:01 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --skip-grant-tables --log-error=/var/lib/mysql/rhel6.er

r --pid-file=/var/lib/mysql/rhel6.pidroot      6851  6830  0 21:47 pts/3    00:00:00 grep mysql

[root@rhel6 ~]# cat /var/lib/mysql/rhel6.pid 

6754

[root@rhel6 ~]# kill `cat /var/lib/mysql/rhel6.pid `

[root@rhel6 ~]# ps -ef |grep mysql

root      6859  6830  0 21:48 pts/3    00:00:00 grep mysql

2、使用--skip-grant-tables选项重启MySQL服务

    --skip-grant-tables意思是启动MySQL服务时跳过权限表认证。启动后,连接到MySQL的root将不需要口令。或者在/etc/my.cnf中添加skip-grant-tables参数,启动mysql,service mysql start/systemctl start mysql

[root@rhel6 bin]# ./mysqld_safe --skip_grant-tables --user=mysql &

[1] 6900

[root@rhel6 bin]# 161122 21:52:03 mysqld_safe Logging to '/var/lib/mysql/rhel6.err'.

161122 21:52:03 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

[root@rhel6 bin]# ps -ef |grep mysql

root      6900  6830  0 21:52 pts/3    00:00:00 /bin/sh ./mysqld_safe --skip_grant-tables --user=mysql

mysql     7052  6900  1 21:52 pts/3    00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --skip-grant-tables --log-error=/var/lib/mysql/rhel6.er

r --pid-file=/var/lib/mysql/rhel6.pidroot      7075  6830  0 21:52 pts/3    00:00:00 grep mysql

[root@rhel6 bin]# mysql -uroot

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.6.34-log MySQL Community Server (GPL)

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> 

3、修改root密码

mysql> set password=password('123456');

ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

mysql> use mysql;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> update user set password=password('123456') where user='root' and host='localhost';

Query OK, 1 row affected (0.00 sec)

Rows matched: 1  Changed: 1  Warnings: 0

    由于使用了--skip-grant-tables选项启动,使用"set password"命令更改密码失败,直接更新user表的password字段后更改密码成功。

在5.7版本中password有规则策略,123456不符合。

4、刷新权限表

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

5、重新用root登录时,必须输入新密码

[root@rhel6 bin]# mysql -uroot 

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@rhel6 bin]# mysql -uroot -p

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 4

Server version: 5.6.34-log MySQL Community Server (GPL)

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> 


参考:《深入浅出MySQL》





     本文转自hbxztc 51CTO博客,原文链接:http://blog.51cto.com/hbxztc/1875966,如需转载请自行联系原作者




相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
关系型数据库 MySQL 网络安全
|
1月前
|
关系型数据库 MySQL 数据库
第十四章 演示MYSQL自定义values.yaml绑定PV和PVC和数据库用户密码
第十四章 演示MYSQL自定义values.yaml绑定PV和PVC和数据库用户密码
22 0
|
2月前
|
关系型数据库 MySQL 数据库
mysql中如何将默认用户名root改成其他?
mysql中如何将默认用户名root改成其他?
29 0
|
1月前
|
关系型数据库 MySQL 数据库
mysql数据库密码
mysql数据库密码
|
2月前
|
SQL 关系型数据库 MySQL
阿里云MySQL数据库价格、购买、创建账号密码和连接数据库教程
阿里云数据库使用指南:购买MySQL、SQL Server等RDS实例,选择配置和地区,完成支付。创建数据库和账号,设置权限。通过DMS登录数据库,使用账号密码访问。同地域VPC内的ECS需将IP加入白名单以实现内网连接。参考链接提供详细步骤。
380 3
|
2天前
|
关系型数据库 MySQL Linux
Linux CentOs7 安装Mysql(5.7和8.0版本)密码修改 超详细教程
Linux CentOs7 安装Mysql(5.7和8.0版本)密码修改 超详细教程
|
9天前
|
存储 关系型数据库 MySQL
linux安装MySQL8.0,密码修改权限配置等常规操作详解
linux安装MySQL8.0,密码修改权限配置等常规操作详解
|
19天前
|
关系型数据库 MySQL 数据库
docker 安装mysql(踩坑踩得想哭 详细解决教程)ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using pa
docker 安装mysql(踩坑踩得想哭 详细解决教程)ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using pa
24 1
|
22天前
|
关系型数据库 MySQL 数据安全/隐私保护
MySQL忘记密码后重置密码
MySQL忘记密码后重置密码
9 0
|
7天前
|
关系型数据库 MySQL Linux
【MySQL-10】数据库函数-案例演示【字符串/数值/日期/流程控制函数】(代码演示&可cv代码)
【MySQL-10】数据库函数-案例演示【字符串/数值/日期/流程控制函数】(代码演示&可cv代码)
【MySQL-10】数据库函数-案例演示【字符串/数值/日期/流程控制函数】(代码演示&可cv代码)