How to add remote MySQL connection in linux?

Please refer to the following 3 steps to add a remote MySQL connection in Linux.

Edit MySQL config file

  1. Use your appropriate editor to open the MySQL config file. In this tutorial, we will use a nano text editor. Perform the below command in order to open the config file.

    # sudo nano /etc/mysql/ mysql.conf.d/mysql.cnf
  2. The location of the config file may vary based on the version in use. Once you open the config file, go to the Bind-Address line. The default IP will be You will need to change the IP address but make sure that the new IP address should match the server.

  3. Once you make the necessary changes in the file, save it and exit from the config file.

  4. In order to implement changes in the file, we need to restart the service.

    # sudo systemctl restart mysql

Need to setup Firewall to allow remote connection

  1. If you have noticed, in the configuration file there is line “port =3306”. Now, we will need to open traffic for the specific port.

  2. Uncomplicated Firewall is the default tool in linux. Fire the below command to allow traffic and match IP.

    # sudo ufw allow from remote_ip_address to any port 3306
  3. The firewall tool in CentOS uses zones to dedicate what traffic is to be allowed. We’ll create a new zone to set the rules for the MySQL server traffic.

    # sudo firewall-cmd --new-zone=rule_name --permanent
    # sudo firewall-cmd --reload
    # sudo firewall-cmd -permanent --zone=rule_name --add-source=
    # sudo firewall-cmd --permanent --zone=rule_name --add-port=3306/tcp
    # suo firewall-cmd --reload
  4. Now, we will open MySQL port along with iptables to unrestricted traffic.

    # sudo iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
  5. To provide access to specific IP address.

    # sudo iptables -A INPUT -p tcp -s --dport 3306 -j ACCEPT
  6. Above command will allow IP address Please note that you will have to change to your IP address. Once your fire the above command, it is necessary to save the changes that we made to the IPtable rules.

    # sudo netfilter-persistent save
    # sudo netfilter-persistent reload
    # service iptable save

Connect with MySQL remotely

  1. Run the below command in order to establish connection with MySQL remotely.

    # mysql -u username -h mysql_server_ip -p
    -u username - it represents the username of your MySQL username.
    -h mysql_server_ip - IP or Hostname of your MySQL server.
    -p - it will prompt you to enter the password for the MySQL.
  2. Once your connection gets successful, you’ll see a success message like this.

  3. Connection to mysql_server ip 3306 port [tcp/mysql] succeeded!

This is how you can add a Remote MySQL connection in Linux.

Related Articles

How to setup Database Map tool from WHM Panel?

cPanel provide a feature called Database Map tool. You can provide ownership of databases and...

How to set Automate Tasks on a Linux VPS Using Cron?

One of our Linux VPS customers faced an issue wherein his Virtual Machine’s system clock was...

How to determine what is using the most disk space on a Linux server?

There are times when you receive following error while accessing the cPanel WebMail or WHM. You...

How To Configure Redis Caching to Speed Up WordPress on Ubuntu 18.04?

Redis is an open-source in-memory data structure store. It can be used as a DB and cache. It...

How to use the Rsync command in linux?

Rsync is an important command in Linux and used to synchronize or copy the files and directories...

  • 0 Users Found This Useful

Was this answer helpful?