标签:state 优点 实例 大量数据 生成 ted start more 顺序分配
GTID = source_id:sequence_id
sourceid:主服务器的唯一标识,通常用server_uuid来表示。
sequence_id:事务提交时由系统顺序分配的序列号,在Binlog中是递增且连续有序。
show master status \G
server_id: 跟传统的主从一样,每个实例不能一样
gtid_mode: 设置为ON,开启GTID模式
enforce_gtid_consistency:设置为ON,保证GTID一致性
log_bin: 跟传统的主从一样必须打开
log_slave_updates:建议开启 可以用来级联复制
binlog_format:强烈建议设置为ROW,否则会造成数据不一致
#skip-slave-start:设置为1,SLAVE启动时,不自动开启复制
MySQL5.7.6之前,如果数据库已经启动,必须重启数据库。具体步骤如下:
1.关闭MASTER的写入
2.SLAVE设置参数 skip-slave-start=1,避免SLAVE启动后,继续使用传统的复制模式。
3.按照上节所述,所有MySQL实例修改配置,开启GTID模式
4.重启所有MySQL数据库
MySQL5.7.6之后,可以在线调整,再无须重启了。
分三种情况:
CHANGE MASTER TO MASTER_HOST='***',
MASTER_PORT=*** ,
MASTER_USER='***',
MASTER_PASSWORD='***',
MASTER_AUTO_POSITION=1;
环境
CENTOS 7.2
MySQL5.7.22
1.下载安装xtrabackup
#下载XtraBackup
wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.12/binary/redhat/7/x86_64/percona-xtrabackup-24-2.4.12-1.el7.x86_64.rpm
#安装所需包
yum -y install perl perl-devel libaio libaio-devel perl-Time-HiRes perl-DBD-MySQL rsync perl-Digest-MD5 libev
#安装XtraBackup
rpm -ivh percona-xtrabackup-24-2.4.12-1.el7.x86_64.rpm
2.所有MySQL修改配置,可以在线修改,同时修改配置文件,以便下次生效
server_id =101/102
gtid_mode=ON
enforce_gtid_consistency=ON
log_bin=ON
log_slave_updates=ON
binlog_format=ROW
#skip_slave_start=1
3.备份数据库,获取MASTER数据以及GTID范围
#因为主库才开启GTID,模拟一些事务,使其产生一些binlog
create table test(id int);
insert into test (id)values(1),(2)
#备份数据
innobackupex --user=root --password=root --port=3306 /MySQL/my3306/data/fullbackup/
#查看gtid范围
[root@localhost 2018-07-26_23-23-00]# cat xtrabackup_binlog_info
binlog.000012 579 9760cb92-693e-11e8-85bf-000c29b55cf0:1-2
4.COPY备份数据到SLAVE,搭建SLAVE实例
#COPY数据
scp -r 2018-07-26_23-23-00 root@192.168.2.138:/MySQL/my3306/fullbackup/
#停止SLAVE
service mysqld stop
#从库将原有数据文件夹重命名到新位置,并创建原文件夹
mv /MySQL/my3306/data /MySQL/my3306/databak
mkdir -p /MySQL/my3306/data
#innobackupex apply-log
innobackupex --apply-log --user=root --password=root --port=3606 /MySQL/my3306/fullbackup/2018-07-26_23-23-00/
#innobackupex copy 恢复的文件到原来的数据位置
innobackupex --defaults-file=/etc/my.cnf --user=root --copy-back /MySQL/my3306/fullbackup/2018-07-26_23-23-00/
#给目录并赋权
chown -R mysql:mysql /MySQL/my3306/data
5.建立同步用户,跳过GTID,搭建主从
#主从库建立同步用户
grant replication slave,replication client on *.* to 'rep'@'%' identified by 'rep';
#启动SLAVE实例,设置gtid_purged的值,跳过GTID范围
service mysqld start
SET @@GLOBAL.GTID_PURGED='9760cb92-693e-11e8-85bf-000c29b55cf0:1-2'
#利用CHANGE MASTER语句,配置主从复制
CHANGE MASTER TO MASTER_HOST='192.168.2.144',
MASTER_PORT=3306 ,
MASTER_USER='rep',
MASTER_PASSWORD='rep',
MASTER_AUTO_POSITION=1;
#启动SLAVE复制,SLAVE会自动跳过GTID范围,拉取最新的GTID信息
START SLAVE;
#查看主从情况
SHOW SLAVE STATUS \G
6.验证主从同步
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.144
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000012
Read_Master_Log_Pos: 1561
Relay_Log_File: relaylog.000002
Relay_Log_Pos: 1387
Relay_Master_Log_File: binlog.000012
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: 1561
Relay_Log_Space: 1587
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: 101
Master_UUID: 9760cb92-693e-11e8-85bf-000c29b55cf0
Master_Info_File: /MySQL/my3306/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 9760cb92-693e-11e8-85bf-000c29b55cf0:3-6
Executed_Gtid_Set: 9760cb92-693e-11e8-85bf-000c29b55cf0:1-6
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
标签:state 优点 实例 大量数据 生成 ted start more 顺序分配
原文地址:https://www.cnblogs.com/chinesern/p/9387958.html