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.
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
- Open the configuration file: Use a text editor, like
nanoorvim.
- Locate the
bind-addresssetting: Find the line withbind-address.
- Change the bind address:
- To listen on all interfaces, set the address to
0.0.0.0:
- Alternatively, bind it to a specific IP to limit the accessibility:
- Save and exit: Press
CTRL + X, followed byY, and thenEnterto save changes innano. - Restart MySQL service:
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:
- Log in to the MySQL shell:
- Execute the grant statement:
- Flush privileges:
- Exit the MySQL shell:
Updating Firewall Settings
Firewalls might block remote connections. Therefore, ensure the MySQL port (default is 3306) is open:
Using UFW (Uncomplicated Firewall) on Ubuntu
- Allow MySQL through UFW:
- Check UFW status:
- Enable UFW if not enabled:
Using Firewalld on RHEL/CentOS
- Open port 3306:
- Reload firewall changes:
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.logfor any error messages. - Ensure Network Availability: Use
pingandtelnetto 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:
| Steps | Description |
| Edit Configuration | Update bind-address in mysqld.cnf
Restart MySQL service |
| Modify Privileges | Grant access with SQL statements Flush privileges |
| Adjust Firewall | Open port 3306 using UFW or Firewalld |
| Secure Connections | Use VPN, enable SSL/TLS, strong passwords |
| Troubleshoot | Check 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
- How to alter a column and change the default value?
- How to alter databasechangelog.filename for Spring Boot and Liquibase?
- How to annotate MYSQL autoincrement field with JPA annotations
- How to append a value to list attribute on AWS DynamoDB?
- How to api-query for the default vhost
- How to assign a static IP to a pod using Kubernetes on deployment
- How to apply a global query on the distibuted database tables that I have created
- How to auto generate migrations with Sequelize CLI from Sequelize models?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.