解决在CentOS 7上重装MySQL 5.7.10后root账户无法登陆

来源:互联网 发布:网络心理咨询师招聘 编辑:程序博客网 时间:2024/04/30 00:08
1. Stop mysql:systemctl stop mysqld2. Set the mySQL environment option systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"3. Start mysql usig the options you just setsystemctl start mysqld4. Login as rootmysql -u root5. Update the root user password with these mysql commandsmysql> UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPassword')    -> WHERE User = 'root' AND Host = 'localhost';mysql> FLUSH PRIVILEGES;mysql> quit6. Stop mysqlsystemctl stop mysqld7. Unset the mySQL envitroment option so it starts normally next timesystemctl unset-environment MYSQLD_OPTS8. Start mysql normally:systemctl start mysqldTry to login using your new password:7. mysql -u root -p
----------------------------------------------------------
[root@localhost ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.16 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('XXXXXX') 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)


mysql> quit
Bye
[root@localhost ~]# systemctl stop mysqld
[root@localhost ~]# systemctl unset-environment MYSQLD_OPTS
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@localhost ~]# mysql -uroot -pXXXXXX
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.16


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> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected, 1 warning (0.00 sec)


mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('XXXXXX');
Query OK, 0 rows affected, 1 warning (0.00 sec)


mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)
0 0