准备
1 1. 双主 master1 192.168.199.49
2 master2 192.168.199.50
3 VIP 192.168.199.52 //虚拟IP4 2.环境 master:nginx + php + mysql +keepalived5 VIP:只要和master在一个局域网内即可。6 3. 服务器之间网络通畅,可以互相ping通。7 4. 2个服务器的mysql版本要一致。数据库密码一致8 5. 防火墙增加允许组播和允许VRRP(虚拟路由器冗余协)通信,这样主服务器在故障恢复后才能抢回资源9 -A INPUT -s 182.148.15.0/24 -d 224.0.0.18 -j ACCEPT10 -A INPUT -s 182.148.15.0/24 -p vrrp -j ACCEPT11 重启生效:service iptables reload
配置服务器
1 服务器1:2
3 在my.cnf文件的[mysqld]配置区域添加下面内容:4 log-bin=mysql-bin #记录二进制文件
5 binlog_format=mixed #mysql默认采用的二进制格式
6 server-id = 2 #服务号,必须是唯一的,一般取IP的后8位
7 expire_logs_days = 10 #binlog过期清理时间
8
9 binlog-do-db = db1 #需要服务的数据库。
10 #replicate-ignore-db = db3,mysql,test,information_schema,performance_schema,transfer_new #不需要复制的数据库
11 #replicate-wild-ignore-table=garbage.% #有问题可以尝试使用此项配置
12 sync_binlog = 1 #表示binlog日志在每1次写入后与硬盘同步,设置1最安全也最慢
13 auto-increment-increment = 2 #服务器个数
14 auto-increment-offset = 2 #自增偏移量,如果有2台,则一台设置1,另一台设置2
15 slave-skip-errors = all #从库复制时跳过所有的错误
16 服务器2:17
18 log-bin=mysql-bin19 binlog_format=mixed20 server-id = 1
21 expire_logs_days = 10
22 binlog-do-db =db123 #replicate-ignore-db = db3,mysql,test,information_schema,performance_schema,transfer_new
24 #replicate-wild-ignore-table=garbage.% #有问题可以尝试使用此项配置
25 sync_binlog = 1
26 auto-increment-increment = 2
27 auto-increment-offset = 1
28 slave-skip-errors = all
2台服务器都重启mysql服务:
1 service mysql restart
同步配置(2台服务器分别执行)
1 给对方授权复制权限2
3 //对方服务器60.205.182.26允许复制本服务器数据库,用户名repl 密码ocnt-123
4 mysql> grant replication slave,replication client on *.* to repl@'60.205.182.26' identified by "ocnt-123";5 //生效6 mysql>flush privileges;7 //锁定表8 mysql> flush tables with read lock;