A: 192.168.2.155主
B: 192.168.2.156从/主
C: 192.168.2.160从
A主机:
vi /etc/my.cnf
log-bin=mysql-bin
server-id=1
binlog_do_db=tongbu
(注:如不指定此参数,binlog记录所有库的操作,添加用户binlog也会改变,也会复制到从服务器)
service mysqld restart
mysql -uroot -p123456
create database tongbu;use tongbu;create table tbl1(id int);insert into tbl1 values(1);select * from tbl1;
grant replication slave on *.* to tongbu156@192.168.2.156 identified by '123456';
flush tables with read lock;(可以同时锁定多个库,锁定时不可以建表等操作,退出后所有库自动解锁)
show master status; (记下日志文件及position)
exit;
mysqldump -uroot -p123456 tongbu > /tongbu.sql (把此文件拷贝到B主机/下)
B主机:
vi /etc/my.cnf
log-bin=mysql-bin
server-id=2
binlog_do_db=tongbu
(注:如不指定此参数,binlog记录所有库的操作,添加用户binlog也会改变,也会复制到从服务器)
log-slave-updates(产生自己的日志文件,建议在此位置添加该行)
service mysqld restart
mysql -uroot -p123456
create database tongbu;
use tongbu;
source /tongbu.sql
开启从服务器角色
change master to master_host='192.168.2.155',master_user='tongbu156',master_password='123456',master_log_file='mysql-bin.000007',master_log_pos=107
start slave;
grant replication slave on *.* to tongbu160@192.168.2.160 identified by '123456';
flush tables with read lock;
show master status; (记下日志文件及position)
exit;
mysqldump -uroot -p123456 tongbu > /tongbu.sql (把此文件拷贝到C主机/下)
C主机:
vi /etc/my.cnf
log-bin=mysql-bin
server-id=3
service mysqld restart
mysql -uroot -p123456
create database tongbu;
use tongbu;
source /tongbu.sql
开启从服务器角色
change master to master_host='192.168.2.156',master_user='tongbu160',master_password='123456',master_log_file='mysql-bin.000009',master_log_pos=222
start slave;
设置完成,在A主机修改数据,查看B、C主机数据是否改变
注:
show slave status;查看从服务器状态
show processlist;查看日志处理状态
====================================
从服务器线程控制命令
mysql>start slave sql_thread;
mysql>start slave io_thread;