赞
踩
MySQL互为主从
Mysql-A:192.168.189.140
Mysql-B:192.168.189.141
在A、B上操作:
#安装:
[root@ localhost ~]# yum -y install mysql mysql-server
#修改配置文件:
[root@ localhost ~]# vim /etc/my.cnf
#添加二进制日志:
log-bin=mysql-bin //开启二进制日志
server-id=1 //mysql的id,默认是1,唯一的,建议使用ip最后一段
relay-log=mysql-relay //开启中继日志
#重启mysql
[root@ localhost ~]# service mysqld restart
#登录mysql,新建同步用户:
[root@ localhost ~]# mysql -uroot -p
mysql> grant replication slave on *.* to 'tom'@'%' identified by '123456';
mysql> flush privileges;
#初始化bin-log日志
mysql> reset master;
#查看最新bin-log日志:
mysql> show master status;
主机A配置完毕,切记为了保持主从的一致性,先不要操作主机A mysql数据库,待主机B配置完成且设置同步后才可以进行操作。
主机A、B配置:
#修改配置文件:
[root@ localhost ~]# vim /etc/my.cnf
log-bin=mysql-bin
server-id=2
relay-log=mysql-relay
#重启mysql服务器
[root@ localhost ~]# service mysqld restart
#增加同步用户:
[root@ localhost ~]# mysql -uroot –p
mysql> grant replication slave on *.* to 'tom'@'%' identified by '123';
mysql> flush privileges;
#初始化bin-log日志:
mysql> reset master;
#查看bin-log日志:
mysql> show master status;
主机B配置完成,现在进行同步配置
同步配置:
#在主机A上操作:
mysql> change master to
master_host='192.168.189.141',
master_user='tom',
master_password='123',
master_log_file='mysql-bin.000001',
master_log_pos=120;
这里的master_host是指主机B的ip地址,master_uesr是执行同步操作的用户名,master_password是执行同步的用户密码,master_log_file是主机B的bin-log日志,master_log_pos是从bin-log日志开始同步的位置。
#开启同步:
mysql> start slave;
查看同步设置是否成功:
mysq 1> show slave status \G; -*************************************1.row********************************************- slave_ IO_ State: waiting for master to send event Master_ .Host: 172.18.49. 2 Master_ user: tongbu Master_ Port: 3306 Connect_ Retry: 60 Master_ Log_Fi le: mysql-bin. 000001 Read_ Master_ .Log_ POS: 120 Relay_ Log_ File: test1-relay-bin. 000002 Relay_ Log_ POS: 283 Relay_ Master_ Log_ File: mysq1-bin. 000001 STave_ IO_ Running: Yes slave_ SQL_ Running: Yes Replicate DO_ DB: Replicate_ Ignore_ DB: Replicate_ DO_ Table: Replicate_ Ignore_ Table: Replicate_ _wild_DO_ Table: Replicate_ _wild_Ignore_Table: Last_ Errno: 0 Last_Error :
有双yes则完成正确
在主机B 上操作:
mysql> change master to
master_host='192.168.189.140',
master_user='tom',
master_password='123',
master_log_file='mysql-bin.000001',
master_log_pos=120;
mysql> start slave;
mysql> show slave status\G;
mysq 1> show slave status \G; -*************************************1.row********************************************- slave_ IO_ State: waiting for master to send event Master_ .Host: 172.18.49. 2 Master_ user: tongbu Master_ Port: 3306 Connect_ Retry: 60 Master_ Log_Fi le: mysql-bin. 000001 Read_ Master_ .Log_ POS: 120 Relay_ Log_ File: test1-relay-bin. 000002 Relay_ Log_ POS: 283 Relay_ Master_ Log_ File: mysq1-bin. 000001 STave_ IO_ Running: Yes slave_ SQL_ Running: Yes Replicate DO_ DB: Replicate_ Ignore_ DB: Replicate_ DO_ Table: Replicate_ Ignore_ Table: Replicate_ _wild_DO_ Table: Replicate_ _wild_Ignore_Table: Last_ Errno: 0 Last_Error :
有双yes则完成正确
同步测试:
在A主机或者B主机上做都可以。
#在A上新建一个数据库
mysql> create database test1;
#在B上查看
mysql>show databases;
mysql> create database test1;
#在A上查看
mysql>show databases;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。