mysql5.7以上root用户密码重置
仅限centos系统 不要想Windows系统 Windows系统看了很多,几乎都是失败的,所以Windows系统是否可以修改都是问题。
这篇文章是开发中遇到了root用户密码设置错误,或者多次设置导致忘记密码,需要对root密码进行重新设置:
在51博客上看到的博主安静的疯子
[https://blog.51cto.com/quietmadman/1768057]
1,停止 mysql 服务
[root@study usr]# systemctl stop mysqld
2,设置 mysqld 选项 --skip-grant-tables 参数 也有在/etc/my.sqld文件下直接设置这个文件的,但是我在测试过程中并没有生效
[root@study usr]# systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
3,重新启动 mysql
[root@study usr]# systemctl start mysqld
4,执行 mysql -u root 登录 mysql,并更改密码
单独拿出来修改密码的位置,千万别改错了,否则需要自己更新成原样
UPDATE mysql.user SET authentication_string = PASSWORD('设置你的密码'), password_expired = 'N' WHERE User = 'root' AND Host = 'localhost';
[root@study usr]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.11 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> UPDATE mysql.user
-> SET authentication_string = PASSWORD('newrootpasswd'), password_expired = 'N'
-> WHERE User = 'root' AND Host = 'localhost';
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.01 sec)
5,重置完密码后,去掉 --skip-grant-tables 参数后,重启 mysql
如果忘记去掉的话你的mysql依旧是没有密码直接登录的
[root@study usr]# systemctl unset-environment MYSQLD_OPTS
[root@study usr]# systemctl restart mysqld
再次登录,这时候需要再次输入密码,也就是你上面设置的密码
[root@study usr]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.11 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>