文章目录
  1. 1. 问题描述
  2. 2. 解决办法

问题描述

在Centos上用如下命令连接MySQL

mysql -u root

提示错误

Access denied for user 'root@localhost' (using password:NO)

解决办法

  • Stop the service/daemon of mysql running
1
2
[root ~]# service mysql stop   
mysql stop/waiting
  • Start mysql without any privileges using the following option; This option is used to boot up and do not use the privilege system of MySQL.
1
[root ~]# mysqld_safe --skip-grant-tables &
  • enter the mysql command prompt
1
2
[root ~]# mysql -u root
mysql>
  • Fix the permission setting of the root user ;
1
2
3
4
5
6
7
8
9
10
mysql> use mysql;
Database changed
mysql> select * from user;
Empty set (0.00 sec)
mysql> truncate table user;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> grant all privileges on *.* to root@localhost identified by 'YourNewPassword' with grant option;
Query OK, 0 rows affected (0.01 sec)

if you don`t want any password or rather an empty password

1
2
3
4
mysql> grant all privileges on *.* to root@localhost identified by '' with grant option;
Query OK, 0 rows affected (0.01 sec)*
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

Confirm the results:

1
2
3
4
5
6
7
 mysql> select host, user from user;
+-----------+------+
| host | user |
+-----------+------+
| localhost | root |
+-----------+------+
1 row in set (0.00 sec)
  • Exit the shell and restart mysql in normal mode.
1
2
3
4
mysql> quit;
[root ~]# kill -KILL [PID of mysqld_safe]
[root ~]# kill -KILL [PID of mysqld]
[root ~]# service mysql start
  • Now you can successfully login as root user with the password you set
1
2
[root ~]# mysql -u root -pYourNewPassword 
mysql>
文章目录
  1. 1. 问题描述
  2. 2. 解决办法