一:问题描述
--登陆mysql
[root@target_pc software]# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
二:解决办法
[root@target_pc software]# /etc/init.d/mysql stop
Shutting down MySQL.. [ OK ]
[root@target_pc software]# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
[1] 3119
[root@target_pc software]# 140918 23:02:23 mysqld_safe Logging to '/var/lib/mysql/target_pc.err'.
140918 23:02:23 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
--它会停留在这个界面一段时间,感觉需要一直等待一样,此时,直接回车就行了。
[root@target_pc software]# mysql -u root -p
Enter password:
--这里不用输入密码,直接回车就行了。
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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;
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('beijing') where user='root';
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
[root@target_pc software]# /etc/init.d/mysql restart
Shutting down MySQL..140918 23:07:25 mysqld_safe mysqld from pid file /var/lib/mysql/target_pc.pid ended
[ OK ]
Starting MySQL. [ OK ]
[1]+ Done mysqld_safe --user=mysql --skip-grant-tables --skip-networking
[root@target_pc software]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.20
Copyright (c) 2000, 2014, 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> show databases;
ERROR 1820 (HY000): You must SET PASSWORD before executing this statement
解决办法:
--重新设置一次密码:
mysql> SET PASSWORD=PASSWORD('beijing')
-> ;
Query OK, 0 rows affected (0.00 sec)
--现在,再输入命令,就不再报错了:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.01 sec)
--发现8.0不支持password函数,没法这样改,可以先将authentication_string设为空:
update mysql.user set authentication_string='' where user='root' and host='localhost'; #将密码设为空
flush privileges;
然后去掉skip-grant-tables这个参数,重启数据库,然后用空密码登录,然后手动修改个新密码:
alter user root@'localhost' identified by '密码';