Sometimes we only use MySQL for localhost, which means that your application and MySQL databases are on one server for simplicity.
And this guide will help you to enable remote access MySQL, so in case you want to build a dedicated database server and make it another server (for example web server) to connect your database server.
Step 1: Create a Remote MySQL User
We need to create a remote mysql user to allow connect it to our database server, open mysql root session with:
mysql -u root
and execute the following query:
CREATE USER 'newremoteuser'@'%' IDENTIFIED BY 'remote_password';
newremoteuserwith your desired mysql username, and
remote_passwordwith mysql password
- % means that this newremoteuser can connect to this database server from the Internet (any host).
Step 2: Remote Access MariaDB
After we installed the latest MariaDB server (I've posted this tutorial here), open the configuration of your mariadb (my version MariaDB 10.6) with your favorite editor:
Scroll until you find
bind-address, replace it with:
save it with
Restart your mysql / mariadb server with the following command:
service mysql restart
Step 3: Check Remote Access
To start to check remote access of your database server is successfully configured or not, first log in to another server (for example web server). Install
mariadb-client if you don't install it before, use the following command in the ssh session:
mysql -u newremoteuser -p -h 188.8.131.52 -P 3306
newremoteuserto your mysql user that was previously created on step 1.
184.108.40.2064to your database server's IP public address.
Optional: Check Firewall
If you are using default firewall Uncomplicated Firewall (UFW), use the following command to allow IP to connect remote mysql your database server:
If you are using Config Server Firewall (CSF), make sure to add port 3306 on an incoming connection (TCP_IN) (UDP_IN) to allow everyone on the Internet to connect remotely mysql your database server.
Or, you can add whitelisted IP on
Restart CSF with
csf -r command to apply the configuration.
Thanks for reading!