Skip to content

Instantly share code, notes, and snippets.

@someshkar
Last active June 18, 2020 14:34
Show Gist options
  • Save someshkar/9e53ba41524633844f5ded1d1da90b17 to your computer and use it in GitHub Desktop.
Save someshkar/9e53ba41524633844f5ded1d1da90b17 to your computer and use it in GitHub Desktop.

Setup MySQL remote access on any server

This has been tested on AWS, but should work the same pretty much anywhere.

A simple guide for quickly setting up MySQL remote access on any VPS. This involves creating a new user (other than root) and changing the mysqld config to make it listen on all network interfaces.

Step 1: Install MySQL

Run these commands to install MySQL on Debian based operating systems:

sudo apt update
sudo apt install mysql-server

Step 2: Configure MySQL

Run this command to initiate the mysql_secure_installation script:

sudo mysql_secure_installation

Go through the script's steps, I generally turn off remote root login and remove the test database. But the most important step here is to set a root MySQL user password.

Note this down because you'll need it later.

Step 3: Add a user

To get into the MySQL CLI, run this command:

sudo mysql -u root -p

Once prompted, enter the root user's password you set in Step 2.

When your prompt says mysql>, enter the following commands one by one, replacing username and password with the ones you'd like.

CREATE USER 'username'@'%' IDENTIFIED BY 'password';  
GRANT ALL PRIVILEGES ON *.* TO 'username'@'%';  
SHOW GRANTS FOR 'username'@'%';

The last command's output should show a user with the username you set and it should have a long list of privileges.

Make a database

At this step, you might want to make a database from the MySQL CLI using the following command, replacing db_name with your preferred database name:

CREATE DATABASE db_name;

Step 4: Make mysqld listen on all network interfaces

Edit the mysqld.cnf with vim with the command below. You can switch out vim for whichever editor you prefer, like nano.

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

Once in this file, find the bind-address line and replace 127.0.0.1 with 0.0.0.0, to make mysqld listen on all network interfaces.

Step 5: Restart the MySQL daemon

All that's left to do is to run this command to restart the MySQL daemon:

sudo service mysql restart

After this, you should be able to remotely connect using a MySQL client like Adminer. You can find a hosted version of Adminer here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment