Linux centos下的数据库主从复制的配置
数据库主从复制的配置:
一台master ,两台slave
1.修改机器名称 在各个主机上
hostnamectl set-hostname master
slave1和slave2同理
2.三台机器时间同步,如果没有ntpdate 要安装,以阿里云时间为例,最后写入硬件时钟
yum install ntpdate -y
ntpdate ntp.aliyun.com
hwclock --systohc
3.修改master和两个slave的/etc/my.cnf
master追加
server-id=1
log-bin=master
slave分别添加
server-id=2
server-id=3
4.创建replication用户,在master里并赋权
create user replication@'%' identified by 'password';
grant replication slave ,replication client on . to 'replication'@'%';
flush privileges;
5.slave server上修改
change master to
master_host = 'master_ip',
master_user= 'replication',
master_password= 'master_password';
然后开启slave
start slave
show slave status\G
如果看到2个YES,成功。
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
注:
如果是两主一从即salve1也是slave2的master,在slave和master的/etc/my.cnf文件添加内容应该为
server-id=2 #主1用1,主2用2
log-bin=master
log-slave-updates=1
并且要在slave2的mysql里把slave1也设为master
change master to
master_host = 'master_ip',
master_user= 'replication',
master_password= 'master_password';
主从复制原理工作流程
主库操作:
执行事务并提交
将事务记录写入二进制日志(binlog)
返回结果给客户端
从库同步:
I/O线程:连接主库,请求binlog变更
主库的binlog dump线程发送binlog事件给从库
从库I/O线程接收事件并写入中继日志
SQL线程:读取中继日志并执行其中的SQL语句
即
主库执行提交事务-->事务记录写入binlog日志-->从库通过IO线程连接主库请求binlog-->主库用dump线程发送binlog事件给从库-->从库IO线程接收事件写入中继日志-->从库sql线程执行中继日志sql语句

浙公网安备 33010602011771号