主服务器
1、创建一个用于slave同步的的账户:
/usr/local/mysql/bin/mysql -uroot -pryb123456 create user repl; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.0.%' IDENTIFIED BY 'mysql'; exit
2、配置master
vim /etc/my.cnf
在[mysqld]下面增加下面几行代码
server-id=188 log-bin=master-bin log-bin-index=master-bin.index
3、重启服务
su - mysql service mysql restart exit
查看状态:
/usr/local/mysql/bin/mysql -uroot -p密码 SHOW MASTER STATUS;#查看master,file字段用于从库连接实现复制
从服务器
1、配置Slave
vim /etc/my.cnf
在[mysqld]下面增加下面几行代码
server-id=196 relay-log-index=slave-relay-bin.index relay-log=slave-relay-bin
2、重启服务
su - mysql service mysql restart exit
3、连接master:
/usr/local/mysql/bin/mysql -uroot -p密码
SHOW SLAVE STATUS;#查看Slave状态
4、连接master
change master to master_host='47.93.254.196', master_port=3306, master_user='repl', master_password='mysql', master_log_file='master-bin.000001', master_log_pos=0; #启动slave主从复制 START SLAVE;