【mysql基本实验】mysql复制(异步复制)

关键词:mysql复制(异步复制),mysql异步复制

#mysql主从基本实验

前提:centos6.5,mysql5.26

  (1)网络可以互相ping通:ping 192.168.135.158

  (2)双方端口是否侦听开启:netstat -an|grep 3306

  (3)防火墙策略:service iptables stop;(这里只是测试,我就直接关闭了哈)

  (4)selinux策略:getenforce(查看状态) setenforce 0 (关闭)

  (5)彻底关闭开机自启:

  防火墙 ~~ chkconfig iptables off  

    

  selinux~~ vim /etc/selinux/config ,然后把selinux=disabled,如下图

    


#【0】主从均开启binlog,设置server-id

## replication
log-bin=mysql-bin
server-id=1 #从设置为server-id=2
binlog_format= ROW
#gtid_mode = on
#enforce_gtid_consistency = 1
log_slave_updates = 1   #是否复制日志写入从库的Binlog中
master-info-repository=TABLE
relay-log-info-repository=TABLE

#【1】准备复制账户

  mysql下操作:(这里的host ip是从库IP)
  主:grant replication slave on *.* to 'repl'@'192.168.135.159' identified by '123456';

#【2】在主库上,设置读锁定有效。以便获取一个一致性的快照

  mysql下操作:(锁表,获取一致性)
  flush tables with read lock;

#【3】show master status;获取主库当前的二进制名和偏移量pos位置。

  show master status;
  -- 查看到的日志名:mysql-bin.000002  , postion: 881

#【4】备份主库还原到从库
  物理备份 直接cp,逻辑备份 mysqldump。然后scp拷贝过去。

  物理方式:直接拷贝,备份删除从库原有data目录,然后把主库的data目录复制过去,复制到从库后记得删除拷贝过来data/下的auto.cnf,否则uuid会一样,导致无法复制出现故障【8】。

#【5】跳过从线程启动Mysql
  mysqld_safe --skip-slave-start &

#【6】在mysql下配置从库复制线程

-- mysql环境下
change master to
master_host='192.168.135.158',
master_port=3306,
master_user='repl',
master_password='123456',
master_log_file='mysql-bin.000002',
master_log_pos=881;

#【7】在mysql下启动从线程并验证
  start slave;

  show processlist; -- 一般有2个如下线程,基本就是没有问题了,但是要确认的话,还是得用下面的命令;

    

  show slave status\G -- 这种带error字样的字段没有信息或者是0,那才是正确的!

    



#【8】故障诊断
【8.1】The slave I/O thread stops because master and slave have equal MySQL server UUIDs
原因就是直接用主库的文件覆盖到从库的,导致auto.cnf中的uuid相同;

参考:深入浅出mysql开发、优化与管理维护书目

猜你喜欢

转载自www.cnblogs.com/gered/p/11239898.html