mysql master-slave replication, add new slave server

Keywords: Linux MariaDB MySQL CentOS vim

Master-slave replication, adding slave servers
Three mysql servers. One is the master and one is the slave, realizing automatic backup. Add a new slave server
System: CentOS 7.6
mariadb: 5.5.60

Primary server:

[root@Centos7 ~]#vim /etc/my.cnf
[mysqld]
server_id=7     #Set server ID 
log_bin         #Open binary log

Start mysql, View binary location information, and create users that the slave server can use

[root@Centos7 ~]#systemctl start mariadb
[root@Centos7 ~]#mysql
MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |      245 |
+--------------------+-----------+
1 row in set (0.00 sec)
MariaDB [(none)]> grant replication slave on *.* to repluser@'192.168.37.%' identified by 'centos';
Query OK, 0 rows affected (0.01 sec)

Master server setup complete

from server

[root@Centos7 ~]#vim /etc/my.cnf

[mysqld]
server_id=17   #Set server ID
read_only      #Recommended from server, read only

Configure synchronization information: use the user provided by the master service to copy the data from the master server to the local machine

[root@Centos7 ~]#mysql
.
.
.
MariaDB [(none)]> CHANGE MASTER TO   
    -> MASTER_HOST='192.168.37.7',           #Primary server IP
    -> MASTER_USER='repluser',               #user
    -> MASTER_PASSWORD='centos',             #Password
    -> MASTER_PORT=3306,                     #port
    -> MASTER_LOG_FILE='mariadb-bin.000001', #Binary
    -> MASTER_LOG_POS=245;                   #Where to start copying
Query OK, 0 rows affected (0.03 sec)

MariaDB [(none)]> start slave;               #Start slave thread
MariaDB [(none)]> show slave status\G        #View state
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.37.7  #Primary server IP
                  Master_User: repluser      #Users used
                  Master_Port: 3306          #Use port
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 400
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 686
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: Yes            #Synchronous success
            Slave_SQL_Running: Yes            #Synchronous success
        .
        .
        .
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 7             #Primary server ID
1 row in set (0.00 sec)
.
.
.

Test: add, delete and change data on the master server, and automatically synchronize data on the slave server

Add a slave server to the primary server with existing data

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mysql              |
| performance_schema |
| testdb1            |
| testdb2            |
| testdb3            |
+--------------------+
7 rows in set (0.00 sec)

Fully back up the data of the primary server, view the logging location of the primary server, and deliver it to the new secondary server

[root@Centos7 ~]#mysqldump -A --single-transaction -F --master-data=1 > /data/backup/all.sql
[root@Centos7 ~]#scp -r /data/backup/* 192.168.37.27:/data/
[root@Centos7 ~]#mysql
.
.
.
MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |      8436 |
| mariadb-bin.000002 |       245 |   #Sync from the latest location,
+--------------------+-----------+

New slave settings

[root@Centos7 ~]#vim /etc/my.cnf
[mysqld]
server_id=37
read_only
.
.
.

Set the backup file delivered by the primary server, plus the synchronization information of the primary service

[root@Centos7 ~]#vim /data/all.sql
.
.
.
CHANGE MASTER TO   
MASTER_HOST='192.168.36.7',
MASTER_USER='repluser',
MASTER_PASSWORD='centos',
MASTER_PORT=3306,
MASTER_LOG_FILE='mariadb-bin.000002', 
MASTER_LOG_POS=245; 
.
.
.

Start the MySQL service, import the backed up data, enter mysql, and start the slave thread

[root@Centos7 ~]#systemctl start mariadb
[root@Centos7 ~]#mysql < /data/all.sql 
[root@Centos7 ~]#mysql
MariaDB [(none)]> start slave;  #Open thread
Query OK, 0 rows affected (0.02 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.37.7
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000002
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 531
        Relay_Master_Log_File: mariadb-bin.000002
             Slave_IO_Running: Yes   #Success
            Slave_SQL_Running: Yes   #Success
                            .
                            .
                            .
             Master_Server_Id: 7
1 row in set (0.00 sec)

Test to add, delete and modify data on the main server to see if it can be automatically synchronized

Posted by imcomguy on Fri, 15 Nov 2019 07:20:30 -0800