MySQL系列之十三 MySQL的复制

目录
  • 一、mysql复制相关概念
          • 七、mysql复制的相关指令和变量总结

            一、mysql复制相关概念

            • 主从复制:主节点将数据同步到多个从节点
            • 级联复制:主节点将数据同步到一个从节点,其他的从节点在向从节点复制数据
            • 同步复制:将数据从主节点全部同步到从节点时才返回给用户的复制策略叫同步复制
            • 异步复制:只要数据写入到主节点就立即返回给用户同步完成
            • 读写分离:在前端加一个调度器,负责将改变数据的语句和查询数据的语句分开调度,把写操作调度到主节点,读操作调度到从节点

            主节点:

            • dump thread:为每个slave的i/o thread启动一个dump线程,用于向其发送binary log events

            从节点:

            • i/o thread:向master请求二进制日志事件,并保存于中继日志中
            • sql thread:从中继日志中读取日志事件,在本地完成重放

            跟复制功能相关的文件:

            • master.info:用于保存slave连接至master时的相关信息,例如账号、密码、服务器地址等
            • relay-log.info:保存在当前slave节点上已经复制的当前二进制日志和本地replay log日志的对应关系

            复制架构:

            • 一主一从
            • 一主多从
            • 主主复制
            • 环状复制
            • 级联复制
            • 多主一从

            常见的架构有主从架构或者级联架构

            二、简单的一主一从架构实现

            1、新数据库搭建主从架构

            ​1)主服务器配置

            ~]# vim /etc/my.cnf
                [mysqld]
                log_bin
                binlog_format=row
                log-basename=master1
                server_id=1
            ~]# systemctl restart mariadb
            ~]# mysql
            mariadb [(none)]> grant replication slave on *.* to testuser@'192.168.0.8' identified by 'testpass';  #授权同步账户
            mariadb [(none)]> show master logs;
            +--------------------+-----------+
            | log_name           | file_size |
            +--------------------+-----------+
            | master1-bin.000001 |     26756 |
            | master1-bin.000002 |    921736 |
            | master1-bin.000003 |       401 |  #记录此位置,从服务器从这里开始同步
            +--------------------+-----------+

            ​2)从服务器配置

            ~]# vim /etc/my.cnf
                [mysqld]
                server_id=2  #服务器id唯一
                relay_log=relay-log
                relay_log_index=relay-log.index
                read_only=on
            ~]# systemctl restart mariadb
            ~]# mysql
            mariadb [(none)]> change master to
                -> master_host='192.168.0.7',  #指定主节点ip
                -> master_user='testuser',  #同步用户的用户名
                -> master_password='testpass',  #密码
                -> master_port=3306,
                -> master_log_file='master1-bin.000003',  #以上记录的文件
                -> master_log_pos=401,  #位置
                -> master_connect_retry=10;  #重试时间10秒
            mariadb [(none)]> start slave;  #开始主从复制

            ​3)测试

            在主节点上生成一些数据:
            mariadb [(none)]> create database testdb;
            mariadb [(none)]> use testdb
            mariadb [testdb]> create table testlog (id int auto_increment primary key,name char(30),age int default 20);
            mariadb [testdb]> delimiter $$
            mariadb [testdb]> create procedure pro_testlog()
                -> begin
                -> declare i int;
                -> set i = 1;
                -> while i < 100000
                -> do insert into testlog(name,age) values (concat('testuser',i),i);
                -> set i = i +1; 
                -> end while;
                -> end$$
            mariadb [testdb]> delimiter ;
            mariadb [testdb]> start transaction;
            mariadb [testdb]> call pro_testlog;
            mariadb [testdb]> commit;
            在从节点上查看同步情况:
            mariadb [(none)]> select count(*) from testdb.testlog;
            +----------+
            | count(*) |
            +----------+
            |    99999 |  #同步成功
            +----------+
            mariadb [(none)]> show slave status\g
            *************************** 1. row ****************************
                           slave_io_state: waiting for master to send event
                              master_host: 192.168.0.7
                              master_user: testuser
                              master_port: 3306
                            connect_retry: 10
                          master_log_file: master1-bin.000003
                      read_master_log_pos: 10389814
                           relay_log_file: relay-log.000002
                            relay_log_pos: 10389944
                    relay_master_log_file: master1-bin.000003
                         slave_io_running: yes  #io线程已启动
                        slave_sql_running: yes  #sql线程已启动
                    seconds_behind_master: 0    #主从复制的时间差
                         master_server_id: 1

            2、旧数据库新加从服务器

            ​1)主服务器配置

            ~]# vim /etc/my.cnf
                [mysqld]
                log_bin
                binlog_format=row
                log-basename=master1
                server_id=1
            ~]# systemctl restart mariadb
            ~]# mysqldump -a -f --single-transaction --master-data=1 > full.sql
            ~]# scp full.sql root@192.168.0.8:/root/
            ~]# mysql -e 'grant replication slave on *.* to testuser@'192.168.0.8' identified by 'testpass';'

            ​2)从服务器配置

            ~]# vim /etc/my.cnf
                [mysqld]
                server_id=2
                relay_log=relay-log   
                relay_log_index=relay-log.index
                read_only=on
            ~]# systemctl restart mariadb
            ~]# vim full.sql  #在备份的sql文件中加入以下信息
                change master to
                master_host='192.168.0.7',
                master_user='testuser',
                master_password='testpass',
                master_port=3306,
                master_log_file='master1-bin.000005',
                master_log_pos=245,
                master_connect_retry=10;
            ~]# mysql < full.sql  #导入sql的同时配置已经完成
            mariadb [(none)]> select count(*) from testdb.testlog;
            +----------+
            | count(*) |
            +----------+
            |    99999 |
            +----------+
            mariadb [(none)]> start slave;  #启动复制

            三、级联复制架构实现

            1)主节点

            [root@master ~]# vim /etc/my.cnf
            [mysqld]
            log_bin
            binlog_format=row
            log-basename=master
            server_id=1
            [root@master ~]# systemctl restart mariadb
            mariadb [(none)]> grant replication slave on *.* to repluser@'192.168.0.%' identified by 'replpass';
            mariadb [(none)]> show master logs;
            +-------------------+-----------+
            | log_name          | file_size |
            +-------------------+-----------+
            | master-bin.000001 |     26753 |
            | master-bin.000002 |    921736 |
            | master-bin.000003 |       401 |
            +-------------------+-----------+

            2)从节点

            [root@slave1 ~]# vim /etc/my.cnf
            [mysqld]
            log_bin  #注意,级联架构中中继从节点一定得开二进制日志功能
            binlog_format=row
            read_only=on
            server_id=2
            log_slave_updates  #这项为关键,作用是将从服务的数据改变记录到二进制日志文件中
            relay_log=relay-log
            relay_log_index=relay-log.index
            [root@slave1 ~]# systemctl start mariadb
            mariadb [(none)]> change master to
                -> master_host='192.168.0.7',
                -> master_user='repluser',
                -> master_password='replpass',
                -> master_port=3306,
                -> master_log_file='master-bin.000003',
                -> master_log_pos=401,
                -> master_connect_retry=10;
            mariadb [(none)]> start slave;
            mariadb [(none)]> show master logs;
            +--------------------+-----------+
            | log_name           | file_size |
            +--------------------+-----------+
            | mariadb-bin.000001 |       245 |
            +--------------------+-----------+
            mariadb [(none)]> grant replication slave on *.* to repluser@'192.168.0.%' identified by 'replpass';

            3)从节点的从节点

            [root@slave2 ~]# vim /etc/my.cnf
            [mysqld]
            read_only=on
            server_id=3
            relay_log=relay-log
            relay_log_index=relay-log.index
            [root@slave2 ~]# systemctl start mariadb
            mariadb [(none)]> change master to
                -> master_host='192.168.0.8',
                -> master_user='repluser',
                -> master_password='replpass',
                -> master_port=3306,
                -> master_log_file='mariadb-bin.000001',
                -> master_log_pos=245,
                -> master_connect_retry=10;
            mariadb [(none)]> start slave;

            4)从节点的从节点2

            [root@slave3 ~]# vim /etc/my.cnf
            [mysqld]
            read_only=on
            server_id=4
            relay_log=relay-log
            relay_log_index=relay-log.index
            [root@slave3 ~]# systemctl start mariadb
            mariadb [(none)]> change master to
                -> master_host='192.168.0.8',
                -> master_user='repluser',
                -> master_password='replpass',
                -> master_port=3306,
                -> master_log_file='mariadb-bin.000001',
                -> master_log_pos=245,
                -> master_connect_retry=10;
            mariadb [(none)]> start slave;
            到此已经搭建好了级联复制,接下来测试一下把~

            四、主主复制架构

            容易产生的问题:数据不一致,因此慎用;考虑要点:自动增长id
            配置一个节点使用奇数id
            auto_increment_offset=1 开始点
            auto_increment_increment=2 增长幅度
            另一个节点使用偶数id
            auto_increment_offset=2
            auto_increment_increment=2

            1)主1

            [mysqld]
            log_bin
            binlog_format=row
            log-basename=master1
            server_id=1
            relay_log=relay-log
            relay_log_index=relay-log.index
            auto_increment_offset=1  #自增长字段从1开始
            auto_increment_increment=2  #每次增长2,也就是说master1节点写入的数据的id字段全部是奇数
            [root@master ~]# systemctl start mariadb
            mariadb [(none)]> show master logs;
            +--------------------+-----------+
            | log_name           | file_size |
            +--------------------+-----------+
            | master1-bin.000001 |     27033 |
            | master1-bin.000002 |    942126 |
            | master1-bin.000003 |       245 |
            +--------------------+-----------+
            mariadb [(none)]> grant replication slave on *.* to repluser@'192.168.0.%' identified by 'replpass';
            mariadb [(none)]> change master to
                -> master_host='192.168.0.8',
                -> master_user='repluser',
                -> master_password='replpass',
                -> master_port=3306,
                -> master_log_file='master2-bin.000003',
                -> master_log_pos=245,
                -> master_connect_retry=10;
            mariadb [(none)]> start slave;

            2)主2

            [mysqld]
            log_bin
            binlog_format=row
            log-basename=master2
            server_id=2
            relay_log=relay-log
            relay_log_index=relay-log.index
            auto_increment_offset=2  #自增长字段从1开始
            auto_increment_increment=2  #每次增长2,也就是说master1节点写入的数据的id字段全部是偶数
            [root@master2 ~]# systemctl start mariadb
            mariadb [(none)]> show master logs;
            +--------------------+-----------+
            | log_name           | file_size |
            +--------------------+-----------+
            | master2-bin.000001 |     27036 |
            | master2-bin.000002 |    942126 |
            | master2-bin.000003 |       245 |
            +--------------------+-----------+
            mariadb [(none)]> grant replication slave on *.* to repluser@'192.168.0.%' identified by 'replpass';
            mariadb [(none)]> change master to
                -> master_host='192.168.0.7',
                -> master_user='repluser',
                -> master_password='replpass',
                -> master_port=3306,
                -> master_log_file='master1-bin.000003',
                -> master_log_pos=245,
                -> master_connect_retry=10;
            mariadb [(none)]> start slave;

            3)测试

            在master1上创建表,增加数据
            mariadb [(none)]> create database db1;
            mariadb [(none)]> use db1
            mariadb [db1]> create table t1(id int(2) auto_increment primary key,name char(30));
            mariadb [db1]> insert t1(name) values ('tom');
            mariadb [db1]> insert t1(name) values ('maria'); 
            mariadb [db1]> select * from t1;
            +----+-------+
            | id | name  |
            +----+-------+
            |  1 | tom   |
            |  3 | maria |
            +----+-------+
            在master2上增加数据
            mariadb [db1]> insert t1(name) values ('jerry');
            mariadb [db1]> insert t1(name) values ('tony'); 
            mariadb [db1]> select * from t1;
            +----+-------+
            | id | name  |
            +----+-------+
            |  1 | tom   |
            |  3 | maria |
            |  4 | jerry |
            |  6 | tony  |

            五、半同步复制的实现

            ​默认情况下,mysql的复制功能是异步的,异步复制可以提供最佳的性能,主库把binlog日志发送给从库即结束,并不验证从库是否接收完毕。这意味着当主服务器或从服务器端发生故障时,有可能从服务器没有接收到主服务器发送过来的binlog日志,这就会造成主服务器和从服务器的数据不一致,甚至在恢复时造成数据的丢失;半同步复制的机制是只有当主节点和从节点同步完成,仅有一台同步完成即可,返回写入完成,这样的机制保证了数据的安全性。

            1)主节点

            [root@master ~]# vim /etc/my.cnf
            [mysqld]
            log_bin
            binlog_format=row
            log-basename=master
            server_id=1
            relay_log=relay-log
            relay_log_index=relay-log.index
            [root@master ~]# systemctl restart mariadb
            mariadb [(none)]> grant replication slave on *.* to repluser@'192.168.0.%' identified by 'replpass';
            mariadb [(none)]> show master logs;
            +-------------------+-----------+
            | log_name          | file_size |
            +-------------------+-----------+
            | master-bin.000001 |     26753 |
            | master-bin.000002 |    921736 |
            | master-bin.000003 |       401 |
            +-------------------+-----------+
            mariadb [(none)]> install plugin rpl_semi_sync_master soname 'semisync_master.so';  #安装模块
            mariadb [(none)]> set global rpl_semi_sync_master_enabled=1;  #开启半同步功能
            mariadb [(none)]> show global variables like '%semi%';
            +------------------------------------+-------+
            | variable_name                      | value |
            +------------------------------------+-------+
            | rpl_semi_sync_master_enabled       | on    |  #已开启
            | rpl_semi_sync_master_timeout       | 10000 |
            | rpl_semi_sync_master_trace_level   | 32    |
            | rpl_semi_sync_master_wait_no_slave | on    |
            +------------------------------------+-------+
            mariadb [(none)]> show global status like '%semi%';
            +--------------------------------------------+-------+
            | variable_name                              | value |
            +--------------------------------------------+-------+
            | rpl_semi_sync_master_clients               | 0     |
            | rpl_semi_sync_master_net_avg_wait_time     | 0     |
            | rpl_semi_sync_master_net_wait_time         | 0     |
            | rpl_semi_sync_master_net_waits             | 0     |
            | rpl_semi_sync_master_no_times              | 0     |
            | rpl_semi_sync_master_no_tx                 | 0     |
            | rpl_semi_sync_master_status                | on    |
            | rpl_semi_sync_master_timefunc_failures     | 0     |
            | rpl_semi_sync_master_tx_avg_wait_time      | 0     |
            | rpl_semi_sync_master_tx_wait_time          | 0     |
            | rpl_semi_sync_master_tx_waits              | 0     |
            | rpl_semi_sync_master_wait_pos_backtraverse | 0     |
            | rpl_semi_sync_master_wait_sessions         | 0     |
            | rpl_semi_sync_master_yes_tx                | 0     |
            +--------------------------------------------+-------+

            2)从节点1

            [root@slave1 ~]# vim /etc/my.cnf
            [mysqld]
            read_only=on
            log_bin
            binlog_format=row
            log-basename=slave
            server_id=2
            relay_log=relay-log
            relay_log_index=relay-log.index
            [root@slave1 ~]# systemctl restart mariadb
            mariadb [(none)]> change master to
                -> master_host='192.168.0.7',
                -> master_user='repluser',
                -> master_password='replpass',
                -> master_port=3306,
                -> master_log_file='master-bin.000003',
                -> master_log_pos=401,
                -> master_connect_retry=10;
            mariadb [(none)]> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
            mariadb [(none)]> set global rpl_semi_sync_slave_enabled=1;
            mariadb [(none)]> start slave;
            mariadb [(none)]> show master logs;
            +------------------+-----------+
            | log_name         | file_size |
            +------------------+-----------+
            | slave-bin.000001 |     26753 |
            | slave-bin.000002 |    921736 |
            | slave-bin.000003 |       245 |
            +------------------+-----------+
            mariadb [(none)]> grant replication slave on *.* to repluser@'192.168.0.%' identified by 'replpass';

            3)从节点2

            [root@slave2 ~]# vim /etc/my.cnf
            [mysqld]
            read_only=on
            server_id=3
            relay_log=relay-log
            relay_log_index=relay-log.index
            [root@slave2 ~]# systemctl restart mariadb
            mariadb [(none)]> change master to
                -> master_host='192.168.0.8',
                -> master_user='repluser',
                -> master_password='replpass',
                -> master_port=3306,
                -> master_log_file='slave-bin.000003',
                -> master_log_pos=245,
                -> master_connect_retry=10;
            mariadb [(none)]> start slave;

            4)从节点3

            [root@slave3 ~]# vim /etc/my.cnf
            [mysqld]
            read_only=on
            server_id=4
            relay_log=relay-log
            relay_log_index=relay-log.index
            [root@slave3 ~]# systemctl restart mariadb
            mariadb [(none)]> change master to
                -> master_host='192.168.0.8',
                -> master_user='repluser',
                -> master_password='replpass',
                -> master_port=3306,
                -> master_log_file='slave-bin.000003',
                -> master_log_pos=245,
                -> master_connect_retry=10;
            mariadb [(none)]> start slave;

            六、加密传输复制的实现

            在默认的主从复制过程或远程连接到mysql/mariadb所有的链接通信中的数据都是明文的,外网里访问数据或则复制,存在安全隐患。通过ssl/tls加密的方式进行复制的方法,来进一步提高数据的安全性

            主服务器开启ssl:[mysqld] 加一行ssl
            主服务器配置证书和私钥;并且创建一个要求必须使用ssl连接的复制账号
            从服务器使用changer master to 命令时指明ssl相关选项

            mariadb [(none)]> show variables like '%ssl%';+---------------+----------+| variable_name | value    |+---------------+----------+| have_openssl  | disabled || have_ssl      | disabled || ssl_ca        |          || ssl_capath    |          || ssl_cert      |          || ssl_cipher    |          || ssl_key       |          |+---------------+----------+

            特别提示:在配置之前先检查mysql服务是否支持ssl功能,如果have_ssl的值为’disabled’则支持;如果为’no’则不支持,需要再重新编译安装或者安装具有ssl功能的版本

            1)ca

            [root@ca ~]# mkdir /etc/my.cnf.d/ssl/
            [root@ca ~]# cd /etc/my.cnf.d/ssl/
            [root@ca ssl]# openssl genrsa 2048 > cakey.pem
            [root@ca ssl]# openssl req -new -x509 -key cakey.pem -out cacert.pem -days 3650 #自签证书
                country name (2 letter code) [xx]:cn
                state or province name (full name) []:beijing
                locality name (eg, city) [default city]:beijing
                organization name (eg, company) [default company ltd]:testmysqlca 
                organizational unit name (eg, section) []:opt
                common name (eg, your name or your server's hostname) []:ca.testmysqlca.com
            
            [root@ca ssl]# openssl req -newkey rsa:2048 -days 365 -nodes -keyout master.key > master.csr
                country name (2 letter code) [xx]:cn
                state or province name (full name) []:beijing
                locality name (eg, city) [default city]:beijing
                organization name (eg, company) [default company ltd]:testmysqlca
                organizational unit name (eg, section) []:opt
                common name (eg, your name or your server's hostname) []:master.testmysqlca.com
            [root@ca ssl]# openssl x509 -req -in master.csr -ca cacert.pem -cakey cakey.pem -set_serial 01 > master.crt #签署master证书
            
            [root@ca ssl]# openssl req -newkey rsa:2048 -days 365 -nodes -keyout slave.key > slave.csr
                country name (2 letter code) [xx]:cn
                state or province name (full name) []:beijing
                locality name (eg, city) [default city]:beijing
                organization name (eg, company) [default company ltd]:testmysqlca 
                organizational unit name (eg, section) []:opt
                common name (eg, your name or your server's hostname) []:slave.testmysqlca.com
            [root@ca ssl]# openssl x509 -req -in slave.csr -ca cacert.pem -cakey cakey.pem -set_serial 02 > slave.crt #签署slave证书
            
            [root@ca ssl]# openssl req -newkey rsa:2048 -days 365 -nodes -keyout slave2.key > slave2.csr
            country name (2 letter code) [xx]:cn
            state or province name (full name) []:beijing
            locality name (eg, city) [default city]:beijing
            organization name (eg, company) [default company ltd]:testmysqlca
            organizational unit name (eg, section) []:opt
            common name (eg, your name or your server's hostname) []:slave2.testmysqlca.com
            [root@ca ssl]# openssl x509 -req -in slave2.csr -ca cacert.pem -cakey cakey.pem -set_serial 03 > slave2.crt #签署slave2证书
            
            [root@ca ssl]# openssl verify -cafile cacert.pem master.crt slave.crt slave2.crt #检查证书是否可用
            master.crt: ok
            slave.crt: ok
            slave2.crt: ok
            先在各个节点上创建/etc/my.cnf.d/ssl/文件夹,将各自的证书,ca的证书和各自的秘钥文件复制过去
            [root@ca ssl]# scp cacert.pem master.crt master.key root@192.168.0.7:/etc/my.cnf.d/ssl/
            [root@ca ssl]# scp cacert.pem slave.crt slave.key root@192.168.0.8:/etc/my.cnf.d/ssl/ 
            [root@ca ssl]# scp cacert.pem slave2.crt slave2.key root@192.168.0.9:/etc/my.cnf.d/ssl/

            2)master

            [root@master ~]# mkdir /etc/my.cnf.d/ssl/
            [root@master ~]# vim /etc/my.cnf
            [mysqld]
            log_bin
            binlog_format=row
            log-basename=master
            server_id=1
            ssl #开启ssl功能
            ssl-ca=/etc/my.cnf.d/ssl/cacert.pem #指定ca证书的路径
            ssl-cert=/etc/my.cnf.d/ssl/master.crt #指定自己的证书的路径
            ssl-key=/etc/my.cnf.d/ssl/master.key #指定自己的秘钥文件路径
            [root@master ~]# systemctl restart mariadb
            mariadb [(none)]> grant replication slave on *.* to repluser@'192.168.0.%' identified by 'replpass' require ssl; #授权用户并且强制迫使用户开启ssl登录
            mariadb [(none)]> show master logs;
            +-------------------+-----------+
            | log_name          | file_size |
            +-------------------+-----------+
            | master-bin.000001 |     26753 |
            | master-bin.000002 |    921736 |
            | master-bin.000003 |       413 |
            +-------------------+-----------+

            3)slave1

            [root@slave1 ~]# mkdir /etc/my.cnf.d/ssl/
            [root@slave1 ~]# mysql -urepluser -preplpass -h192.168.0.7 --ssl-ca=/etc/my.cnf.d/ssl/cacert.pem --ssl-cert=/etc/my.cnf.d/ssl/slave.crt --ssl-key=/etc/my.cnf.d/ssl/slave.key
            [root@slave1 ~]# vim /etc/my.cnf
            [mysqld]
            read_only=on
            server_id=2
            relay_log=relay-log
            relay_log_index=relay-log.index
            ssl
            ssl-ca=/etc/my.cnf.d/ssl/cacert.pem
            ssl-cert=/etc/my.cnf.d/ssl/slave.crt
            ssl-key=/etc/my.cnf.d/ssl/slave.key
            [root@slave1 ~]# systemctl restart mariadb
            mariadb [(none)]> change master to
                -> master_host='192.168.0.7',
                -> master_user='repluser',
                -> master_password='replpass',
                -> master_port=3306,
                -> master_log_file='master-bin.000003',
                -> master_log_pos=413,
                -> master_connect_retry=10,
                -> master_ssl=1;  #注意,需要指明开启ssl链接
            mariadb [(none)]> start slave;

            4)slave2

            [root@slave2 ~]# mkdir /etc/my.cnf.d/ssl/
            [root@slave2 ~]# mysql -urepluser -preplpass -h192.168.0.7 --ssl-ca=/etc/my.cnf.d/ssl/cacert.pem --ssl-cert=/etc/my.cnf.d/ssl/slave2.crt --ssl-key=/etc/my.cnf.d/ssl/slave2.key
            [root@slave2 ~]# vim /etc/my.cnf
            [mysqld]
            read_only=on
            server_id=3
            relay_log=relay-log
            relay_log_index=relay-log.index
            ssl
            ssl-ca=/etc/my.cnf.d/ssl/cacert.pem
            ssl-cert=/etc/my.cnf.d/ssl/slave2.crt
            ssl-key=/etc/my.cnf.d/ssl/slave2.key
            [root@slave2 ~]# systemctl restart mariadb
            mariadb [(none)]> change master to
                -> master_host='192.168.0.7',
                -> master_user='repluser',
                -> master_password='replpass',
                -> master_port=3306,
                -> master_log_file='master-bin.000003',
                -> master_log_pos=413,
                -> master_connect_retry=10,
                -> master_ssl=1;
            mariadb [(none)]> start slave;

            七、mysql复制的相关指令和变量总结

            选项:

            • log_bin 启用二进制日志,在主节点或级联复制中间的从节点必须要开启
            • binlog_format=row 二进制日志记录方式为基于行的方式记录,强烈建议开启
            • log-basename=master | slave … 二进制日志的前缀名,不是必须向,但建议标识
            • server_id = # 服务器id,各个节点的id必须唯一
            • relay_log = relay-log 开启中继日志,并以relay-log为文件名开头,从节点开启
            • relay_log_index = relay-log.index 中继日志索引文件
            • log_slave_updates 作用是sql线程重读中继日志时将改变数据的操作记录为二进制日志,在级联复制中使用
            • ssl 开启ssl功能
              • ssl-ca=/etc/my.cnf.d/ssl/cacert.pem
              • ssl-cert=/etc/my.cnf.d/ssl/master.crt
              • ssl-key=/etc/my.cnf.d/ssl/master.key
            • sync_binlog=1 每次写后立即同步二进制日志到磁盘
              • innodb_flush_log_at_trx_commit=1 每次事务提交立即同步日志写磁盘
              • sync_master_info=# #次事件后master.info同步到磁盘
            • skip_slave_start=on 不自动启动slave
            • sync_relay_log=# #次写后同步relay log到磁盘
            • sync_relay_log_info=# #次事务后同步relay-log.info到磁盘
            • auto_increment_offset=1 自动增长开始点,在主主复制中使用

            变量:

            • replicate_do_db= 指定复制库的白名单
            • replicate_ignore_db= 指定复制库黑名单
            • replicate_do_table= 指定复制表的白名单
            • replicate_ignore_table= 指定复制表的黑名单
            • replicate_wild_do_table= foo%.bar% 支持通配符
            • replicate_wild_ignore_table= 指定复制的表,黑名单
            • rpl_semi_sync_slave_enabled=1 开启半同步复制,需要安装模块

            指令:

            • start slave; 启动主从复制
            • stop slave; 停止复制
            • show slave status; 查看复制状态
              • seconds_behind_master: 0 从服务器是否落后于主服务
            • reset slave all; 重置从服务器的配置
            • master_ssl=1, 配合 change master to 使用,开启ssl加密复制
              • master_ssl_ca = ‘/etc/my.cnf.d/ssl/cacert.pem’,
              • master_ssl_cert = ‘/etc/my.cnf.d/ssl/slave.crt’,
              • master_ssl_key = ‘/etc/my.cnf.d/ssl/slave.key’;
            • purge { binary | master } logs { to ‘log_name’ | before datetime_expr } 删除二进制日志,谨慎操作
            • show master status 查看二进制日志状态
            • show binlog events 查看二进制日志
            • show binary logs 查看二进制日志

            到此这篇关于mysql系列之十三 mysql的复制的文章就介绍到这了,更多相关mysql的复制内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

            (0)
            上一篇 2022年3月21日
            下一篇 2022年3月21日

            相关推荐