MySQL主从复制(实操)

2019-10-16  本文已影响0人  牍中玉的小木屋

一主一从模式

配置文件路径 /etc/my.cnf

  1. 主服务器( /etc/my.cnf )

基础配置
skip_name_resolve=ON
innodb_file_per_table=ON
server-id=1
log-bin=master-bin
log-bin-index=master-bin.index

登录bash,创建用户并授权

  • mysql -uroot -p
  • grant replication slave ,replication client on *.* to 'slave'@'%' identified by '123456';
  • flush privileges;
查看主服务器的状态信息,在从服务器中会用到
show master status\G
image.png
  1. 从服务器

基础配置( /etc/my.cnf )
skip_name_resolve=ON
innodb_file_per_table=ON
server-id=2
relay_log=slave_relay_bin

登录bash,配置master并开启slave

  • mysql -uroot -p
  • stop slave;
  • change master to master_host='192.168.43.56',master_port=3306,master_user='slave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=245;
  • start slave;
  • show slave status\G;
查看从服务器的状态信息,检车slave状态
image.png

主主复制模式

在主从复制的基础上继续处理,即:在主服务器(master)上开启relay_log在从服务器(slave)上开启log-bin,并且把步进值改为n

master

skip_name_resolve = ON
innodb_file_per_table = ON
server-id = 21
auto_increment_offset = 1
auto_increment_increment = 2
relay_log = slave_relay_bin
log-bin = master-bin
log-bin-index = master-bin.index

slave

skip_name_resolve = ON
innodb_file_per_table = ON
server-id = 22
relay_log = slave_relay_bin
auto_increment_offset = 2
auto_increment_increment = 2
log-bin = master-bin
log-bin-index = master-bin.index

  • change master to master_host='192.168.43.45',master_port=33066,master_user='slave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=669;
上一篇下一篇

猜你喜欢

热点阅读