Software >> OS >> Unix >> Linux >> RHEL >> 7 >> RHCE >> Section 11 - Database Services

 

Install and Configure MariaDB

 

### Setup the MariaDB server



## install the required packages

[root@rhel7server1 ~]# yum install -y mariadb-server mariadb


## edit the configuration if necessary

[root@rhel7server1 etc]# cd /etc

[root@rhel7server1 etc]# vi my.cnf


## e.g. to set the bind address for the mariadb server,

bind_address=<server-ip>


## enable and start the service

[root@rhel7server1 etc]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

[root@rhel7server1 etc]# systemctl start mariadb


## confirm the port is listening

[root@rhel7server1 etc]# netstat -an | grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN    


## open firewall ports

[root@rhel7server1 etc]# firewall-cmd --permanent --add-service=mysql
success


[root@rhel7server1 etc]# firewall-cmd --reload
success


## change the default root password for the DB

[root@rhel7server1 etc]# 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):
<Enter>   ## default password is blank
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] n     ## we want to test root login remotely later
 ... 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!


root@rhel7server1 etc]# systemctl restart mariadb


## test connect to the mariadb locally using the new root password

[root@rhel7server1 etc]# mysql -u root -p
Enter password: ********
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]>
show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
24 rows in set (0.00 sec)


## GRANT the required privileges for root user login from the client IP

MariaDB [(none)]> GRANT ALL on *.* TO root@'192.168.0.20' IDENTIFIED BY 'training';
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> quit
Bye

 

### Setup a MariaDB client


## install the client package


[root@rhel7client1 ~]# yum install -y mariadb


## test login to the MariaDB server

[root@rhel7client1 ~]# mysql -h rhel7server1 -u root -p
Enter password: ********
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> quit
Bye


 

back to Objectives

 

Create a simple database schema


## login as root

[root@rhel7server1 ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CREATE DATABASE emails;
Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> USE emails;
Database changed

MariaDB [emails]> CREATE TABLE users (ID INT, FirstName CHAR(25), LastName CHAR(25), Email CHAR(50));
Query OK, 0 rows affected (0.05 sec)

MariaDB [emails]> DESCRIBE users;
+-----------+----------+------+-----+---------+-------+
| Field     | Type     | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+-------+
| ID        | int(11)  | YES  |     | NULL    |       |
| FirstName | char(25) | YES  |     | NULL    |       |
| LastName  | char(25) | YES  |     | NULL    |       |
| Email     | char(50) | YES  |     | NULL    |       |
+-----------+----------+------+-----+---------+-------+
4 rows in set (0.00 sec)

MariaDB [emails]> quit
Bye


 back to Objectives

 

Perform simple SQL queries against a database


##

[root@rhel7server1 ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> USE emails;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

MariaDB [emails]> SHOW TABLES;
+------------------+
| Tables_in_emails |
+------------------+
| users            |
+------------------+
1 row in set (0.00 sec)

MariaDB [emails]> INSERT INTO users (ID,FirstName,LastName,Email) VALUES (1,"John","Doe","jd@example.com");
Query OK, 1 row affected (0.02 sec)

MariaDB [emails]> INSERT INTO users (ID,FirstName,LastName,Email) VALUES (2,"Jane","Jones","jj@example.com");
Query OK, 1 row affected (0.00 sec)

MariaDB [emails]> SELECT * FROM users;
+------+-----------+----------+----------------+
| ID   | FirstName | LastName | Email          |
+------+-----------+----------+----------------+
|    1 | John      | Doe      | jd@example.com |
|    2 | Jane      | Jones    | jj@example.com |
+------+-----------+----------+----------------+
2 rows in set (0.00 sec)

MariaDB [emails]> UPDATE users SET LastName="Smith",Email="js@example.com" WHERE ID=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [emails]> SELECT * from users;
+------+-----------+----------+----------------+
| ID   | FirstName | LastName | Email          |
+------+-----------+----------+----------------+
|    1 | John      | Doe      | jd@example.com |
|    2 | Jane      | Smith    | js@example.com |
+------+-----------+----------+----------------+
2 rows in set (0.00 sec)

MariaDB [emails]> QUIT
Bye




 back to Objectives

 

 

Backup and Restore a database


## login to MariaDB and list the databases

[root@rhel7server1 ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| emails             |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.01 sec)

MariaDB [(none)]> QUIT
Bye

### Backup database(s)


## backup a single database

[root@rhel7server1 ~]# mysqldump -u root -p emails > /var/tmp/emails-dbbackup.sql
Enter password:

[root@rhel7server1 ~]# head -5 /var/tmp/emails-dbbackup.sql
-- MySQL dump 10.14  Distrib 5.5.65-MariaDB, for Linux (x86_64)
--
-- Host: localhost    Database: emails
-- ------------------------------------------------------
-- Server version    5.5.65-MariaDB



## backup multiple databases

[root@rhel7server1 ~]# mysqldump -u root -p --databases emails mysql > /var/tmp/multi-dbbackup.sql
Enter password:

[root@rhel7server1 ~]# grep 'Current.Database[:]' /var/tmp/multi-dbbackup.sql
-- Current Database: `emails`
-- Current Database: `mysql`



## backup all databases

[root@rhel7server1 ~]# mysqldump -u root -p --all-databases > /var/tmp/all-dbbackup.sql
Enter password:

### Restore a database


[root@rhel7server1 ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 15
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| emails             |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)


MariaDB [(none)]> DROP DATABASE emails;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> QUIT
Bye

## test restore without creating the databases first
## the restore will fail


[root@rhel7server1 ~]# mysql -u root -p emails  < /var/tmp/emails-dbbackup.sql
Enter password:
ERROR 1049 (42000): Unknown database 'emails'


## create the (empty) DB first

[root@rhel7server1 ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 21
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CREATE DATABASE emails;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> QUIT
Bye


## then re-run the restore

[root@rhel7server1 ~]# mysql -u root -p emails  < /var/tmp/emails-dbbackup.sql
Enter password:


## verify that the DB was restored

[root@rhel7server1 ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 23
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> USE emails;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

MariaDB [emails]> SHOW TABLES;
+------------------+
| Tables_in_emails |
+------------------+
| users            |
+------------------+
1 row in set (0.00 sec)

MariaDB [emails]> QUIT
Bye




back to Objectives