maridb(>10.0)主从复制
1.环境说明
-
测试环境
系统版本:Ubuntu 16.04.5 LTS
内核版本:4.4.0-141-generic x86_64
MariaDB版本:mysql Ver 15.1 Distrib 10.0.36-MariaDB
主服务器:192.168.100.179 xyx-test01.lingda.com
从服务器:192.168.100.70 xyx0001.lingda.com
2.MariaDB配置
-
/etc/mysql/mariadb.conf.d/50-server.cnf #通用配置
[mysqld]
binlog-format=ROW
log-slave-updates=True # slave更新是否记入日志
master-info-repository=TABLE
relay-log-info-repository=TABLE # 此两项为打开从服务器崩溃二进制日志功能,信息记录在事物表而不是保存在文件
sync-master-info=1 # 值为1确保信息不会丢失
slave-parallel-threads=1 #同时启动多少个复制线程,最多与要复制的数据库数量相等即可
binlog-checksum=CRC32 # 效验码
master-verify-checksum=1 # 启动主服务器效验
slave-sql-verify-checksum=1 # 启动从服务器效验
binlog-rows-query-log-events=1 # 用于在二进制日志详细记录事件相关的信息,可降低故障排除的复杂度; -
master #添加配置
[mysqld]
server-id = 1
log_bin = /data/mysql/maridb-bin/maridb-bin.log -
重启master数据库
root@xyx-test01:~# /etc/init.d/mysql restart
root@xyx-test01:~# ls /data/mysql/maridb-bin/ #生成日志文件
maridb-bin.000001 maridb-bin.index -
slave添加配置
server-id = 2
relay_log = /var/lib/mysql/maridb-bin/maridb-relay-bin.log -
重启slave数据库
root@xyx0001:~# /etc/init.d/mysql restart
3.导出数据
-
保证数据一致性
主要考虑的问题是maridb从节点slave是在主节点master运行了一段时间之后才接入,常规同步时锁表(master)导出老数据,记录当前二进制日志文件maridb-bin.log和位置postion,但锁表会影响业务正常使用;故采用方法二:通过gtid的方式同步,导出老数据的时候文件开头自带当前二进制日志文件名、位置postion、gtid值,导出数据时带入参数:
--master-data=2
,记录二进制日志文件名、位置postion、gtid值--single-transaction
,导出老数据过程不锁表root@xyx-test01:/data# mysqldump -uzabbix -pzabbix --master-data=2 --single-transaction zabbix |gzip > zabbix.sql.gz
-
日志样例
-- CHANGE MASTER TO MASTER_LOG_FILE=‘maridb-bin.000005‘, MASTER_LOG_POS=3554718;
--
-- GTID to start replication from
--
28 -- SET GLOBAL gtid_slave_pos=‘0-1-465‘;
4.创建同步权限用户
master:
MariaDB [zabbix]>GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘replica‘@‘%‘ identified by ‘replica‘;
MariaDB [zabbix]>flush privileges;
5.同步数据
-
导入数据
root@xyx0001:/var/lib/mysql# nohup mysql -uzabbix -pzabbix zabbix < zabbix.sql &
-
启动同步(slave)
MariaDB [(none)]> SET GLOBAL gtid_slave_pos=‘0-1-465‘;
MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST=‘192.168.100.179‘,
-> MASTER_USER=‘replica‘,
-> MASTER_PASSWORD=‘lingdasa‘,
-> MASTER_PORT=3306,
-> MASTER_USE_GTID=slave_pos;
MariaDB [(none)]> START SLAVE;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.100.179
Master_User: replica
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: maridb-bin.000007
Read_Master_Log_Pos: 87457185
Relay_Log_File: maridb-relay-bin.000007
Relay_Log_Pos: 64698613
Relay_Master_Log_File: maridb-bin.000007
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: 64698321
Relay_Log_Space: 192672037
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: 2839
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
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: Slave_Pos
Gtid_IO_Pos: 0-1-884
-
部分参数说明
Slave_IO_Running: Yes、Slave_SQL_Running: Yes #表示同步状态正常
Master_Log_File和Relay_Master_Log_File 对应文件名相同,则slave当前读的日志和master当前最新的日志文件相同