From
https://websiteforstudents.com/allow-remote-access-to-mariadb-database-server-on-ubuntu-18-04/
Allow Remote Access to MariaDB? Database Server on Ubuntu
This brief tutorial shows students and new users how to configure remote access connection to
MariaDB? database servers on Ubuntu 18.04 systems.
By default when you install
MariaDB? database server, it only accepts connections its local host.. The same host computer it is installed on..
If want to connect from a remote client computer from a remote location, you will not be able to connect databases setup on the server.. This brief guide shows you how to enable that...
When configured correctly, you will be able to connect to the database servers from a remote systems and applications not connected to the same subnet or host computer...
If the server is connected directory to the Internet, you may able able to access it from anywhere around the world where Internet access is available.. however, opening up your database servers directly to the internet is not recommended, especially in a production environment..
When you're ready to setup remote database access, please continue below.
Step 1: Install MariaDB? Database Server
If you haven't installed
MariaDB? server and you're looking for a truly open source database server, then
MariaDB? is a great place to start... To install
MariaDB?, simply run the commands below:
sudo apt update
sudo apt install mariadb-server mariadb-client
After installing
MariaDB?, the commands below can be used to stop, start and enable
MariaDB? service to always start up when the server boots...
Run these on Ubuntu
sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service
Next, run the commands below to secure the database server with a root password if you were not prompted to do so during the installation...
sudo mysql_secure_installation
When prompted, answer the questions below by following the guide.
Enter current password for root (enter for none): Just press the Enter
Set root password? [Y/n]: Y
New password: Enter password
Re-enter new password: Repeat password
Remove anonymous users? [Y/n]: Y
Disallow root login remotely? [Y/n]: Y
Remove test database and access to it? [Y/n]: Y
Reload privilege tables now? [Y/n]: Y
Now that
MariaDB? is installed, to test whether the database server was successfully installed, run the commands below...
type the root password when prompted...
mariadb welcome
If you see a similar screen as shown above, then the server was successfully installed...
Step 2: Configure MariaDB? Remote Access
As we mentioned above, all remote access to the server is denied by default... To enable remote access, you'll need to set the bind-address to allow for remote access..
For example, to allow all IPv4 addresses, set the bind-address to: 0.0.0.0 This will allow
MariaDB? server accepts connections on all host IPv4 interfaces... If you have IPv6 configured on your system, use ::
On Ubuntu systems with
MariaDB? database server installed, its default configuration file is located at: /etc/mysql/mariadb.conf.d/50-server.cnf
Simply run the commands below to open
MariaDB? configuration file...
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
Depending on your systems, you may find that same configuration file maybe at the location below:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
When the file is opened, search for a line that begins with bind-address as shown below... It default value should be 127.0.0.1..
# this is read by the standalone daemon and embedded servers
[server]
# this is only for the mysqld standalone daemon [mysqld]
#
# Basic Settings
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
# Instead of skip-networking the default is now to listen only on localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
#
When you need to do is change the default value 127.0.0.1 to 0.0.0.0 as shown below:
# this is read by the standalone daemon and embedded servers
[server]
# this is only for the mysqld standalone daemon [mysqld]
#
# Basic Settings
# user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
# Instead of skip-networking the default is now to listen only on localhost which is more compatible and is not less secure.
bind-address = 0.0.0.0
#
#
In the same file, you'll want to comment out the line that begin with skip-networking by putting the # before it... or delete it all together.. then save your changes..
Please make sure to add the changes above under the [mysqld] section.
After making the change above, save the file and run the commands below to restart the server.
sudo systemctl restart mariadb.service
To verify that the change happens, run the commands below
sudo apt install net-tools
sudo netstat -anp | grep 3306v
and you should find the result that looks like the one below
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3213/mysqld
Now the server is setup to listen to all IP addresses but individual IP needs to be explicitly configure to connect to a database.
To enable a client to connect to a database, you must grant access to the remote server.
Step 3: Access from Remote Clients
Now that the server is configured... use the steps below to allow remote clients to access the database...
Create New MariaDB? User
To create a new
MariaDB? user, type the following command:
CREATE USER 'user1'@localhost IDENTIFIED BY 'password1';
In this case, we use the ‘localhost' host-name and not the server's IP. This practice is commonplace if you plan to SSH in to your server, or when using the local client to connect to a local
MySQL? server.
Note: Substitute user1 and password1 with the credentials for the user you are creating.
Once you create user1, check its status by entering:
SELECT User FROM mysql.user;
The output lists all existing users.
example of listing existing users in mariadb
Grant Privileges to MariaDB? User
The newly created user does not have privileges to manage databases nor to access the
MariaDB? shell.
To grant all privileges to user1:
GRANT ALL PRIVILEGES ON *.* TO 'user1'@localhost IDENTIFIED BY 'password1';
The *.* in the statement refers to the database or table for which the user is given privileges. This specific command provides access to all databases located on the server. As this might be a major security issue, you should replace the symbol with the name of the database you are providing access to.
You can grant all privileges to an user from any host on a database by :
GRANT ALL PRIVILEGES ON 'yourdb'.* TO 'user1'@% IDENTIFIED BY 'password1';
You can grant all privileges to an user from only 172.20.10.196 host on a database by :
GRANT ALL PRIVILEGES ON 'yourdb'.* TO 'user1'@'172.20.10.196' IDENTIFIED BY 'password1';
To grant privileges only for yourDB, type the following statement:
GRANT ALL PRIVILEGES ON 'yourDB'.* TO 'user1'@localhost;
It's crucial to refresh the privileges once new ones have been awarded with the command:
The user you have created now has full privileges and access to the specified database and tables.
Once you have completed this step, you can verify the new user1 has the right permissions by using the following statement:
SHOW GRANTS FOR 'user1'@localhost;
The information provided by the system is displayed on the terminal.
The terminal displays the privileges grated to a specific user.
Remove MariaDB? User Account
If you need to remove a user, you can employ the DROP statement:
DROP USER 'user1'@localhost;
The output confirms that user1 no longer has access nor privileges.
After running the commands above, you should be able to access the server from the client computer with that assigned IP.
Conclusion
You have successfully created a
MariaDB? user and granted full user privileges. This basic task should quickly become a routine. There are numerous options to customize privileges and tailor them to your requirements.
We encourage you to explore the many available options that allow you to administer your databases securely and effectively.