MySQL
Remote Connection
Database Management
SQL
Networking

How to allow remote connection to MySQL

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Configuring a MySQL server to allow remote connections can be necessary for several reasons, such as enabling database administration over a network or integrating remote applications with the database. However, it’s crucial to ensure that these configurations are done securely to prevent unauthorized access. In this article, we'll walk through the steps and provide technical details on how to successfully allow remote connections to your MySQL server.

Pre-requisites

Before configuring your MySQL server for remote access, ensure:

  • MySQL Server is installed and running.
  • You have administrative access to the server.
  • Appropriate firewall rules are set to allow traffic through the MySQL port (default 3306).

Editing the MySQL Configuration File

The MySQL configuration file is typically located in /etc/mysql/mysql.conf.d/mysqld.cnf on Ubuntu-based systems or /etc/my.cnf for RHEL-based distributions. You need to find the bind-address directive, which by default is set to 127.0.0.1.

Steps to Edit

  1. Open the configuration file: Use a text editor, like nano or vim.
bash
    sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
  1. Locate the bind-address setting: Find the line with bind-address.
ini
    bind-address = 127.0.0.1
  1. Change the bind address:
    • To listen on all interfaces, set the address to 0.0.0.0:
ini
      bind-address = 0.0.0.0
  • Alternatively, bind it to a specific IP to limit the accessibility:
ini
      bind-address = your.server.ip.address
  1. Save and exit: Press CTRL + X, followed by Y, and then Enter to save changes in nano.
  2. Restart MySQL service:
bash
    sudo systemctl restart mysql

Configuring User Privileges

MySQL privileges need to be updated to allow remote access. Suppose you want to permit the user remote_user to connect from any host (% is a wildcard symbol). Here’s how you can update privileges:

  1. Log in to the MySQL shell:
bash
    mysql -u root -p
  1. Execute the grant statement:
sql
    GRANT ALL PRIVILEGES ON *.* TO 'remote_user'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
  1. Flush privileges:
sql
    FLUSH PRIVILEGES;
  1. Exit the MySQL shell:
sql
    EXIT;

Updating Firewall Settings

Firewalls might block remote connections. Therefore, ensure the MySQL port (default is 3306) is open:

Using UFW (Uncomplicated Firewall) on Ubuntu

  1. Allow MySQL through UFW:
bash
    sudo ufw allow 3306/tcp
  1. Check UFW status:
bash
    sudo ufw status
  1. Enable UFW if not enabled:
bash
    sudo ufw enable

Using Firewalld on RHEL/CentOS

  1. Open port 3306:
bash
    sudo firewall-cmd --zone=public --add-port=3306/tcp --permanent
  1. Reload firewall changes:
bash
    sudo firewall-cmd --reload

Security Considerations

Granting remote access to your MySQL server can expose it to unauthorized access attempts. Consider implementing these security practices:

  • Use a VPN: Restrict MySQL access to clients connected via a VPN.
  • Enable SSL/TLS: Secure data transmission using --require_secure_transport=ON.
  • Audit Logs: Keep track of all access attempts using MySQL logs.
  • Strong Passwords: Use strong, complex passwords for all MySQL users.
  • Update Regularly: Ensure MySQL server and OS are regularly updated to patch any vulnerabilities.

Troubleshooting Common Issues

Even when correctly configured, remote connectivity to MySQL might face issues:

  • Check MySQL Logs: Examine /var/log/mysql/error.log for any error messages.
  • Ensure Network Availability: Use ping and telnet to verify connectivity.
  • Check User Privileges: Verify that user privileges have been correctly granted.

Summary Table

Here's a condensed table summarizing the key steps and actions:

StepsDescription
Edit ConfigurationUpdate bind-address in mysqld.cnf Restart MySQL service
Modify PrivilegesGrant access with SQL statements Flush privileges
Adjust FirewallOpen port 3306 using UFW or Firewalld
Secure ConnectionsUse VPN, enable SSL/TLS, strong passwords
TroubleshootCheck logs, verify connectivity Ensure correct privileges

This guide should help you configure MySQL for remote connections securely and efficiently. Always keep security as a priority when exposing database services to external networks.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.