栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

【MYSQL优化27,28,29】主主复制

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【MYSQL优化27,28,29】主主复制

主主复制

即两台服务器互相复制

大致思路:

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						  |
+-----------------------------------------+---------------------------+




转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/972586.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号