A tutorial of remote connection to MySQL database under Linux system

  • 2021-08-17 01:17:35
  • OfStack

Preface

Recently, I encountered this demand in my work, and it is estimated that it took more than one hour to do a good job in this remote connection. One local computer and one cloud server are linux systems. Let's take a look at the detailed introduction:

Steps

1. Open remote access on the server side

First enter the mysql database, and then enter the following two commands:


grant all privileges on *.* to 'root'@'%' identified by 'password';
flush privileges;

The first * is the database, which can be changed to the name of the database that is allowed to be accessed

The second is the table name of the database, which means that access to any table is allowed

root stands for the user name used by remote login and can be customized

% is allowed for any ip login. If you want to specify a specific IP, you can replace%

password stands for the password used when logging in remotely and can be customized

flush privileges; This is for permissions to take effect immediately

2. Modify the my. cnf configuration file

This is the configuration file of mysql. If you can't find the untitled article, you can enter it find /* -name my.cnf Find

Edit the file through vim and find bind-address    = 127.0.0.1 This sentence, then add a # in front of the comment, save and exit

3. Restart the service


service mysql restart

4. Connect remotely locally

Enter at the terminal:


mysql -h  Server ip Address  -P 3306 -u root -p

Then enter the password.

root is the user name set at point 1, and the password is also the password set at point 1

1 Some details

I found a lot of articles on the Internet, saying that I need to open port 3306 to connect, but I still can't connect when I open it. Later, I found some articles, saying that I want to change my. cnf, that is, the second point above, and then restart the server.

I just interviewed another server for 1 time, but I didn't configure the port. Through the above 3 steps, I quickly connected it.

So the second point is very important. Basically, everyone will configure that file when installing mysql, because the character set needs to be configured. So there must be that file, just look for it with find command.

Summarize


Related articles: