1. Overview
The developers of MySQL database which is the most popular database management system left the company to found MariaDB Corporation Ab, MariaDB Foundation. They created MariaDB in 2009 and become one of the most popular open source database system management because some new features over MySQL with additional storage engines, and performance improvements.
In this article will describe how to install, configure, and manage MariaDB database system management on Debian 9 Stretch linux server.
2. Prerequisites
In this tutorial of how to install, configure, and manage MariaDB database system management on Debian 9 Stretch linux server, it is supposed that:
a. You have already install Debian 9 Linux server up and running. In case that you don’t, you would probably like to read this link to help you. Installing Debian OS With Logical Volume Manager (LVM)
b. You have already done the initial server setup. Please refer to this link for help. Debian 8.x Initial Server Configuration
3. System Architecture Diagram
We will set up the MariaDB database management system as show in the following diagram. For security and performance optimization purpose, we will need to separated the Web server and the Database server in two different servers. Web server with hostname “vkcent-web01” and IP address of 10.0.0.1 will connect to database on the remote MariaDB database server with hostname “vkcent-dbs01” and IP address of 10.0.0.2.
4. Install and Configure MariaDB Package
To install MaraiDB server, execute the following commands.
# apt-get -y install mariadb-server
Now we need to do basic security hardening on MariaDB server by executing mysql_secure_installation command to address several security concerns in a default MariaDB installation.
# mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n] Y New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success! By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] Y ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] Y ... Success! By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] Y - Dropping test database... ... Success! - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] Y ... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB!
5. Create Database
To create a database name “webdb01” and a user name “webdbuser01” and grant all privilege for user “webdbuser01” on the database name “webdb01”, execute the following command.
# mysql -u root -p> create database webdb01 charset utf8; > create user 'webdbuser01'@'localhost' identified by 'p@ssword123'; > grant all privileges on webdb01.* to 'webdbuser01'@'localhost' with grant option; > flush privileges;
If use those above commands to grant a user privileges for a database, we will not be able to connect MariaDB database system management from the remote Apache web server. To allow the Apache web server with IP address of 10.0.0.1 to access the MariaDB database system management server, when creating the databases we need to specify the IP address of Apache web server instead of localhost.
# mysql -u root -p > create database webdb01 charset utf8; > create user 'webdbuser01'@'10.0.0.1' identified by 'p@ssword123'; > grant all privileges on webdb01.* to 'webdbuser01'@'10.0.0.1' with grant option; > flush privileges;
By default, MariaDB database on Debian 9 only allow connection from localhost itself. To allow the remote Apache web server with IP address of 10.0.0.1 to access the MariaDB database system management server, we need to edit file “/etc/mysql/mariadb.conf.d/50-server.cnf” as the following.
# vim /etc/mysql/mariadb.conf.d/50-server.cnf bind-address = 0.0.0.0
Then, let restart the service of MariaDB database
# service mariadb restart # service mariadb status
5. Test Connecting to MariaDB Server
To connect to MariaDB database server (IP 10.0.0.2) remotely from Apache web server (IP 10.0.0.1) , we need to install MariaDB client as the following on Apache web server.
# apt-get -y install mariadb-client
Let test MariaDB connection with telnet command from Apache web server (IP 10.0.0.1) to MariaDB database server (IP 10.0.0.2) as the following.
# telnet 10.0.0.2 3306
Now let try to login to MariaDB database server from Apache web server with the username and password that just created (Username: webdbuser01, Password: p@sssword123). IP 10.0.0.2 is the MariaDB database server IP.
# mysql -u webdbuser01 -p -h 10.0.0.2 Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 6 Server version: 10.1.26-MariaDB-0+deb9u1 Debian 9.1 Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
If we execute the command show databases; , we will see only one database is listed because user “webdbuser01” is granted for this database only.
MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | webdb01 | +--------------------+ 2 rows in set (0.00 sec)
6. SQL Commands
If you want to know which host is allowed to connected to the MariaDB database server using which user, we can use the following SQL commands on MariaDB database server.
> select user,host,password from mysql.user; +-------------+-----------------+-------------------------------------------+ | user | host | password | +-------------+-----------------+-------------------------------------------+ | root | localhost | *DA03A40B1EA484798CC1E892642A3EA14D5B6341 | | webdbuser01 | 192.168.171.130 | *45DE09574DD7395CB49B9E7AE24CC93A18AED04D | +-------------+-----------------+-------------------------------------------+ 2 rows in set (0.00 sec)
In case that you don’t want to allow host with IP address of 10.0.0.1 to remote connect to MariaDB database server using account name “webdbuser01” any more, we can delete it using the following SQL commands.
> drop user webdbuser01@10.0.0.1;
To list all the available databases, execute the following SQL commands.
> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | webdb01 | +--------------------+ 4 rows in set (0.01 sec)
To create a table name “tbname” in a database name “webdb01”, we can execute the following SQL commands.
> use webdb01; > create table tbname (id int(1),name varchar(50));
To list the current table in the datbase name “tbname”, execute the following SQL command.
MariaDB [webdb01]> show tables; +-------------------+ | Tables_in_webdb01 | +-------------------+ | tbname | +-------------------+ 1 row in set (0.00 sec)
To insert values into the table name “tbname”, execute the following SQL command.
MariaDB [webdb01]> insert into tbname (id,name) values (1,'Vannath'); Query OK, 1 row affected (0.00 sec)
To list the current values in the table name “tbname”, execute the following SQL command.
MariaDB [webdb01]> select * from tbname; +------+---------+ | id | name | +------+---------+ | 1 | Vannath | +------+---------+ 1 row in set (0.00 sec)
To update the value in the table name “tbname”, execute the following SQL command.
MariaDB [webdb01]> update tbname set name='Solida' where id=1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [webdb01]> select * from tbname; +------+--------+ | id | name | +------+--------+ | 1 | Solida | +------+--------+ 1 row in set (0.00 sec)
To delete the value from the table name “tbname”.
MariaDB [webdb01]> delete from tbname where id=1;
To delete the name database name “webdb01”, execute the following SQL command.
MariaDB [webdb01]> drop database webdb01;
7. Backup Database
If you are looking for way to backup MariaDB database system management, you can use mysqldump command to backup. In the following, we will backup a MariaDB database name “webdb01”.
# mysqldump -h localhost -u root -p webdb01 | gzip -9 > /var/backups/webdb01-2018030201.sql.gz
To extract the .gz file, use the command below.
# cd /var/backups/ # ll --rw-r--r-- 1 root root 463 Mar 2 23:05 webdb01-2018030201.sql.gz # gunzip webdb01-2018030201.sql.gz # ll -rw-r--r-- 1 root root 1289 Mar 2 23:05 webdb01-2018030201.sql
8. Restore Database
Here is the current value in database table.
> select * from tbname; +------+---------+ | id | name | +------+---------+ | 1 | Vannath | | 2 | Solida | +------+---------+ 2 rows in set (0.00 sec)
Now, let try to delete one value from the table name “tbname”.
> delete from tbname where id=1; Query OK, 1 row affected (0.00 sec) > select * from tbname; +------+--------+ | id | name | +------+--------+ | 2 | Solida | +------+--------+ 1 row in set (0.00 sec)
To restore the MariaDB from the backup, execute the following command.
# mysql -u root -p webdb01 < /var/backups/webdb01-2018030201.sql
Now, need to verify if the restore is successful.
# mysql -u root -p > use webdb01; > select * from tbname; +------+---------+ | id | name | +------+---------+ | 1 | Vannath | | 2 | Solida | +------+---------+ 2 rows in set (0.00 sec)
7. Conclusion
That’s all about how to install, configure, and manage MariaDB database system management on Debian 9 Stretch linux server from Tech Space KH. Hopefully, you can find this guide informative. If you have any questions or suggestions you can always leave your comments below. I will try all of my best to review and reply them.