CentOS 7 MariaDB Build Master-Slave Server

Keywords: Database MariaDB MySQL DNS

This article is written in CentOS 7. Ensure that SELinux is closed, firewalls are closed, or specified ports are prevented from being opened. Detailed information is as follows

#master
[root@promote ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@promote ~]# 
[root@promote ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.216.135  netmask 255.255.255.0  broadcast 192.168.216.255
        inet6 fe80::ccc2:d1b:1fc4:8ce2  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:7e:c3:24  txqueuelen 1000  (Ethernet)
        RX packets 913  bytes 70384 (68.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 438  bytes 47706 (46.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 78  bytes 3900 (3.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 78  bytes 3900 (3.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

#slave
[root@promote ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@promote ~]# 
[root@promote ~]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.216.136  netmask 255.255.255.0  broadcast 192.168.216.255
        inet6 fe80::9a13:471b:dee2:4e27  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::ccc2:d1b:1fc4:8ce2  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::c354:a1e1:869f:7ae1  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:36:5d:96  txqueuelen 1000  (Ethernet)
        RX packets 66317  bytes 89492193 (85.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 22069  bytes 1979972 (1.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@promote ~]# 

To ensure that the test goes smoothly, close SELinux.

The following will demonstrate that the master-slave server installs MariaDB Server, starts the database, and adds boot boot.

#Installation software
[root@promote ~]# yum install mariadb mariadb-devel mariadb-server -y
#Startup service
[root@promote ~]# systemctl start mariadb
//View Service Running Status
[root@promote ~]# systemctl status mariadb
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
   Active: active (running) since One 2019-03-25 22:08:41 CST; 4s ago
  Process: 7647 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
  Process: 7616 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
 Main PID: 7646 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─7646 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─7808 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --p...

3 Month 2522:08:38 promote.cache-dns.local systemd[1]: Starting MariaDB database server...
3 Month 2522:08:38 promote.cache-dns.local mariadb-prepare-db-dir[7616]: Database MariaDB is probably initialized in /var/lib/mysql already, nothing is done.
3 Month 2522:08:38 promote.cache-dns.local mysqld_safe[7646]: 190325 22:08:38 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
3 Month 2522:08:38 promote.cache-dns.local mysqld_safe[7646]: 190325 22:08:38 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
3 Month 2522:08:41 promote.cache-dns.local systemd[1]: Started MariaDB database server.
#StartUp Actions Manager
[root@promote ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@promote ~]# 

Modify master configuration file. Vim/etc/my.cnf. server_id=1 (not identical). After the master and slave server modify the configuration file and save it, restart the database service.

//Configuration file additions 
//server_id cannot be the same
server_id=1 //[Must] Server unique ID, default is 1, generally take the last IP segment
log-bin=mysql-bin //[Must] Enable binary logging

#master configuration file
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
server_id=1
log-bin=mysql-bin
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

#slave configuration file
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
server_id=10
log-bin=mysql-bin
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
                                                                                                                 

The master server executes the following commands.

#Log in to the database
[root@promote ~]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 222
Server version: 5.5.60-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.

#Allow root user to log in at will
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

#View master status
MariaDB [(none)]> 
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      387 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> 

The slave server executes commands and views status.

#Log on to slave server
[root@promote ~]# 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.60-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.
#Configure master information to see master_log_file and master_log_pos above
#master_log_file='mysql-bin.000001',master_log_pos=387; 
MariaDB [(none)]> change master to master_host='192.168.216.135',master_user='root',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=387; 
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> slave start;
Query OK, 0 rows affected (0.00 sec)
#Partial Option Annotations
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master 	#IO connection thread status
                  Master_Host: 192.168.216.135 			#master server address
                  Master_User: root 					#master user name
                  Master_Port: 3306						#Listen for master port information
                Connect_Retry: 60						#Connection Failure Retry Connection Time
              Master_Log_File: mysql-bin.000001			#master server binary log file name
          Read_Master_Log_Pos: 387						#master	Position id
               Relay_Log_File: mariadb-relay-bin.000001	#slave reads and executes the relay file location
                Relay_Log_Pos: 4						#slave reads and executes the relay file location
        Relay_Master_Log_File: mysql-bin.000001			#
             Slave_IO_Running: Connecting				#Does the IO thread connect to the master server status [Important Options]
            Slave_SQL_Running: Yes						#Startup of SQL Threads [Important Options]
              Replicate_Do_DB: 							#Synchronized database name
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 387
              Relay_Log_Space: 245
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 2003
                Last_IO_Error: error connecting to master 'root@192.168.216.135:3306' - retry-time: 60  retries: 86400  message: Can't connect to MySQL server on '192.168.216.135' (113)
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
1 row in set (0.00 sec)

MariaDB [(none)]> 

Check that Slave_IO_Running and Slave_SQL_Running are both Yes.

Query server_id results.

#master server_id
MariaDB [db]> SHOW VARIABLES LIKE 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 1     |
+---------------+-------+
1 row in set (0.00 sec)

MariaDB [db]> 

#slave server_id

MariaDB [db]> SHOW VARIABLES LIKE 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 10    |
+---------------+-------+
1 row in set (0.01 sec)

MariaDB [db]> 

The master creates a simple database and tables, inserts several pieces of data, and the slave server queries the database and tables.

#master database operation

[root@promote ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-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 master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      245 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 1     |
+---------------+-------+
1 row in set (0.00 sec)

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

MariaDB [(none)]> create database db; 
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> use db
Database changed
MariaDB [db]> create table test(id int,name text);
Query OK, 0 rows affected (0.00 sec)

MariaDB [db]> insert into test values(1,"bill"),(2,"tom"); 
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [db]> 


#slave database operation

[root@promote ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.60-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)]> change master to master_host='192.168.216.135',master_user='root',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=245; 
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.216.135
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 245
              Relay_Log_Space: 825
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
1 row in set (0.00 sec)

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

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

MariaDB [(none)]> use db
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 [db]> show tables;
+--------------+
| Tables_in_db |
+--------------+
| test         |
+--------------+
1 row in set (0.00 sec)

MariaDB [db]> select * from test;
+------+------+
| id   | name |
+------+------+
|    1 | bill |
|    2 | tom  |
+------+------+
2 rows in set (0.00 sec)

MariaDB [db]> 

Find mariadb software installation in addition.

[root@promote ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
mariadb-5.5.60-1.el7_5.x86_64
mariadb-server-5.5.60-1.el7_5.x86_64
mariadb-devel-5.5.60-1.el7_5.x86_64

Posted by brett on Tue, 26 Mar 2019 06:00:29 -0700