Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2809917
  • 博文数量: 587
  • 博客积分: 6356
  • 博客等级: 准将
  • 技术积分: 6410
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-23 10:54
个人简介

器量大者,福泽必厚

文章分类

全部博文(587)

文章存档

2019年(3)

2018年(1)

2017年(29)

2016年(39)

2015年(66)

2014年(117)

2013年(136)

2012年(58)

2011年(34)

2010年(50)

2009年(38)

2008年(16)

分类: LINUX

2013-08-17 20:06:41

semi-sync工作原理:
mysql5.0---mysql5.1下master/slave工作原理图:

With regular replication, you send a transaction to the master (1). When the COMMIT is received, the master executes it (2), and if successful it logs the transaction to the binary log (3). The the master answers the client request (4) with a successful result. In the meantime, the slaves replicate the record (5).
What happens if the master crashes after point #4 and before a slave has had a chance of getting the data in point #5?
The client will have a result for that transaction, but that data is lost, because it has never reached one slave. 
##个人理解这种情况如果发生,数据写到了master,而没有写入到slave,造成了master/slave的数据不一致!


mysql5.5下引入了semi-sync ,工作流程:

Let's see the same scenario with semi-synchronous replication. All is the same until point #3. Then, things change. The master does not return to the client. Instead, it alerts the slave that a transaction is available (4). The slave gets it and stores it to the relay log (5). Without further action, the slave tells the master that the transaction was received (6) and only then the master returns the result to the client (7).
What is the benefit? If the master crashes, the transaction is lost. but the client does not get the false information that the transaction was stored. Instead, if the master crashes before it returns the result to the client, the client gets an error, and knows that that transaction needs to be reworked when a new master is back.

 The important thing to understand about this feature is that semi-synchronous replication does not guarantee that your transaction is executed in the slave. It will only tell you that the data has been transferred to the slave relay log. It can still happen that the transaction fails to execute on the slave (which could be either a bug in your application or a bug in MySQL replication). But this is not a cluster. Don't expect a two-phase commit.
摘之:
http://datacharmer.blogspot.com/2010/11/testing-mysql-55-semi-synchronous.html   ##感谢作者


1:安装两台mysql,配置、并启动它
  做master/slave时最好两台机器型号、性能一样
2:查看是否安装semi相关so文件
[root@master binlog]# cd /data/mysql/lib/plugin/
[root@master plugin]# ls *semi*
semisync_master.so  semisync_slave.so
 
在master上加载该模块:
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (0.04 sec)


并在配置[mysqld]部分下加入:
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=1000
log-bin                 = /data/mysqllog/binlog/s3-***-g***z-com-bin
log-bin-index           = /data/mysqllog/binlog/s3.***.g****z.com-bin.index
binlog_format    = mixed
binlog_cache_size = 4M
max_binlog_cache_size = 8M
max_binlog_size       = 2G
expire_logs_days = 7
binlog-do-db     = hello
#binlog-do-db     = db1
#binlog-do-db     = db2

#binlog-do-db     = db3
replicate-ignore-db =mysql



在slave上加载:
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.03 sec)


并在配置文件[mysqld]中加入:
 server-id        = 2
expire_logs_days = 2
relay-log-index   = /data/mysqllog/relaylog/slave-relay.index
relay-log         = /data/mysqllog/relaylog/slave-relay-bin
rpl_semi_sync_slave_enabled=1
slave-skip-errors=all ##这个可以

然后分别重启mysqld服务!


3:在master创建一个用户:
mysql> grant all on *.* to 'slaveuser'@'192.168.1.182'  identified by 'slaveuser';
Query OK, 0 rows affected (0.00 sec)


mysql> 
在slave机器上测试是否可以登录:
[root@haproxy1 ~]# mysql -h 192.168.1.181 -uslaveuser -pslaveuser
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.29-log Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> 

显然可以登录!
4:下面配置slave机器:
mysql>stop slave
mysql>
mysql>start slave
当 :
    Slave_IO_Running: Yes
    Slave_SQL_Running: Yes
就表示都正常了!


mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.1.181
                  Master_User: slaveuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: s3-***-g***z-com-bin.000001
          Read_Master_Log_Pos: 107
               Relay_Log_File: slave-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: s3-****-g****z-com-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          ****
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 1236
                Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Misconfigured master - server id was not set'
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
1 row in set (0.00 sec)


原因:我忘记设置master的server-id了!
解决方法:在master上的/etc/my.cnf中加入:server-id=1即可!
然后重启master和slave上的上的mysql即可正常!

问题:
在master/slave使用过程中发现一个问题:如果在master上大量插入数据,例如:不停地导入sql,在master/slave上会有延迟,甚至延迟很大!
见附件!

这是master/slave机制决定的,mysql5.6版本已经进行了改观!当然如果不大量导入sql,仅仅是通过web网站插入、update数据,master/slave工作没问题的!
或者大量写的时候,Seconds_Behind_Master 相关项不为0  ,见附件
 
原因:
master/slave在大量load数据的时候会有延迟,见附件 ##摘之淘宝丁奇
阅读(1536) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~