1、简介
MySQL 5.7的发布,在复制方面有了很大的改进,比如开始支持多源复制(multi-source)了。以及真正的支持多线程复制了。需要说明一点的是,多源复制可以使用基于二进制日志的复制或者基于事务的复制。下面开始配置基于二进制日志的多源复制。
2、复制模式
一主一从
一主多从
级联复制
Multi-Master
Multi-Source
3、多源复制
# 用途
第一种:一般企业内大数据部门会需要各个业务部门的部分数据做数据分析,这个时候就可以用到多源复制把各个主数据库的数据复制到统一的大数据数据库中。
第二种:一般企业内大数据部门会需要各个业务部门的部分数据做数据分析,这个时候就可以用到多源复制把各个主数据库的数据复制到统一的大数据数据库中。
第三种:在从服务器进行数据汇总,如果我们的主服务器进行了分库分表的操作,为了实现后期的一些数据统计功能,往往需要把数据汇总在一起再统计
第四种:如果我们想在从服务器时时对主服务器的数据进行备份,在MySQL 5.7之前每一个主服务器都需要一个从服务器,这样很容易造成资源浪费,同时也加大了DBA的维护成本,但MySQL 5.7引入多源复制,可以把多个主服务器的数据同步到一个从服务器进行备份。
# 前提条件
不管是使用基于二进制日志的复制或者基于事务的复制,要开启多源复制功能,必须需要在从库上首先设置master_info_repository和relay_log_info_repository两个参数为’TABLE’(在mysql库下可以看见这两个表信息),是用来存储同步信息的,可以设置的值为’FILE’和’TABLE’,默认是FILE。
动态调整方式:SET GLOBAL master_info_repository = ‘TABLE’;
SET GLOBAL relay_log_info_repositiry = ‘TABLE’;
备注:同步状态需关闭
静态调整模式:master_info_repository = 'TABLE'
4、基础环境
# 软件版本
mysql-5.7.23.tar.gz
boost_1_59_0.tar.gz
# 主机地址
10.0.2.129:3360
10.0.2.130:3306
10.0.2.131:3306
5、部署
# 创建用户
mkdir -pv /data/mysql
useradd -u mysql -s /sbin/nologin -M mysql
chown -R mysql.mysql /usr/local/mysql
chown -R mysql.mysql /data/mysql
# 安装工具包
yum -y install gcc-c++ ncurses-devel cmake make perl gcc autoconf automake zlib libxml libgcrypt libtool bison
# 编译
cd /usr/local/src
tar xf boost_1_59_0.tar.gz
tar xf mysql-5.7.23.tar.gz
cd mysql-5.7.23
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_DATADIR=/data/mysql \
-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_BOOST=/usr/local/ boost_1_59_0
make -j `grep processor /proc/cpuinfo |wc -l`
make install
# 配置my.cnf
[client]
socket = /data/mysql/mysql.sock
[mysqld]
#skip-grant-tables
user = mysql
port = 3306
server_id = 1080
log_slave_updates = 1
basedir = /usr/local/mysql
datadir = /data/mysql
socket=/data/mysql/mysql.sock
pid_file=/data/mysql/mysql.pid
federated
character_set_server = utf8mb4
skip_name_resolve = 1
max_allowed_packet = 16777216
max_connections = 10000
max_connect_errors = 1000
tmpdir = /data/mysql
tmp_table_size = 67108864
tmp_table_size = 67108864
interactive_timeout = 1800
interactive_timeout = 1800
read_buffer_size = 16777216
read_rnd_buffer_size = 33554432
sort_buffer_size = 33554432
key_buffer_size = 256M
transaction_isolation = READ-COMMITTED
log-bin = /data/mysql/mysql-bin
expire_logs_days = 30
binlog_format = row
log_error = /data/mysql/error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/slow.log
innodb_buffer_pool_size = 8192M
innodb_thread_concurrency = 4
innodb_flush_log_at_trx_commit = 2
open_files_limit = 655350
innodb_open_files = 655350
master_info_repository = 'TABLE'
relay_log_info_repository = 'TABLE'
relay-log = /data/mysql/relay-log
explicit_defaults_for_timestamp=1
log-bin-trust-function-creators=1
sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
# 环境变量
echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
chmod a+x /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh
# 初始化
/usr/local/mysql/bin/mysql_install_db –user=mysql –basedir=/usr/local/mysql –datadir=/data/mysql
备注:配置文件查找顺序
/usr/local/mysql/bin/mysqld --help -vv | grep my.cnf
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf my.cnf, $MYSQL_TCP_PORT, /etc/services, built-in default
# 启动
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod a+x /etc/init.d/mysqld
chkconfig –add mysqld
chkconfig –level 3,5 mysqld on
# 安全加固
mysql_install_db
备注:密码修改
select user,host,authentication_string from mysql.user;
update mysql.user set authentication_string=PASSWORD('redhat') where user='root';
# 配置ssl
mysql_ssl_rsa_setup
show global variables like ‘%ssl%’; #查看变量值
\s #查看用户的连接方式
alter mysql.user test@'%' require ssl; #更改用户必须使用ssl登陆
grant all on *.* to 'slave'@'192.168.133.1' identified by 'ASDF123asdf' require ssl; #新建用户启用ssl
mysql -uroot -p123456 -h10.0.2.129 –ssl=1 #使用ssl方式登录
6、主从模式(基于二进制日志)
# 主库
mysql -uroot -p
reset master;
grant replication slave on *.* to ‘rsync’@’%’ identified by ‘123456’;
flush privileges;
show slave status;
# 从库
relay-log = /data/mysql/relay-log
log-slave-updates = true
skip-slave-start = true
master-info-repository = table
relay-log-info-repository = table
report-port = 3308
report-host = 10.0.2.129
replicate-do-db = test1
replicate-do-db = test2
replicate-do-table = test1.test1
replicate-do-table = test2.test2
# 配置主从
change master to master_host=’10.0.2.130’,
master_user=’rsync’,
master_port=3306,
master_password=’123456’,
master_log_file=’mysql-bin.000001’,
master_log_pos=1;
start slave;
show slave status;
# 测试
主库创建
create databases bbc;
use bbc;
create table bbc(a int,b char(2));
从库验证
show databases;
7、主从模式(基于GTID)
# 主库
vi /etc/my.cnf
gtid-mode = on
enforce-gtid-consistency = true
master-info-repository = table
relay-log-info-repository = table
log-slave-updates = true
binlog-checksum = CRC32
master-verify-checksum = 1
slave-sql-verify-checksum = 1
slave_allow_batching = 1
binlog-rows-query-log_events = 1
# 创建用户及权限
mysql -uroot -p
reset master;
grant replication slave on *.* to ‘rsync’@’%’ identified by ‘123456’;
flush privileges;
show slave status;
# 从库配置
gtid-mode = on
enforce-gtid-consistency = true
sync-master-info = 1
slave-parallel-workers = 1
binlog-checksum=CRC32
master-verify-checksum = 1
slave-sql-verify-checksum = 1
slave_allow_batching = 1
binlog-rows-query-log_events = 1
relay_log_purge = 1
relay_log_recovery = 1
# 配置主从
change master to master_host=’10.0.2.130’,
master_user=’rsync’,
master_port=3306,
master_password=’123456’,
master_log_file=’mysql-bin.000001’,
master_log_pos=1,
master_auto_position=1;
start slave;
show slave status;
8、多源同步
# 主库001
mysql -uroot -p
reset master;
grant replication slave on *.* to ‘rsync’@’%’ identified by ‘123456’;
flush privileges;
show slave status;
# 主库002
mysql -uroot -p
reset master;
grant replication slave on *.* to ‘rsync’@’%’ identified by ‘123456’;
flush privileges;
show slave status;
# 从库同步主库001
change master to master_host=’10.0.2.129’,
master_user=’rsync’,
master_port=3306,
master_password=’123456’,
master_log_file=’mysql-bin.000001 for channel ‘master_129’;
# 从库同步主库002
change master to master_host=’10.0.2.130’,
master_user=’rsync’,
master_port=3306,
master_password=’123456’,
master_log_file=’mysql-bin.000001 for channel ‘master_130’;
# 管理主从
start slave; #开启所有主从
start slave for channel ‘master_129’; #开启从库129
start slave for channel ‘master_130’; #开启从库130
reset slave all for channel ‘master_129’; #删除129同步
show slave status; #查看同步信息
# 测试多源复制
主库001
create database test1;
use test1;
create table ‘test2’(id int(11) default null, ‘count’ int(11) default null );
insert into test2 values(1,1);
主库002
create database test2;
use test1;
create table ‘test1’(id int(11) default null, ‘count’ int(11) default null );
insert into test2 values(1,1);
从库查看
show databases;
标签:info,同步,slave,log,data,MySQL,master,mysql,多源
From: https://blog.51cto.com/zzzhao/6164749