主主复制
即两台服务器互相复制
大致思路:
1.两台服务器都设置上2进制日志和relay日志
[root@localhost ~]# vi /etc/my.cnf [mysqld] #binary log server-id=1 #server-id=2 log-bin=mysql-bin binlog_format=mixed relay-log=mysql-relay
2.都设置replication账号
mysql> grant replication client,replication slave on *.* to 'repl'@'192.168.6.%' identified by '123456';
3.都设置对方为自己的master
change master to master_host='192.168.6.4',master_user='repl',master_password='123456',master_log_file='mysql-bin.000026',master_log_pos=120;
主主复制时主键冲突问题
主主复制时,两台服务器同步的主键有重复
解决办法:
A服务器,主键按1,3,5奇数增长
mysql> set global auto_increment_increment=2; mysql> set global auto_increment_offset=1; mysql> set session auto_increment_increment=2; mysql> set session auto_increment_offset=1;
B服务器,主键按2,4,6偶数增长
mysql> set global auto_increment_increment=2; mysql> set global auto_increment_offset=2; mysql> set session auto_increment_increment=2; mysql> set session auto_increment_offset=2;
后期加业务此方法有限制,可在业务逻辑上解决.
比如orcle,有sequnce序列
序列每次访问生成递增/递减的数据
以redis为例,可专门构建global:userid
每次php插入mysql前,先incre->global:userid,得到一个不重复的userid
被动主主复制
两台服务器地位一样,其中一台为只读,另一台为只写
优势为,写服务器出现问题,能迅速切换到从服务器,或者出于检修等目的,把写入功能切换到另一台服务器也比较方便.
设置为只读服务器
[root@localhost ~]# vi /etc/my.cnf [mysqld] read-only=1
mysql> show variables like '%read_only%' +-----------------------------------------+---------------------------+ | Variable_name | Value | +-----------------------------------------+---------------------------+ | read_only | ON | +-----------------------------------------+---------------------------+



