Solve the problem of unable to connect to the database remotely under Linux

origin

Today, when accessing the remote database through mysql workbench in the ubuntu16.04 environment, it is found that the connection cannot be made. The solutions and methods are recorded as follows. If there are any deficiencies, please advise.

 

question

Entering the password to access through the workbench reports this error:

Your connection attempt failed for user 'root' from your host to server at 118.89.153.162:3306:   Access denied for user 'root'@'118.89.153.162' (using password: YES)

 

ideas

  1. Network problem, restart the workbench and MySQL service to connect after changing the network, it is still this error.
  2. The password is incorrect. Check the password of the remote mysql through the ssh remote login server, and find that the entered password is correct.
  3. Remote mysql does not have access rights. Generally, there are two methods: the table modification method and the authorization method    :

 

            Authorization Act:

            Step 1: Try to open the /etc/mysql/my.cnf configuration file. If the file does not exist or the content is empty, open the following file path:

sudo vim /etc/mysql/ my.cnf  
 //或: 
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

            Step 2: Find the following line in the file, if there is no # before the line, you can add # to comment before the line, save and exit.

bind-address = 127.0.0.1    
    

           Change the table method:

mysql>use mysql;
mysql >GRANT ALL PRIVILEGES ON *.* TO ' root ' @ ' % ' WITH GRANT OPTION    // Give any host permission to access and modify all data
 // For example, if you want the root user to use root to connect to the mysql server from any host .
// GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
 // If you want to allow user root to connect to mysql server from host with ip 192.168.1.124 and use 123456 As password
 // GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.124'IDENTIFIED BY '123456' WITH GRANT OPTION; 
mysql>flush privileges // Permission refresh, make changes take effect immediately 
sudo /etc/init.d /mysql restart // or: service mysql restart restart mysql service

           

   I tried both ways and the error type became:

Can't connect to MySQL server on ...

Seeing this error, I thought of the reason for the remote server firewall, so I had the following ideas:

 

        4. The firewall of the remote ubuntu server makes it impossible to access the remote port 3306 locally.

sudo ufw disable // Turn off the firewall, this command needs to be executed under root privileges, use it with caution

Connect to remote mysql again through workbench, and the connection is successful.

 

reason

My local cannot connect to the remote because the remote ubuntu server firewall prohibits external access to port 3306 by default.

 

practice

sudo ufw enable   // Open the firewall 
sudo ufw allow 3306   // Allow external access to port 3306

 

Summarize

              1. The general problem of inability to connect to remote mysql is caused by the above four willingnesses, so I will not repeat them here.

              2. When encountering a problem, the first thing to do is not to find a way to solve the problem immediately, but to think about why the problem occurs, the reasons for the problem, and to prescribe the right medicine.

              3. I still have a lot of shortcomings. Only by guarding against arrogance and impatience can we make progress.

 

Bonus

        Ubuntu commonly used ufw firewall commands

// Installation method 
sudo apt- get install ufw
   
// Enable 
sudo ufw enable
sudo ufw default deny // Turn on the firewall and close all external access to the machine with the system startup, and the local access to the outside is normal.

// Close 
sudo ufw disable

// View firewall status 
sudo ufw status

// Enable/disable the corresponding port or service example 
sudo ufw allow 3306  // Allow external access to port 3306 
sudo ufw delete allow 3306 prohibit external access to port 3306
 sudo ufw allow from  118.89 . 153.162  // Allow this IP to access all local ports 
sudo ufw deny smtp   // Forbid external access to the smtp service 
sudo ufw delete allow smtp // Delete a rule established above 
sudo ufw deny proto tcp from  10.0 . 0.0 / 8 to 192.168 . 0.1 port 22  // To deny all TCP traffic from 10.0.0.0/8 to port 22 of the 192.168.0.1 address

// Recommend to use 
sudo apt- get install ufw
sudo ufw enable
sudo ufw default deny 
Reference 
blog: https://www.cnblogs.com/kluan/p/5993767.html

 

 

 

 

 

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324883286&siteId=291194637