To solve the problem: Access denied for user 'root @ localhost' (NO using password)

Error details :

Pymysql connect to the database using mysql, it has been unable to connect,

conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='1234', db='test', charset='utf8')

The reason is that their mysql without a password, which you can enter directly into the root access to the database, but this database may be connected to other such problems will arise in use, so the first step to solve the problem is to set up a password to the root.

Proceed as follows:

[root ~]# mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password:NO)

1. Stop the mysql service

[root ~]# net stop mysql

2. Start mysql without any privileged situation:

First find mysql installation path, find my.ini file, at the end of the file plus the skip-grant-tables

And then start mysql service

[root ~]# net start mysql 

3. Enter the command character mysql

[root ~]# mysql -u root
mysql> 

4. Repair root user permissions settings

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)

verify results:

    mysql> select host, user from user;
+-----------+------+
| host      | user |
+-----------+------+
| localhost | root |
+-----------+------+
1 row in set (0.00 sec)

The exit the shell in normal mode and restart mysql

mysql> quit;
[root ~]# kill -KILL [PID of mysqld_safe]
[root ~]# kill -KILL [PID of mysqld]
[root ~]# service mysql start

6. Now, you can use the password set successful login as root

 [root ~]# mysql -u root -pYourNewPassword 
 mysql> 

  

 

Guess you like

Origin www.cnblogs.com/AIchangetheworld/p/11726383.html