You are on page 1of 5

MySQL 忘记 root 密码??重置方法

173780 人看了这个视频
播放列表

如果 MySQL 密码忘记了 root 密码导致无法登录,如下图所示,这个时候怎么办,只能重置 root 密码了。有一个弊端就是需


要停 MySQL 服务,如果这是生产数据库操作起来就没那么方便了

工具/原料
 Windows 10 x64
 mysql-5.7.19-winx64

方法/步骤
1. 1
如果不是以管理员身份运行 cmd 执行命令 net stop mysql 会报“发生系统错误 5 拒绝访问”

2. 2
重新以管理员身份运行 cmd,输入 net stop mysql
停止 MySQL 服务

3. 3
打开一个 cmd 窗口输入 mysqld --skip-grant-tables,注意这个 cmd 命令窗口不要关闭

4. 4
重新打开一个 cmd 窗口,运行下面命令
C:\Users\Administrator>mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.19 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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> use mysql
Database changed
mysql> update user set authentication_string=password('123456') where user='root';
Query OK, 0 rows affected, 1 warning (0.06 sec)
Rows matched: 1 Changed: 0 Warnings: 1
mysql>
将 mysql 的 root 密码重置为 123456

5. 5
继续以管理员身份运行 cmd,输入 net start mysql
启动 MySQL 服务

6. 6
以重置后的 root 密码登录测试,如下图
mysql -uroot -p123456

END
linux 下 mysql 忘记密码怎么办

前言

今天在服务器安装 mysql 之后,登录发现密码错误,但是我没有设置密码呀,最后百度之后得知,mysql 在 5.7 版本之后


会自动创建一个初始密码。
报错如下:

[root@mytestlnx02 ~]# mysql -u root -p


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

修改密码

1. 检查 mysql 服务是否启动,如果启动,关闭 mysql 服务


//查看 mysql 服务状态
[root@mytestlnx02 ~]# ps -ef | grep -i mysql
root 22972 1 0 14:18 pts/0 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/li
b/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/us
r --user=mysql
mysql 23166 22972 0 14:18 pts/0 00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var
/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --p
id-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root 23237 21825 0 14:22 pts/0 00:00:00 grep -i mysql

//关闭服务
[root@mytestlnx02 ~]# service mysql stop
[root@mytestlnx02 ~]#
2. 修改 mysql 的配置文件 my.cnf
my.cnf 配置文件的位置,一般在/etc/my.cnf,有些版本在/etc/mysql/my.cnf
在配置文件中,增加 2 行代码

[mysqld]

skip-grant-tables
作用是登录 mysql 的时候跳过密码验证

然后启动 mysql 服务,并进入 mysql

[root@mytestlnx02 ~]# service mysqld start


[root@mytestlnx02 ~]#
[root@mytestlnx02 ~]# mysql -u root
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
3. 修改密码
连接 mysql 这个数据库,修改用户密码
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 mysql.user set authentication_string=password('root_password') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1

mysql> flush privileges;


Query OK, 0 rows affected (0.00 sec)
cab 注释:
1)mysql 什么时候需要 flush privileges 2016 年 11 月 14 日
flush privileges 命令本质上的作用是将当前 user 和 privilige 表中的用户信息/权限设置从 mysql 库(MySQL 数据库的内置库)中提取到内存里。MySQL 用户数据和权限有修改后,希望在"不重启 MySQL
服务"的情况下直接生效,那么就需要执行这个命令。通常是在修改 ROOT 帐号的设置后,怕重启后无法再登录进来,那么直接 flush 之后就可以看权限设置是否生效。而不必冒太大风险。

2)以上 2.+3.介绍的方法,在 rhel6.7 的 niwoserver 上测试成功!!!,成功修改了 mysql 的 root 密码为“mysql”.—2019-01-05

mysql> exit
4. 重启 mysql 服务
先将之前加在配置文件里面的 2 句代码注释或删除掉,然后重启 mysql 服务,就可以使用刚刚设置的密码登录了。

[root@mytestlnx02 ~]# service mysql start


[root@mytestlnx02 ~]#
[root@mytestlnx02 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.

p.s.

在 CentOS 上的操作方式有所不同。[cab 注释: rhel6.7 上不适用]

执行修改密码的命令一直报错

mysql> update user set authentication_string=password('xxxxxxxx') where User='root';


ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near '('root_password') where User='roo
t'' at line 1
不可能是语法问题,检查了很多遍,最后发现 CentOS 下应该这样操作:

查看初始密码
[root@VM_0_8_centos ~]# grep 'temporary password' /var/log/mysqld.log
2018-09-26T04:25:54.927944Z 5 [Note] [MY-010454] [Server] A temporary password is generated fo
r root@localhost: DN34N/=?aIfZ
可以看到初始密码为 DN34N/=?aIfZ

cab 在 niwoserver 上测试:


[root@niwoserver home]# grep 'temporary password' /var/log/mysqld.log
2017-09-20T08:04:55.302959Z 1 [Note] A temporary password is generated for root@localhost: 0huEri?KPl*3
[root@niwoserver home]#

使用初始密码登录
[root@VM_0_8_centos ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.12 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
修改密码

mysql> ALTER USER 'root' IDENTIFIED BY 'xxxxxxxxx';


ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing t
his statement.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxxxxxx';
Query OK, 0 rows affected (0.11 sec)

mysql> flush privileges;


Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye
重启服务就生效了
[root@VM_0_8_centos ~]# service mysqld stop
Redirecting to /bin/systemctl stop mysqld.service
[root@VM_0_8_centos ~]# service mysqld start
Redirecting to /bin/systemctl start mysqld.service

You might also like