Pycharm connects to the pit encountered by mysql, and reports an error Access denied for user 'root'

Yesterday, I was trying to connect to mysql with pycharm, so as to add, delete, modify and check the database in python. But in the step of connecting to mysql in pycharm, I encountered several very difficult problems. After reading other tutorials, the connection was completed quickly, but I always reported an error here. The command line connection to mysql was successful, but the connection in the ide would report an error. Finally, it took several hours to solve all the problems. Here are some reasons and solutions for the problems, hoping to help people who also encounter difficulties.

1. Operation process

Open the database on the right side of pycharm, click the + sign to add a database, and select mysql;

In the dialog box that pops up, there are several items that need to be filled in: user, password, database. If the driver is not installed as shown below, you need to download the driver, and finally you can test whether the connection is successful.

a. User: Mysql的username

b. Password: Mysql的password

c. Database: database name

d. Automatically download missing drivers

e. Test connection: Test whether the connection is successful

 

二、报错:The specified database user/password combination is rejected: com.mysql.cj.exceptions.CJException: Access denied for user 'root'

 There are several possible reasons for this problem: (You can check each item)

1. Wrong username and password

At first I thought it was to customize the user name and password, but after searching, I found out that the user name here needs to be the user name of mysql;

View mysql user name

select user,host from mysql.user;

Then I filled in the user name root, but I still can’t connect, considering that the password may be wrong; (but in fact, the password of the root user is the password entered during mysql installation) 

view password

SELECT User, Password, Host FROM user;

This command encountered an error ERROR 1054 (42S22): Unknown column 'Password' in 'field list'

The reason for the error is that there is no password field in the mysql database, and the password field has been changed to authentication_string

SELECT User, authentication_string, Host FROM user;

But using the root user name and the correct password still fails to connect;

2. Username does not have remote access

The root account does not allow remote access permissions by default, so you need to modify the relevant permissions to add remote access permissions to the database;

Check the host of the root user in the table. The default localhost should be displayed, which only supports local access and does not allow remote access.

% Indicates that all hosts are wildcarded and can access remote.

First select the mysql database

mysql>use mysql

set access permissions

Tried the following commands, all encountered errors:

mysql> GRANT ALL PRIVILEGES ON . TO 'root'@'%' IDENTIFIED BY '密码' WITH GRANT OPTION;
mysql> grant all privileges on . to root@localhost identified by '密码';

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '. TO 'root'@'%' IDENTIFIED BY '密码' WITH GRANT OPTION' at line 1

According to the search, you need to create a user first, and then execute the authorization;

Create a user first, (the password here is set by yourself);

mysql> CREATE USER 'test'@'%' IDENTIFIED BY '密码';

Then assign user permissions to the user;

mysql> GRANT ALL PRIVILEGES ON *.* TO 'test'@'%' WITH GRANT OPTION;

Finally refresh permissions;

mysql> FLUSH PRIVILEGES;

Check again, you can see that the newly created user test has the remote login permission.

select user,host from mysql.user;

 But the connection still fails again;

3. Check whether the background mysql is started

ps -ef|grep mysql

Can see mysql related processes;

Another way to check if mysql is started:

mysql.server status

But this command will report an error on my side -bash: mysql.server: command not found

One theory is that the environment variable is not configured, but I have configured it; I don’t know why, and it can only be used in the following way for the time being:

/usr/local/mysql/support-files/mysql.server status

Executing this command shows ERROR! MySQL is running but PID file could not be found

Reason: mysql process exception

Find and kill all mysql-related processes;

After deleting a bunch of pycharm connection mysql processes, only this process remains;

 

Here the mysql process cannot be killed, and when mysql is closed illegally, it will be restarted by it;

I saw a saying that there are many ways to start the mysql service:

mysql.service start - for MySQL installed using package managers

brew services start mysql - for MySQL installed with Homebrew
Using the preference pane for macOS official MySQL installation

focus:

Each time the MySQL service is started, a PID file is generated at a specific location based on the configuration used by MySQL.

A PID file location mismatch occurs when the PID file is generated in one location, but the currently running command looks for the PID file in another location.

After starting the mysql service in the System Preferences UI panel that is bundled with the mysql installation files, MySQL generates a PID file named mysqld.local.pidin the directory./usr/local/mysql/data/

To fix this, mysql.server startthe currently running MySQL thread needs to be stopped from the System Preferences UI panel before running the command again.

Stop the mysql service from the system preference panel, and then run ps -ef | grep mysql, and found that there is indeed no mysql process;

 

At this time, check /usr/local/mysql/support-files/mysql.server status, prompt:

In line with expectations.

Then try to restart the mysql service

sudo /usr/local/mysql/support-files/mysql.server start

 

Started successfully.

But trying to connect to mysql again in pycharm still failed.

 4. Whether the port number is correct or whether the port number is occupied

url:jdbc:mysql://localhost:3306

Be careful to change the port number to your own mysql default listening port, some are 3306, some are 3308. (In general, the default port number is correct, and a self-examination method is also provided here)

(1) The my.init file in the windows computer can check the port number of mysql, and the my.init file is in the mysql installation directory; (there is no my.ini file under the mac, but my.cnf under the mac, and some settings can be customized after creation in /usr/local/mysql/support-files/) (2) Another way to query the port number is to enter mysql in the terminal, then enter status, press Enter, and you can also query mysql and port number and other information;

 (3) However, the status command under mac does not view the port number, and the following command can be used to view the port number under mac:

show global variables like 'port';

The port number is correct.

Next check to see if the port is in use.

sudo lsof -i :3306

 If there is a process occupied, kill the corresponding process.

5. The driver version does not correspond

The mysql driver version installed in pycharm needs to correspond to the mysql version installed on the computer; 5.x corresponds to 5.x, 8.x corresponds to 8.x;

The mysql I installed is version 8.0.31, (Installation tutorial reference (2023) mac installation mysql_Vermouth_00's blog - CSDN blog )

So the driver I installed is version 8.x.

If the driver version does not correspond, you can switch the driver version here. 

三、报错[08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up. java.net.ConnectException: Connection refused (Connection refused).

This error is relatively common, basically because of the time zone problem, you can change the url, where the name of the database is changed to your own;

jdbc:mysql:// localhost:3306/database name?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC


or

jdbc:mysql://localhost:3306/test?serverTimezone=GMT

Even after changing the URL it still doesn't work.

At this time, my command line connection to mysql suddenly failed, and an error was reported:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

So ps -ef|grep mysql again, then kill the process, restart the mysql service, then check that the port number is not occupied, try to connect again, and finally succeed.

Four. Summary 

The specified database user/password combination is rejected: com.mysql.cj.exceptions.CJException: Access denied for user 'root'

This error is not common, and there are few solutions found. If someone encounters it unfortunately, you can troubleshoot according to the possible reasons provided in this article. New solutions are welcome to discuss~

Summarize the key points for me to solve this problem: 1. Grant remote access permissions to users; 2. Restart the mysql service; 3. Check the port occupation and kill the process.

I hope everyone can go well, don't step on the pit~

Guess you like

Origin blog.csdn.net/Vermouth_00/article/details/130969577