环境:mysql
linux64位,一台机器两个实例,主库3306端口,从库3307端口linux
步骤:sql
1、下载安装数据库
先下载安装mysql,这里使用了5.7.21版本,具体过程不作详细说明,可自行查资料如何下载socket
2、配置ide
1.master配置,/etc/my.cnf文件入下
[mysqld]
port=3306
server-id=1
basedir=/home/shared_disk/mysql-5.7.21
datadir=/home/shared_disk/mysql-5.7.21/data
socket=/home/shared_disk/mysql-5.7.21/data/mysql.sock
symbolic-links=0
log-error=/home/shared_disk/mysql-5.7.21/data/error.log
pid-file=/home/shared_disk/mysql-5.7.21/data/mysql.pidspa
#skip-grant-tablesorm
#开启gtid模式,global transaction identified
gtid-mode=on
#保证gtid强一致性
enforce-gtid-consistency=1
#slave记录binlog,不然slave执行完relay log后就删了,找不到slave的执行记录
log_slave_updates
#按行记录binlog
binlog_format=row
#binlog文件名
log-bin=/home/shared_disk/mysql_datadir/mysql-bin
#须要记录binlog的数据库,即须要同步的数据库,同步是经过binlog实现的
binlog-do-db=allen
#不须要记录binlog的数据库
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=sysserver
其中红色部分是主从同步必须的配置事务
2.启动主库,安装到系统服务的直接service命令启动便可,没安装的运行mysqld --defaults-file=/etc/my.cnf --user=mysql &
3.登陆主库,建立主从复制的帐号
create user 'sync'@'localhost' identified by 'sync';
给帐号受权
grant file,replication slave on *.* to 'sync'@'localhost' identified by 'sync';
4.slave配置
配置文件/etc/my-slave.cnf入下
[mysqld]
port=3307
server-id=2
basedir=/home/shared_disk/mysql-slave
datadir=/home/shared_disk/mysql-slave/data
socket=/home/shared_disk/mysql-slave/data/mysql.sock
symbolic-links=0
log-error=/home/shared_disk/mysql-slave/data/error.log
pid-file=/home/shared_disk/mysql-slave/data/mysql.pid
#skip-grant-tables
gtid-mode=on
enforce-gtid-consistency=1
log_slave_updates
binlog_format=row
log-bin=/home/shared_disk/mysql_datadir/mysql-bin
#从库启动时跳过自动启动slave这一步
skip_slave_start=1
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
replicate-ignore-db=sys
5.启动从库,service命令或mysqld --defaults-file=/etc/my-slave.cnf --user=mysql &
启动后登陆进去,执行
stop slave;
change master to master_host='locahost',master_port=3306,master_user='sync',master_password='sync',master_auto_position=1;
start slave;
show slave status\G;
看到如下两个YES即说明主从已经搭建成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
PS:
当slave遇到问题时候,咱们能够show slave status\G;看到同步状况
Retrieved_Gtid_Set: 9461bcc7-4850-11e8-a9f7-525400045eeb:1-7
Executed_Gtid_Set: 4f7a4829-9a29-11e8-aa21-525400045eeb:1-5,9461bcc7-4850-11e8-a9f7-525400045eeb:1-7。
其中9461bcc7-4850-11e8-a9f7-525400045eeb是主库的mysql实例惟一id,4f7a4829-9a29-11e8-aa21-525400045eeb是从库的mysql实例惟一id,
Retrieved_Gtid_Set和Executed_Gtid_Set分别为已接收的和已执行的gtid,已执行的gtid中有一个和Retrieved_Gtid同样的,代表是执行了从主库同步过来的事务,另外一个则是本机本身执行的事务。
假设如今Executed_Gtid_Set以下:
Executed_Gtid_Set: 4f7a4829-9a29-11e8-aa21-525400045eeb:1-5,9461bcc7-4850-11e8-a9f7-525400045eeb:1-3
咱们能够看到同步的gtid从4以后就没执行了,应该是遇到错误了致使slave中止了,咱们能够用如下操做跳过第四个gtid,让slave继续工做
stop slave;
set gtid_next='9461bcc7-4850-11e8-a9f7-525400045eeb:4';
begin;
commit;
set gtid_next='automatic';
start slave;
show slave status\G;
原理就是让下一个事务指到第四个,而后begin接着commit,中间为空实现,让slave觉得这个事务执行成功了。
3、验证
主库建表,插入数据,去从库看是否存在相同数据。
从库使用show slave status\G;命令查看Retrieved_Gtid_Set和Executed_Gtid_Set。