In this article, I will show you how to expose MySQL and MariaDB database server to the internet. I will be using Ubuntu 18.04 LTS to demonstrate the processes. But, it should work in other Linux distributions as well. The configuration files may be in a different location. That’s the only difference. So, let’s get started.
Do I Need a Public IP Address?
If you want to expose MySQL or MariaDB to the internet, then you will need a public IP address. Otherwise, your server won’t able accessible from the internet.
If you want to access MySQL or MariaDB from only your local network (LAN), then the procedures shown here should work. In that case, you don’t need a public IP address.
Installing MariaDB/MySQL Ubuntu:
MariaDB/MySQL is available in the official package repository of Ubuntu. So, it is easy to install.
First, update the APT package repository cache with the following command:
Now, you can install MySQL or MariaDB database on Ubuntu. They are both technically the same. The only difference is; MySQL is developed by Oracle and MariaDB is community developed. The license of MariaDB is more open than MySQL. MariaDB is a fork of MySQL.
You can install MySQL on Ubuntu with the following command:
If you want to use MariaDB instead of MySQL, you can install it on Ubuntu with the following command:
Once you’ve run the required command to install your desired database package, press y and then press <Enter>. I will go with MariaDB.
MySQL/MariaDB should be installed.
Changing Bind Address of MySQL/MariaDB:
Now, you have to change the bind address of MySQL/MariaDB.
If you’ve picked MySQL, then the configuration file to edit is /etc/mysql/mysql.conf.d/mysqld.cnf
If you’ve picked MariaBD, then the configuration file to edit is /etc/mysql/mariadb.conf.d/50-server.cnf
Now, edit the required configuration file (in my case the MariaDB configuration file /etc/mysql/mariadb.conf.d/50-server.cnf
) with the following command:
Now, scroll down a little bit and find the line as marked in the screenshot below.
Once you do find the line, comment out the line (by putting a # at the beginning of the line).
Now, save the configuration file by pressing <Ctrl> + x followed by y and then press <Enter>.
Now, you have to restart MySQL/MariaDB service.
If you’ve installed MySQL, then run the following command:
If you’ve installed MariaDB, then run the following command:
Creating New Users:
Now, in order to access MySQL/MariaDB remotely, you have to create at least one MySQL/MariaDB database user with remote access privileges.
To do that, login to the MySQL/MariaDB database console as root with the following command:
NOTE: By default, MySQL/MariaDB server has no root password set. If you’re using an existing MySQL/MariaDB server, then it may have root password set. In that case, you can login to the MySQL/MariaDB console as follows:
You should be logged in to the MySQL/MariaDB console.
Now, create a database user with the following SQL command:
NOTE: Replace your_username and your_password depending on what you want the username and password to be. Here, host_ip_addr is the hostname or IP address of the computer from where you want to connect to the MySQL/MariaDB server. You can also use % as host_ip_addr if you want to connect from any computer. It can also be something like 192.168.2.% if you want to connect from computers from the IP range 192.168.2.1 – 192.168.2.254.
Now, grant privileges to the necessary databases to the user you just created. I will just let the user use all the databases.
NOTE: *.* means all databases. You can also use db_name.* to only let the user use the database db_name.
Now, apply the changes with the following SQL command:
Now, exit out of the MariaDB/MySQL console with the following command:
Connecting to the MySQL/MariaDB Server Remotely:
In order to access the MySQL/MariaDB server remotely, you need to know the IP address or hostname of the MySQL/MariaDB server.
To find the IP address of the MySQL/MariaDB server, run the following command on the server:
As you can see, the IP address is in my case 192.168.21.128. It will be different for you. So, make sure you replace it with yours from now on.
You can access the MySQL/MariaDB server from any MySQL/MariaDB client programs including the traditional terminal based mysql client program. There are many graphical MySQL/MariaDB IDEs such as DataGrip, MySQL Workbench etc. In this section, I am going to connect to the MySQL/MariaDB server from the terminal based mysql client program. The MySQL/MariaDB client program is not installed by default. But, you can install them very easily.
To install the MySQL client tools, run the following command:
To install the MariaDB client tools, run the following command:
NOTE: You can install either the mysql-client or the mariadb-client and you will be able to connect to the MySQL/MariaDB server from any of these clients.
Now, from your client machine, connect to the MySQL/MariaDB server remotely with the following command:
Note: Repalce your_username with your MySQL/MariaDB username and host_ip_addr with the hostname or IP address of your MySQL/MariaDB server.
Now, type in the password for your MySQL/MariaDB user and press <Enter>.
You should be connected to the MySQL/MariaDB server remotely as you can see in the screenshot below. I am connected to my MariaDB server.
I can also run MySQL/MariaDB database queries.
So, that’s how you expose MySQL and MariaDB database servers to the internet. Thanks for reading this article.