Home  >  Article  >  Database  >  How to connect remotely with mysql

How to connect remotely with mysql

PHPz
PHPzOriginal
2023-04-20 10:14:5612442browse

MySQL is a popular relational database management system that is widely used in many applications. When sharing data between multiple applications, it is necessary to use different MySQL clients to connect to different MySQL servers. If you want to use the MySQL client on one computer to connect to the MySQL server running on another computer, you need to connect through the network, which is called a remote connection. In this article, we will discuss how to connect remotely using MySQL.

Configure MySQL server to allow remote connections

If you want to allow remote connections, you need to do some configuration on the MySQL server. First, find the bind-address parameter in the MySQL configuration file. This parameter specifies which IP address the MySQL server should listen to. By default, bind-address is set to 127.0.0.1, which means MySQL only listens for local connections. If you want to allow connections from other computers, you need to set bind-address to that computer's IP address.

In addition, it is also necessary to create a MySQL user on the MySQL server that allows remote connections. You can create a new user using the following command:

CREATE USER 'remoteuser'@'%' IDENTIFIED BY 'password';

This will create a MySQL user named "remoteuser" and set its password to "password". Note that @'%' means that the user can connect to the MySQL server from anywhere, while @'localhost' means that the user can only connect from locally.

Next, grant access to the user. You can grant all permissions to a user using the following command:

GRANT ALL ON *.* TO 'remoteuser'@'%';

This way, the user will get full access to all databases and tables. If you only want to grant a user access to a specific database or table, you can use the following command:

GRANT ALL ON mydatabase.* TO 'remoteuser'@'%';

This will limit all permissions for that user to all tables in the mydatabase database.

Finally, refresh the MySQL permissions using the following command:

FLUSH PRIVILEGES;

After performing the above steps on the MySQL server, you can allow connections from the remote computer.

Using the MySQL client for remote connections

To use the MySQL client for remote connections, you need to know the IP address of the MySQL server, the port number, and the credentials of the MySQL user.

First, install the necessary software on the MySQL client. There are many different MySQL clients to choose from, including the command line client officially provided by MySQL and the MySQL Workbench GUI client.

Then, open the MySQL client and enter the following command:

mysql -u remoteuser -p -h mysql_server_ip_address

Where,

  • -u specifies the MySQL username to use
  • -p Requests MySQL to prompt for a password
  • -h Specifies the IP address or host name of the MySQL server

When prompted for a password, enter the MySQL user's password to log in to the MySQL server. If you can log in successfully, you can use the MySQL server through the MySQL client.

Conclusion

In this article, we discussed how to configure remote connections on the MySQL server and use the MySQL client for remote connections. Remote connection to MySQL is very useful, allowing you to deploy the application and database separately on different computers. However, to ensure security and data integrity, remote connections should be handled with caution.

The above is the detailed content of How to connect remotely with mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn