Mysql 5.5开始,加入了对半同步的支持(Semisynchronous Replication),半同步最初是由google设计的。同时Mysql将之前的主从结构逐渐取消,在5.5中,如果在slave服务器上的/etc/my.cnf中写入master-host等信息,会报未知参数的错误
首先按照前文,安装Mysql 5.5.8并进行TCP优化。
目前测试条件中,只使用2台服务器,其中一台作为Master:192.168.1.11,另一台作为Slave:192.168.1.12。实际生产条件下,若有多台slave一样进行配置操作。因为服务器在内网,且对多台不同的应用提供数据库服务,所以考虑简单起见,在两台服务器中分别执行以下权限代码:
view plaincopy to clipboardprint?
01.CREATE USER IDENTIFIED BY 'password';
02.GRANT ALL PRIVILEGES ON * . * TO IDENTIFIED BY 'password' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;
CREATE USER IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO IDENTIFIED BY 'password' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;
(请不要在公网使用类似的设置,这将造成一定的安全问题!)
根据mysql在线文档中的描述,使用半同步,需遵照以下3个条件:
1.版本5.5以上;
2.have_dynamic_loading 属性为 Yes,可以在mysql命令行运行
view plaincopy to clipboardprint?
01.[root@db001 ~]# /usr/local/mysql/bin/mysql -u root -p
02.Enter password:
03.mysql> SHOW VARIABLES LIKE "have_dynamic_loading";
04.+----------------------+-------+
05.| Variable_name | Value |
06.+----------------------+-------+
07.| have_dynamic_loading | YES |
08.+----------------------+-------+
09.1 row in set (0.00 sec)
[root@db001 ~]# /usr/local/mysql/bin/mysql -u root -p
Enter password:
mysql> SHOW VARIABLES LIKE "have_dynamic_loading";
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| have_dynamic_loading | YES |
+----------------------+-------+
1 row in set (0.00 sec)
即可得知是否支持,下文其他地方参数判断可以用同样方法;
3.必须已设置了主从关系。
设置主从关系步骤:
1.Master中my.cnf需设置server-id为1,log-bin需进行设置
view plaincopy to clipboardprint?
01.[mysqld]
02.log-bin = mysql-bin
03.server-id = 1
[mysqld]
log-bin = mysql-bin
server-id = 1
view plaincopy to clipboardprint?
01.mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.11', MASTER_USER='root', MASTER_PASSWORD='password', MASTER_PORT=3306, MASTER_CONNECT_RETRY=10, MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=107;
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.11', MASTER_USER='root', MASTER_PASSWORD='password', MASTER_PORT=3306, MASTER_CONNECT_RETRY=10, MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=107;
其中MASTER_LOG_FILE和MASTER_LOG_POS值来自于在Master命令行下运行SHOW MASTER STATUS。
设置半同步步骤:
1.在Master和Slave的mysql命令行运行如下代码:
view plaincopy to clipboardprint?
01.# On Master
02.mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
03.mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;
04.mysql> SET GLOBAL rpl_semi_sync_master_timeout = 1000;
05.# On Slave
06.mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
07.mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1;
08.mysql> STOP SLAVE IO_THREAD; START SLAVE IO_THREAD;
# On Master
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;
mysql> SET GLOBAL rpl_semi_sync_master_timeout = 1000;
# On Slave
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1;
mysql> STOP SLAVE IO_THREAD; START SLAVE IO_THREAD;
2.在Master和Slave的my.cnf中编辑:
view plaincopy to clipboardprint?
01.# On Master
02.[mysqld]
03.rpl_semi_sync_master_enabled=1
04.rpl_semi_sync_master_timeout=1000 # 1 second
05.# On Slave
06.[mysqld]
07.rpl_semi_sync_slave_enabled=1
# On Master
[mysqld]
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=1000 # 1 second
# On Slave
[mysqld]
rpl_semi_sync_slave_enabled=1
3.重启两台服务器即完成。
半同步测试
1.在Slave的mysql命令行输入SHOW SLAVE STATUS;如果显示有master内容且数据正确即已成功;
2.通过在Master上新建数据库和表,在Slave上查询测试是否成功;
3.通过查看data目录下的二进制日志增减可以进行测试。
通过以上的设置,简单的完成了数据库半同步,还有很多有待挖掘的部分,比如SSL支持等。作为内网Mysql均衡负载,还是有一定的可操作性。接下来的文章,将结合Mysql Proxy提出一个整体的数据库解决方案。
转:
阅读(1879) | 评论(0) | 转发(0) |