1. Overview
MariaDB replaced MySQL with many places such as additional storage engines, new features, and performance improvements. It is a new popular database management system used for web and server applications.
In this instruction will introduce how to install, configure, and manage MariaDB database program on CentOS 7 or RHEL 7 server.
2. Prerequisites
In this tutorial, it is supposed that:
a. You have already install RHEL/CentOS 7 Linux server up and running. In case that you don’t, you would probably like to read this link. Minimal RHEL/CentOS 7 Installation With Logical Volume Manager (LVM).
b. You have already done the initial server setup. Please refer to this link Minimal RHEL/CentOS 7 Initial Server Setup.
3. System Architecture Diagram
We will set up the MariaDB 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.
# yum -y install mariadb-server
After finish the installation, we need to enable MariaDB to start on boot and then start its service.
# systemctl enable mariadb
# systemctl start mariadb
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 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 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;
We also need to open firewall to make sure that port 3306 is open on the MariaDB server.
# vim /etc/sysconfig/iptables
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
# systemctl restart iptables
# iptables -L -v -n
5. Test Connecting to MariaDB Server
To connect to MariaDB server, we need to install MariaDB client as the following on Apache web server.
# yum -y install mariadb
Test MariaDB connection with telnet command 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 13
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, 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.
> 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.
> select user,host,password from mysql.user;
+-------------+----------------+-------------------------------------------+
| user | host | password |
+-------------+----------------+-------------------------------------------+
| root | localhost | *A1F1CB851D62F002C09A0C9C4A76262473432F55 |
| root | 127.0.0.1 | *A1F1CB851D62F002C09A0C9C4A76262473432F55 |
| root | ::1 | *A1F1CB851D62F002C09A0C9C4A76262473432F55 |
| glpiuser | localhost | *A1F1CB851D62F002C09A0C9C4A76262473432F55 |
| webdbuser01 | 10.0.0.1 | *45DE09574DD7395CB49B9E7AE24CC93A18AED04D |
+-------------+----------------+-------------------------------------------+
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 | | glpidb | | mysql | | performance_schema | | webdb01 | +--------------------+ 5 rows in set (0.00 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.
> show tables;
+-------------------+
| Tables_in_webdb01 |
+-------------------+
| tbname |
+-------------------+
1 row in set (0.01 sec)
To insert values into the table name “tbname”, execute the following SQL command.
> insert into tbname (id,name) values (1,'Vannath');
To list the current values in the table name “tbname”, execute the following SQL command.
> 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.
> update tbname set name='Solida' where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
> select * from tbname;
+------+--------+
| id | name |
+------+--------+
| 1 | Solida |
+------+--------+
1 row in set (0.00 sec)
To delete the value from the table name “tbname”.
> delete from tbname where id=1;
To delete the name database name “webdb01”, execute the following SQL command.
> drop database webdb01;
7. Backup Database
If you are looking for way to backup MariaDB database, 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 > /var/www/webdb01-2017061401.sql
If your database size is big, you might want to compress it, we can use the mysqldump command and pipe the output to gzip. Then, you will get the output as gzip file.
# mysqldump -h localhost -u root -p webdb01 | gzip -9 > /var/www/webdb01-2017061401.sql.gz
To extract the .gz file, use the command below.
# ll -rw-r--r-- 1 root root 686 Jun 14 21:27 webdb01-2017061401.sql.gz # gunzip webdb01-2017061401.sql.gz # ll -rw-r--r-- 1 root root 1865 Jun 14 21:27 webdb01-2017061401.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/www/webdb01-2017061401.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 installing and configuring MariaDB server in RHEL/ CentOS 7 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.