实验:MySQL实现级联复制

1 实现级联复制

1.1 实验环境

  • 三台主机实现级联复制
    • 在192.168.66.8充当master
      在192.168.66.18充当级联midslave
      在192.168.66.28充当slave
    • 主机系统为CentOS8
    • 需要干净的系统,关闭防火墙,selinux=disabled
    • dnf仓库需自己创建
      注意:此实验采用dnf 安装的mariadb-server

1.2 节点配置

1.2.1 主节点master配置

dnf install mariadb-server
systemctl start mariadb
mysql < /data/helldb.sql
#在master上实现
vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=8  # 个人喜好, 8对应的是master的ip最后一位
log-bin=/data/mysql/logbin/mariadb-bin # 二进制文件存放位置
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
#保存退出
systemctl restart mariadb
# mysql
MariaDB [(none)]> grant replication slave on *.* to repluser@'192.168.66.%' identified by 'ming';
Query OK, 0 rows affected (0.013 sec)

MariaDB [(none)]> select user,host,password from mysql.user;
+----------+--------------+-------------------------------------------+
| user     | host         | password                                  |
+----------+--------------+-------------------------------------------+
| root     | localhost    |                                           |
| root     | master       |                                           |
| root     | 127.0.0.1    |                                           |
| root     | ::1          |                                           |
| repluser | 192.168.66.% | *C6B2B8EC628390A22B99AB0609E5E70A0F962E9F |
+----------+--------------+-------------------------------------------+
5 rows in set (0.001 sec)

MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |       529 |
+--------------------+-----------+
1 row in set (0.000 sec)

MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |       578 |
| mariadb-bin.000002 |       389 |
+--------------------+-----------+




  • 完全备份
  • +注意:
    • 因创建了复制权限的用户账号,需做完全备份
[root@centos8 ~]# mysqldump -A -F --single-transaction --master-data=1 > /data/backup.sql
[root@centos8 ~]# scp /data/backup.sql 192.168.66.18:/data
[root@centos8 ~]# scp /data/backup.sql 192.168.66.28:/data

1.2.2 中间级联slave配置

dnf install mariadb-server
systemctl start mariadb
  • 在中间级联slave实现
[root@midslave ~]#vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=18
log-bin
log_slave_updates **#级联复制中间节点的必选项**
# [root@midslave ~]#systemctl restart mariadb
# [root@midslave ~]# vim /data/backup.sql
CHANGE MASTER TO
MASTER_HOST='192.168.66.8',
MASTER_USER='repluser',
MASTER_PASSWORD='ming',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=389;
 #还原数据库
[root@midslave ~]# mysql
MariaDB [(none)]> set sql_log_bin=0;
MariaDB [(none)]> source /data/all.sql
MariaDB [mysql]> show master logs; #记录二进制位置,给第三个节点使用
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |    483168 |
| mariadb-bin.000002 |       389 | #
+--------------------+-----------+
2 rows in set (0.000 sec)


MariaDB [(none)]> set sql_log_bin=0;
MariaDB [(none)]> start slave;
MariaDB [mysql]> show slave status\G;
*************************** 1. row ***************************
                Slave_IO_State: Waiting for master to send event
                   Master_Host: 192.168.66.8
                   Master_User: repluser
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mariadb-bin.000002
           Read_Master_Log_Pos: 389
                Relay_Log_File: mariadb-relay-bin.000003
                 Relay_Log_Pos: 557
         Relay_Master_Log_File: mariadb-bin.000002
              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: 389
               Relay_Log_Space: 1179
               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: 8
                Master_SSL_Crl: 
            Master_SSL_Crlpath: 
                    Using_Gtid: No
                   Gtid_IO_Pos: 
       Replicate_Do_Domain_Ids: 
   Replicate_Ignore_Domain_Ids: 
                 Parallel_Mode: conservative
                     SQL_Delay: 0
           SQL_Remaining_Delay: NULL
       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
              Slave_DDL_Groups: 0
Slave_Non_Transactional_Groups: 0
    Slave_Transactional_Groups: 0
1 row in set (0.001 sec)


1.2.3 在第三个节点slave配置

[root@slave ~]#dnf install mariadb-server  #
[root@slave ~]#systemctl start mariadb   #
 
[root@slave ~]#vim /etc/my.cnf.d/mariadb-server.cnf  #
[mysqld]
server-id=28
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
[root@centos8 ~]#systemctl restart mariadb   #
 
[root@slave ~]## vim /data/backup.sql 
CHANGE MASTER TO
MASTER_HOST='192.168.66.18',
MASTER_USER='repluser',
MASTER_PASSWORD='ming',
MASTER_PORT=3306,
MASTER_LOG_FILE='mariadb-bin.000002', MASTER_LOG_POS=389;


[root@centos8 ~]# mysql < /data/backup.sql
[root@centos8 ~]# mysql 
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
                Slave_IO_State: Connecting to master
                   Master_Host: 192.168.66.18
                   Master_User: repluser
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mariadb-bin.000002
           Read_Master_Log_Pos: 389
                Relay_Log_File: mariadb-relay-bin.000001
                 Relay_Log_Pos: 4
         Relay_Master_Log_File: mariadb-bin.000002
              Slave_IO_Running: Connecting
             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: 389
               Relay_Log_Space: 256
               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: 1130
                 Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 60  maximum-retries: 86400  message: Host '192.168.66.28' is not allowed to connect to this MariaDB server #错误了?为什么啊?
                Last_SQL_Errno: 0
                Last_SQL_Error: 
   Replicate_Ignore_Server_Ids: 
              Master_Server_Id: 0
                Master_SSL_Crl: 
            Master_SSL_Crlpath: 
                    Using_Gtid: No
                   Gtid_IO_Pos: 
       Replicate_Do_Domain_Ids: 
   Replicate_Ignore_Domain_Ids: 
                 Parallel_Mode: conservative
                     SQL_Delay: 0
           SQL_Remaining_Delay: NULL
       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
              Slave_DDL_Groups: 0
Slave_Non_Transactional_Groups: 0
    Slave_Transactional_Groups: 0
1 row in set (0.000 sec)


**解决办法**
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.008 sec)
MariaDB [(none)]> reset slave all;
Query OK, 0 rows affected (0.001 sec)
**#进入midslave**
root@midslave ~]# systemctl restart mariadb.service 
[root@midslave ~]# mysql
MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |    483168 |
| mariadb-bin.000002 |       438 |
| mariadb-bin.000003 |       412 |
| mariadb-bin.000004 |       344 |
+--------------------+-----------+
4 rows in set (0.000 sec)

**#进入slave**
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.008 sec)

MariaDB [(none)]> reset slave all;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> CHANGE MASTER TO 
MASTER_HOST='192.168.66.18', 
MASTER_USER='repluser', 
MASTER_PASSWORD='ming', 
MASTER_PORT=3306, 
MASTER_LOG_FILE='mariadb-bin.000004', MASTER_LOG_POS=344;
Query OK, 0 rows affected (0.013 sec)

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

MariaDB [(none)]> show slave status;
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
                Slave_IO_State: Waiting for master to send event
                   Master_Host: 192.168.66.18
                   Master_User: repluser
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mariadb-bin.000004
           Read_Master_Log_Pos: 344
                Relay_Log_File: mariadb-relay-bin.000002
                 Relay_Log_Pos: 557
         Relay_Master_Log_File: mariadb-bin.000004
              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: 344
               Relay_Log_Space: 868
               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: 18
                Master_SSL_Crl: 
            Master_SSL_Crlpath: 
                    Using_Gtid: No
                   Gtid_IO_Pos: 
       Replicate_Do_Domain_Ids: 
   Replicate_Ignore_Domain_Ids: 
                 Parallel_Mode: conservative
                     SQL_Delay: 0
           SQL_Remaining_Delay: NULL
       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
              Slave_DDL_Groups: 0
Slave_Non_Transactional_Groups: 0
    Slave_Transactional_Groups: 0
1 row in set (0.000 sec)


1.3 测试

MariaDB [(none)]> create database db1;
[root@master ~]# mysql < /data/hellodb_innodb.sql 
Query OK, 1 row affected (0.001 sec)

MariaDB [mysql]> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| hellodb            |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
5 rows in set (0.001 sec)


  • 中从
[root@midslave ~]# mysql
MariaDB [mysql]> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| hellodb            |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
5 rows in set (0.001 sec)


[root@slave ~]# mysql
MariaDB [mysql]> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| hellodb            |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
5 rows in set (0.001 sec)

发布了39 篇原创文章 · 获赞 2 · 访问量 1037

猜你喜欢

转载自blog.csdn.net/weixin_45341507/article/details/103324169
今日推荐