Enable safe remote connections to your MySQL server on Ubuntu and manage user permissions effectively.
Estimated Time: Approximately 20 - 30 minutes
By default, MySQL on Ubuntu is configured to only accept connections from the local server (`localhost`). This is a strong security measure, but it prevents you from managing your database using tools on your local computer (like MySQL Workbench, DBeaver, or DataGrip) or connecting from another application server.
This guide will show you how to enable remote access to your MySQL server. More importantly, it will demonstrate how to do so securely by:
Properly configuring remote access is essential for flexible database management without compromising your server's security.
20 - 30 minutes
Beginner to Intermediate
Assumes basic command-line knowledge and understanding of IP addresses and firewalls.
sudo
privileges.Enabling remote access to your database is a significant security decision. If not done correctly, it can expose your database to the internet, making it vulnerable to attacks.
Always follow the principle of least privilege:
Always start by ensuring your system is up-to-date.
sudo apt update && sudo apt upgrade -y
Before making any changes to configuration files, it's a good practice to create a backup. The main MySQL configuration file is typically located at `/etc/mysql/mysql.conf.d/mysqld.cnf` on Ubuntu.
sudo cp /etc/mysql/mysql.conf.d/mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.cnf.bak
By default, MySQL listens only on `127.0.0.1` (localhost). We need to change this to allow external connections.
Open the MySQL configuration file:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Locate the `bind-address` line (usually under the `[mysqld]` section). It will likely look like this:
bind-address 127.0.0.1
You have two main options:
For this guide, we'll change it to `0.0.0.0` and use UFW for precise control:
bind-address 0.0.0.0
Save the file (`Ctrl+O`, `Enter`) and exit `nano` (`Ctrl+X`).
This is the **most critical security step**. You must explicitly tell your firewall to allow connections to MySQL's default port (3306) only from trusted sources.
Allow MySQL access from a specific IP address:
sudo ufw allow from YOUR_LOCAL_IP_ADDRESS to any port 3306
Example (if your local IP is 203.0.113.42):
sudo ufw allow from 203.0.113.42 to any port 3306
Reload UFW to apply changes:
sudo ufw reload
Verify UFW status:
sudo ufw status
For the `bind-address` change in `mysqld.cnf` to take effect, you must restart the MySQL service.
sudo systemctl restart mysql
Verify MySQL is listening on all interfaces (or your server's public IP):
sudo netstat -tulnp | grep 3306
Using the `root` MySQL user for remote connections or applications is a major security risk. Instead, create a new user with strong permissions restricted to a specific database and allowed only from your remote IP.
1. Log in to MySQL as root:
sudo mysql -u root -p
2. Create the Database (if you haven't already):
If your application already has a database, skip this. Otherwise, create one:
CREATE DATABASE your_app_database CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
3. Create the Remote User:
Replace `remote_user`, `strong_password`, and `YOUR_LOCAL_IP_ADDRESS` with your chosen values. The `%` wildcard below for the host can be used if you have a range of IPs, but it's *more secure* to use a specific IP.
CREATE USER 'remote_user'@'YOUR_LOCAL_IP_ADDRESS' IDENTIFIED BY 'strong_password';
CREATE USER 'remote_user'@'%' IDENTIFIED BY 'strong_password';
4. Grant Specific Permissions:
Grant only the necessary privileges on your specific database to this new user. For most web applications, `SELECT`, `INSERT`, `UPDATE`, `DELETE` are sufficient. Avoid `ALL PRIVILEGES` unless you fully understand the implications.
GRANT SELECT, INSERT, UPDATE, DELETE ON your_app_database.* TO 'remote_user'@'YOUR_LOCAL_IP_ADDRESS';
If you used `'%'` for the host when creating the user, use it here too:
GRANT SELECT, INSERT, UPDATE, DELETE ON your_app_database.* TO 'remote_user'@'%';
5. Apply Privilege Changes:
FLUSH PRIVILEGES;
6. Exit MySQL:
EXIT;
Now, try connecting to your MySQL server from your local computer using the new user's credentials.
From your local terminal:
mysql -h YOUR_SERVER_IP -u remote_user -p your_app_database
If the connection is successful, you will see the MySQL prompt (`mysql>`). You can then run a simple command:
SHOW TABLES;
Using a GUI tool (e.g., MySQL Workbench, DBeaver):
Configure a new connection with the following details:
Confirm your remote MySQL access is both functional and secure:
You have successfully and securely enabled remote access to your MySQL server on Ubuntu, and, more importantly, created a dedicated user with restricted permissions. This setup allows for flexible database management while adhering to critical security best practices.
Consider these advanced steps for even greater security and manageability:
Need Advanced Database Security or Administration? Contact Us!