主配置文件
[mysqld]
datadir=/data/mydata
log-bin=/data/binlogs/master-bin
socket=/var/lib/mysql/mysql.sock
innodb_file_per_table=on
server-id=1
sync_binlog=on #在事务提交时,立即把bin-log缓存区中的事件立即刷写到磁盘到的二进制日志区上,保证从服务器尽早同步数据
innodb_flush_log_at_trx_commit=on #当使用innodb引擎,在事务提交时,把内存中跟事务相关的数据立即写到事务日志中
innodb_support_xa=on #innodb支持分布式事务
从配置文件
[mysqld]
datadir=/data/mydata
# log-bin=/data/binlogs/bin-log
socket=/var/lib/mysql/mysql.sock
innodb_file_per_table=on
relay-log=/data/binlogs/relay-bin
server-id=7
read_only=on
skip_slave_start=on #第1次启动START SLAVE后,重启从mysql后,默认slave自动启动,应该关闭自动启动
#在主节点授权具有复制权限用户 为了方便,我这里给的权限十分大,生产环境不建议这么搞。一般给予REPLICATION SLAVE,REPLICATION CLIENT MariaDB [(none)]> GRANT ALL ON *.* to "test"@"10.0.0.%" identified by "test"; MariaDB [(none)]> flush privileges; #在从服务器上连接到主 MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='10.0.0.41',MASTER_USER='test',MASTER_PASSWORD='test'; MariaDB [(none)]> START SLAVE; #上面的方式,是从数据库从0开始同步主的数据,很简单就没有贴实验 #下面是先做完全还原后再从事件某位置同步主数据 [root@centos7-4 ~]# mysqldump --all-databases --flush-logs --master-data=2 --lock-all-tables >all.sql [root@centos7-4 ~]# less all.sql -- CHANGE MASTER TO MASTER_LOG_FILE='master-bin.000004', MASTER_LOG_POS=245; [root@centos7-4 ~]# scp all.sql 10.0.0.41:/root/ MariaDB [(none)]> create database helloit; MariaDB [(none)]> use helloit; MariaDB [helloit]> CREATE TABLE test (id int); MariaDB [helloit]> INSERT INTO test VALUES (1),(2),(3); MariaDB [helloit]> show tables; +-------------------+ | Tables_in_helloit | +-------------------+ | test | +-------------------+ 1 row in set (0.00 sec) MariaDB [helloit]> select * from test; +------+ | id | +------+ | 1 | | 2 | | 3 | +------+ MariaDB [helloit]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO "test"@"10.0.0.88" IDENTIFIED BY "test"; #在备节点上 #先导入完全备份的数据 MariaDB [(none)]> source /root/all.sql; MariaDB [test]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hellodb | | mysql | | performance_schema | | test | +--------------------+ #并没有 helloit这个 #这时再实行主从同步 MariaDB [test]> CHANGE MASTER TO MASTER_HOST="10.0.0.41",MASTER_USER="test",MASTER_PASSWORD="test",MASTER_LOG_FILE="master-bin.000004",MASTER_LOG_POS=245; #在从上启动sql和IO线程 MariaDB [test]> START SLAVE; #验证实验结果 MariaDB [test]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hellodb | | helloit | | mysql | | performance_schema | | test | +--------------------+ MariaDB [test]> use helloit; MariaDB [helloit]> select * from test; +------+ | id | +------+ | 1 | | 2 | | 3 | +------+ MariaDB [helloit]> show master status; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000004 | 1115 | | | +-------------------+----------+--------------+------------------+ #再次在主数据库插入数据 MariaDB [helloit]> INSERT INTO test VALUES (4),(5),(6); Query OK, 3 rows affected (0.02 sec) Records: 3 Duplicates: 0 Warnings: 0 MariaDB [helloit]> select * from test; +------+ | id | +------+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | +------+ #在从数据库上查看结果 MariaDB [helloit]> select * from test; +------+ | id | +------+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | +------+ 6 rows in set (0.00 sec) MariaDB [helloit]> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.41 Master_User: test Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000004 Read_Master_Log_Pos: 1115 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 1400 Relay_Master_Log_File: master-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: 1115 Relay_Log_Space: 1688 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、从服务器应该为只读模式,在配置文件里设置read_only=on,但是对于超级管理用户无效,需要从终端施加读锁,一直在后台运行:
MariaDB [(none)]> FLUSH TABLES WITH READ LOCK;
2、在主从节点分别设置sync_master_info=on、sync_relay_log=on、sync_relay_log_info=on会降低I/O性能
主主复制
配置步骤:
1、各个节点使用一个唯一的server_id
2、都启动binary log 和relay_log
3、创建拥有复制权限的用户帐号
4、各自定义自动增长键为奇数和偶数
5、均把对方指定为主节点,并启动复制线程
主配置文件
[mysqld]
datadir=/data/mydata
log-bin=/data/binlogs/master-bin
relay_log=/data/relogs/relay-bin
socket=/var/lib/mysql/mysql.sock
innodb_file_per_table=on
server-id=1
auto_increment_offset=1
auto_increment_increment=2
从配置文件
[mysqld]
datadir=/data/mydata
log-bin=/data/binlogs/bin-log
relay-log=/data/relogs/relay-bin
socket=/var/lib/mysql/mysql.sock
innodb_file_per_table=on
server-id=7
auto_increment_offset=2
auto_increment_increment=2
各自创建授权帐号 主1 MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO "test"@"10.0.0.88" IDENTIFIED BY "test"; MariaDB [(none)]> flush privileges; MariaDB [(none)]> show master status; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000001 | 671 | | | +-------------------+----------+--------------+------------------+ 主2 MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO "test"@"10.0.0.41" IDENTIFIED BY "test"; MariaDB [(none)]> flush privileges; MariaDB [(none)]> show master status; +----------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +----------------+----------+--------------+------------------+ | bin-log.000003 | 671 | | | +----------------+----------+--------------+------------------+ #在主1上连主2 MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST="10.0.0.88",MASTER_USER="test",MASTER_PASSWORD="test",MASTER_LOG_FILE="bin-log.000003",MASTER_LOG_POS=671; #在主2上连主1 MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST="10.0.0.41",MASTER_USER="test",MASTER_PASSWORD="test",MASTER_LOG_FILE="master-bin.000001",MASTER_LOG_POS=671; #分别启动从模式 start slave #在主1上插入一些数据 MariaDB [(none)]> source /root/hellodb.sql MariaDB [hellodb]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hellodb | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.02 sec) #在主2上查看是否同步 MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hellodb | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) #在主2上创建一个数据库 MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | IT | | hellodb | | mysql | | performance_schema | | test | +--------------------+ 6 rows in set (0.00 sec) #在主1上查看 MariaDB [hellodb]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | IT | | hellodb | | mysql | | performance_schema | | test | +--------------------+ 6 rows in set (0.02 sec) 已经双主同步 #主1上的二进制日志和中继日志 [root@centos7-4 data]# ls /data/binlogs/ master-bin.000001 master-bin.index [root@centos7-4 data]# ls binlogs/ master-bin.000001 master-bin.index [root@centos7-4 data]# ls relogs/ relay-bin.000001 relay-bin.000002 relay-bin.index #主2上的二进制日志和中继日志 [root@mariadb data]# ls binlogs/ bin-log.000001 bin-log.000002 bin-log.000003 bin-log.index [root@mariadb data]# ls relogs/ relay-bin.000001 relay-bin.000002 relay-bin.index
半同步
#主配置文件
[mysqld]
datadir=/data/mydata
log-bin=/data/binlogs/master-bin
socket=/var/lib/mysql/mysql.sock
innodb_file_per_table=on
server-id=1
#从配置文件
[mysqld]
datadir=/data/mydata
#log-bin=/data/binlogs/bin-log
relay-log=/data/relogs/relay-bin
socket=/var/lib/mysql/mysql.sock
innodb_file_per_table=on
server-id=7
#主上创建具有复制权限的帐号 MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO "test"@"10.0.0.88" IDENTIFIED BY "test"; #在从上连接主数据库 MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST="10.0.0.41",MASTER_USER="test",MASTER_PASSWORD="test",MASTER_LOG_FILE="master-bin.000003",MASTER_LOG_POS=413; MariaDB [(none)]> start slave; #检查主从能同步后,关闭主从状态,加入半同步插件 MariaDB [(none)]> stop slave; #在主节点上安装主的半同步插件 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)]> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> SET GLOBAL rpl_semi_sync_slave_enabled=1; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show global variables like "%semi%"; +---------------------------------+-------+ | Variable_name | Value | +---------------------------------+-------+ | rpl_semi_sync_slave_enabled | ON | | rpl_semi_sync_slave_trace_level | 32 | +---------------------------------+-------+ 2 rows in set (0.00 sec) MariaDB [(none)]> start slave; #在主上查看是否有从节点加入 MariaDB [(none)]> SHOW GLOBAL STATUS LIKE '%semi%'; +--------------------------------------------+-------+ | Variable_name | Value | +--------------------------------------------+-------+ | Rpl_semi_sync_master_clients | 1 | | 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 | +--------------------------------------------+-------+ #已经有一个备用节点加入 #在主上创建新数据,测试效果 MariaDB [(none)]> CREATE DATABASE MBA; MariaDB [(none)]> SHOW GLOBAL STATUS LIKE '%semi%'; +--------------------------------------------+-------+ | Variable_name | Value | +--------------------------------------------+-------+ | Rpl_semi_sync_master_clients | 1 | | Rpl_semi_sync_master_net_avg_wait_time | 17434 | | Rpl_semi_sync_master_net_wait_time | 17434 | | Rpl_semi_sync_master_net_waits | 1 | | 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 | 18407 | | Rpl_semi_sync_master_tx_wait_time | 18407 | | Rpl_semi_sync_master_tx_waits | 1 | | Rpl_semi_sync_master_wait_pos_backtraverse | 0 | | Rpl_semi_sync_master_wait_sessions | 0 | | Rpl_semi_sync_master_yes_tx | 1 | +--------------------------------------------+-------+ #完成半同步过程需要1.8s左右。
复制过滤器
让从节点仅复制指定数据库,或指定数据库的指定表
有2种方式:
1、主服务器仅向二进制日志中记录与特定数据库(特定表)相关的事件,不建议使用,因为无法实现时间还原
2、从服务器SQL_THREAD在replay中继日志中的事件时,仅读取与特定数据库(特定表)相关的事件并应用于本地
在从服务器配置参数过滤,只复制MBA之个库
从数据库配置文件
[mysqld]
datadir=/data/mydata
#log-bin=/data/binlogs/bin-log
relay-log=/data/relogs/relay-bin
socket=/var/lib/mysql/mysql.sock
innodb_file_per_table=on
server-id=7
replicate_do_db=MBA #只允许复制MBA
注:可用参数
replicate_do_db= #白名单,只复制某个数据库
replicate_ignore_db= #黑名单
replicate_do_table= #表的白名单
replicate_ignore_table= #表的黑名单
replicate_wild_do_table= #表的通配表达,白名单
replicate_wild_ignore_table=
在主上插入数据 MariaDB [MBA]> CREATE TABLE hello (id int); MariaDB [MBA]> INSERT INTO hello VALUES (1),(2),(3); Query OK, 3 rows affected (0.02 sec) Records: 3 Duplicates: 0 Warnings: 0 MariaDB [MBA]> select * from hello; +------+ | id | +------+ | 1 | | 2 | | 3 | +------+ 在从上查看 MariaDB [MBA]> show tables; +---------------+ | Tables_in_MBA | +---------------+ | hello | +---------------+ 1 row in set (0.00 sec) MariaDB [MBA]> select * from hello; +------+ | id | +------+ | 1 | | 2 | | 3 | +------+ 在主上插入其他库数据 MariaDB [MBA]> create database it; Query OK, 1 row affected (0.00 sec) MariaDB [MBA]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | MBA | | it | | mysql | | performance_schema | | test | +--------------------+ 在从数据库上查看 MariaDB [MBA]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | MBA | | mysql | | performance_schema | | test | +--------------------+ 并没有it这个新创建的数据库