Leif160519的blog Leif160519的blog

——————

目录
MySQL—修改root密码方法总结(包括已知密码和忘记密码的情况)
/  

MySQL—修改root密码方法总结(包括已知密码和忘记密码的情况)

一、记得旧密码

方法1: 用SET PASSWORD命令

首先登录MySQL。

格式:

mysql> set password for 用户名@'localhost' = password('新密码'); 

例子:

mysql> set password for root@'localhost' = password('123'); 

方法2:用mysqladmin

格式:

mysqladmin -u用户名 -p旧密码 password 新密码 

例子:

mysqladmin -uroot -p123456 password 123 

方法3:用UPDATE直接编辑user表

首先登录MySQL。

mysql> use mysql; 
mysql> update user set authentication_string=password('123') where user='root' and host='localhost'; 
mysql> flush privileges; 

二、忘记旧密码

windows下:

1. 关闭正在运行的MySQL服务。

2. 打开DOS窗口,转到mysql\bin目录。

3. 输入mysqld --skip-grant-tables 回车。--skip-grant-tables 的意思是启动MySQL服务的时候跳过权限表认证。

4. 再开一个DOS窗口(因为刚才那个DOS窗口已经不能动了),转到mysql\bin目录。

5. 输入mysql回车,如果成功,将出现MySQL提示符>

6. 连接权限数据库: use mysql;

7. 改密码:

mysql>update user set authentication_string=password("123") where user="root"; 

8. 刷新权限(必须步骤):

mysql>flush privileges; 

9. 退出 quit。

10. 注销系统,再进入,使用用户名root和刚才设置的新密码123登录。

ubuntu下

1.停止MySQL服务

sudo service mysql stop

2.进入mysql配置文件目录

cd /etc/mysql/mysql.conf.d 

3.编辑配置文件

sudo nano mysqld.cnf

[mysqld]下面添加 skip-grant-tables(可以试试这个命令:mysqld --skip-grant-tables)

4.启动服务

sudo service mysql start

5.登录mysql

mysql -u root -p

执行以下命令

mysql>update mysql.user  set authentication_string=password('newpassword') where user='root';
mysql>update mysql.user set plugin="mysql_native_password"; 
mysql>flush privileges;
mysql>quit;

6.注解掉skip-grant-tables

7.重启服务

sudo service mysql restart

centos下:

1.修改/etc/my.cnf,最下面添加skip-grant-tables=1

2.重启mysql服务

systemctl restart mysqld

3.登录mysql

mysql -u root -p

执行以下命令

mysql>update mysql.user  set authentication_string=password('newpassword') where user='root';
mysql>update mysql.user set plugin="mysql_native_password"; 
mysql>flush privileges;
mysql>quit;

4.注解掉skip-grant-tables=1

5.重启服务

systemctl restart mysqld

三、故障解决

3.1 ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executin

解决办法
1、 修改用户密码

mysql> alter user 'root'@'localhost' identified by 'youpassword';  

或者

mysql> set password=password("youpassword");

2、刷新权限

mysql> flush privileges;

3.2 Please use --connect-expired-password option or invoke mysql in interactive mode.

此故障一般见于非mysql终端下直接用-e执行命令的时候。解决办法:在命令后加上--connect-expired-password 即可:

mysql -u root -pyoupassword -e "show databases;" --connect-expired-password

“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill

标  题MySQL—修改root密码方法总结(包括已知密码和忘记密码的情况)
作  者Leif160519
出  处https://github.icu/articles/2019/08/30/1567131906756.html
关于博主:坐标南京,运维工程师,如有问题探讨可以直接下方留言。
声援博主:如果您觉得文章对您有帮助,可以评论、订阅、收藏。您的鼓励是博主的最大动力!