Mysql-主多从和读写分离配置的代码示例简介

近期开发的系统中使用mysql作为数据库,由于数据涉及到money,所以不得不慎重。同时,用户对最大访问量也提出了要求。为了避免mysql成为性能瓶颈并具备很好的容错能力,特此实现主从热备和读写分离。在此简做纪要,以备日后所用!

一、配置主从

条件:两台PC,IP分别为192.168.168.253,192.168.168.251。两台PC上的Mysql版本为5.0。253上的Mysql为Master,251上的Mysql为Slave。

1、主数据库服务器配置

进入主数据库服务器安装目录,打开my.ini,在文件末尾增加如下配置:

#数据库ID号, 为1时表示为Master,其中master_id必须为1到232–1之间的一个正整数值;   server-id = 1  #启用二进制日志;  log-bin=mysql-bin  #需要同步的二进制数据库名;  binlog-do-db=minishop  #不同步的二进制数据库名,如果不设置可以将其注释掉;  binlog-ignore-db=information_schema  binlog-ignore-db=mysql  binlog-ignore-db=personalsite  binlog-ignore-db=test  #设定生成的log文件名;  log-bin="D:/Database/materlog"  #把更新的记录写到二进制文件中;  log-slave-updates

保存文件。重启Mysql服务。

进入从数据库服务器安装目录,打开my.ini,在文件末尾增加如下配置:

#如果需要增加Slave库则,此id往后顺延;  server-id = 2    log-bin=mysql-bin  #主库host  master-host = 192.168.168.253  #在主数据库服务器中建立的用于该从服务器备份使用的用户  master-user = forslave  master-password = ******  master-port = 3306  #如果发现主服务器断线,重新连接的时间差;  master-connect-retry=60  #不需要备份的数据库;   replicate-ignore-db=mysql  #需要备份的数据库  replicate-do-db=minishop  log-slave-update

保存文件。重启Mysql服务。

进入主数据库服务器,创建上面备份使用的用户名和密码,同时授权replication slave,super和reload

mysql>grant replication slave,super,reload on minishop.* to forslave@192.168.168.251 identified by '******';

进入从数据库服务器,启动Slave。

mysql>slave start;    mysql>show slave statusG;

测试:进入主数据库服务器,在Minishop中某张表中插入一条数据,然后到从数据库服务器中查看是否含有刚刚插入的数据。完毕!

备注:1)运行配置后的主数据库服务器先于从数据库服务器,这样运行从数据库服务器时,主库的 File 和 Position 与 从库的上设置Master_Log_File、Read_Master_Log_Pos 就会一致。否则,可能出现不一致的情况。这也可以通过命令调整。

2)如果发现主从复制失败时,可以先关闭从数据库服务器,然后删除从数据库服务器中data目录下relay-log.info,hosname-relay-bin*,master.info等文件,重启从服务器。

二、读写分离配置

本想采用Mysql Proxy来实现读写分离,奈何其使用的lua脚本着实让人头痛,最后决定采用国人开发的开源数据库代理中间件Amoeba。使用Amoeba,只需要简单的xml配置,就可以很容易地实现读写分离。

Amoeba处于应用程序和数据库服务器之间,充当一个中间代理层。其支持负载均衡、高可用性、Query过滤、读写分离、可路由相关的query到目标数据库、可并发请求多台数据库合并结果。功能很强大。

Amoeba默认的端口为8066,实现了Mysql协议。应用程序中只需要修改一个数据库连接就可以实现采用Amoeba来代理数据库访问。比如:java应用中,假如你原来的jdbc连接字符串为:jdbc:mysql://192.168.168.42:3306/minishop,那么现在,你想使用Amoeba作为数据库访问代理,则只需要将上面连接字符串改为如下(假如Amoeba所在机子IP为192.168.168.88):jdbc:mysql://192.168.168.88:8066/minishop。Amoeba透明性做的很赞。

主要还是配置Amoeda,但是配置也是相当的简单。基本只需要配置两个文件:confdbServers.xml和confamoeba.xml。配置中各项的含义,可以参考amoeda中文指南,这里不做过多解释。仅记录下配置。

dbServers.xml主要配置

<dbservers><!--   			Each dbServer needs to be configured into a Pool,  			If you need to configure multiple dbServer with load balancing that can be simplified by the following configuration:  			 add attribute with name virtual = "true" in dbServer, but the configuration does not allow the element with name factoryConfig  			 such as &#39;multiPool&#39; dbServer     		--><dbserver><factoryconfig><property>${defaultManager}</property><property>64</property><property>128</property><!-- mysql port --><property>3306</property><!-- mysql schema --><property>minishop</property><!-- mysql user --><property>chenjie</property><!--  mysql password --><property>chenjie</property></factoryconfig><poolconfig><property>500</property><property>500</property><property>10</property><property>600000</property><property>600000</property><property>true</property><property>true</property></poolconfig></dbserver><dbserver><factoryconfig><!-- mysql ip --><property>192.168.168.253</property></factoryconfig></dbserver><dbserver><factoryconfig><!-- mysql ip --><property>192.168.168.119</property></factoryconfig></dbserver><dbserver><factoryconfig><!-- mysql ip --><property>192.168.168.251</property></factoryconfig></dbserver><dbserver><poolconfig><!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA--><property>1</property><!-- Separated by commas,such as: server1,server2,server1 --><property>slave1,slave2</property></poolconfig></dbserver></dbservers>

amoeba.xml配置:

<configuration><proxy><!-- service class must implements com.meidusa.amoeba.service.Service --><service><!-- port --><property>8066</property><!-- bind ipAddress --><property>192.168.168.253</property><property>${clientConnectioneManager}</property><property><bean><property>128</property><property>64</property></bean></property><property><bean><property>chenjie</property><property>chenjie</property><property><bean><property>${amoeba.home}/conf/access_list.conf</property></bean></property></bean></property></service><!-- server class must implements com.meidusa.amoeba.service.Service --><service><!-- port --><!--  default value: random number  			<property name="port">9066</property>  			--><!-- bind ipAddress --><property>127.0.0.1</property><property>true</property><property>${clientConnectioneManager}</property><property><bean></bean></property></service><runtime><!-- proxy server net IO Read thread size --><property>20</property><!-- proxy server client process thread size --><property>30</property><!-- mysql server data packet process thread size --><property>30</property><!-- per connection cache prepared statement size  --><property>500</property><!-- query timeout( default: 60 second , TimeUnit:second) --><property>60</property></runtime></proxy><!--   		Each ConnectionManager will start as thread  		manager responsible for the Connection IO read , Death Detection  	--><connectionmanagerlist><connectionmanager><property>com.meidusa.amoeba.net.ConnectionManager</property><!--   			  default value is avaliable Processors   			<property name="processors">5</property>  			 --></connectionmanager><connectionmanager><property>com.meidusa.amoeba.net.AuthingableConnectionManager</property><!--   			  default value is avaliable Processors   			<property name="processors">5</property>  			 --></connectionmanager></connectionmanagerlist><!-- default using file loader --><dbserverloader><property>${amoeba.home}/conf/dbServers.xml</property></dbserverloader><queryrouter><property><bean><property>${amoeba.home}/conf/rule.xml</property><property>${amoeba.home}/conf/ruleFunctionMap.xml</property></bean></property><property>${amoeba.home}/conf/functionMap.xml</property><property>1500</property><property>master</property><property>master</property><property>multiPool</property><property>true</property></queryrouter></configuration>

至此,Mysql主从热备和读写分离配置完毕。但是,具体应用到生产环境究竟如何,还有待测试和考察。后来测试一主多从,又加入了一台Mysql从数据库服务器,这就是为什么上面amoeba配置中多了一个IP为119的原因。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享