MySQL单机多实例的读写分离可以通过第三方软件来实现,其中一个叫简单的读写分离软件就是今天所带给大家的amoeba软件。
本次使用的amoeba软件为amoeba-mysql-binary-2.1.0-RC5.tar.gz
本软件是基于JAVA环境运行的,所以需要JAVA环境支持,具体环境配置不做详解。
本文主要解释两个主配置文件。
1、dbServer.xml
这个文件配置的是父配置(用来给默认子配置补充配置项)
真实mysql服务器的端口,数据库名称,mysql用户及密码
主服务器,从服务器,及从服务器连接池。这里只定义数据库地址,它们的用户及密码就是上面的abstractServer里的设置。
注意用来连接真实mysql服务器的用户必须拥有远程连接权限。
<?xml version="1.0" encoding="gbk"?> <!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd"> <amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/"> <!-- 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 'multiPool' dbServer --> <!-- 默认父配置,所有子配置的相同项从此读取 --> <dbServer name="abstractServer" abstractive="true"> <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory"> <property name="manager">${defaultManager}</property> <property name="sendBufferSize">64</property> <property name="receiveBufferSize">128</property> <!-- mysql ip MySQL端的IP地址 --> <property name="ipAddress">172.16.1.52</property> <!-- mysql port MySQL端的端口 --> <!-- <property name="port">3306</property> --> <!-- mysql schema MySQL端监控工作库 --> <property name="schema">amoeba</property> <!-- mysql user MySQL端的库对应用户 --> <property name="user">amoeba</property> <!-- mysql user MySQL端的库对应用户的密码 --> <property name="password">123456</property> </factoryConfig> <!-- 默认池的参数配置,一般不用改 --> <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool"> <property name="maxActive">500</property> <property name="maxIdle">500</property> <property name="minIdle">10</property> <property name="minEvictableIdleTimeMillis">600000</property> <property name="timeBetweenEvictionRunsMillis">600000</property> <property name="testOnBorrow">true</property> <property name="testWhileIdle">true</property> </poolConfig> </dbServer> <!-- 自定义配置池,用来设定读取或写入 --> <dbServer name="server1" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <!-- <property name="ipAddress">172.16.1.52</property> --> <property name="port">3306</property> </factoryConfig> </dbServer> <!-- 定义某个池为主(即写)--> <dbServer name="master" virtual="true"> <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool"> <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA 若有多主则可配置此项,1为轮询,2为权重--> <property name="loadbalance">1</property> <!-- Separated by commas,such as: server1,server2,server1 指定主所对应的池--> <property name="poolNames">server1</property> </poolConfig> </dbServer> <!-- 同master配置原理 --> <dbServer name="server2" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <!-- <property name="ipAddress">172.16.1.52</property> --> <property name="port">3307</property> </factoryConfig> </dbServer> <dbServer name="slave" virtual="true"> <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool"> <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA--> <property name="loadbalance">1</property> <!-- Separated by commas,such as: server1,server2,server1 --> <property name="poolNames">server2</property> </poolConfig> </dbServer> </amoeba:dbServers>
2、amoeba.xml
配置的内容为如下项:
定义连接amoeba时用来验证的用户及密码
真实的读写分离配置
defaultPool:配置了默认的数据库节点,一些除了SELECT\UPDATE\INSERT\DELETE的语句都会在 defaultPool执行。
writePool :配置了数据库写库,通常配为Master,如这里就配置为之前定义的Master数据库。
readPool :配置了数据库读库,通常配为Slave或者Slave组成的数据库池,如这里就配置之前的 virtualSlave数据库池
<?xml version="1.0" encoding="gbk"?> <!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd"> <amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/"> <proxy> <!-- service class must implements com.meidusa.amoeba.service.Service --> <service name="Amoeba for Mysql" class="com.meidusa.amoeba.net.ServerableConnectionManager"> <!-- port amoeba服务器对WEB端提供的工作端口 --> <property name="port">3306</property> <!-- bind ipAddress amoeba服务器对WEB端提供的工作IP --> <property name="ipAddress">172.16.1.9</property> <property name="manager">${clientConnectioneManager}</property> <property name="connectionFactory"> <bean class="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory"> <property name="sendBufferSize">128</property> <property name="receiveBufferSize">64</property> </bean> </property> <property name="authenticator"> <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator"> <!-- amoeba服务器对WEB端提供的工作用户 --> <property name="user">root</property> <!-- amoeba服务器对WEB端提供的工作用户的密码 --> <property name="password">123456</property> <property name="filter"> <bean class="com.meidusa.amoeba.server.IPAccessController"> <property name="ipFile">${amoeba.home}/conf/access_list.conf</property> </bean> </property> </bean> </property> </service> <!-- server class must implements com.meidusa.amoeba.service.Service --> <service name="Amoeba Monitor Server" class="com.meidusa.amoeba.monitor.MonitorServer"> <!-- port --> <!-- default value: random number <property name="port">9066</property> --> <!-- bind ipAddress --> <property name="ipAddress">127.0.0.1</property> <property name="daemon">true</property> <property name="manager">${clientConnectioneManager}</property> <property name="connectionFactory"> <bean class="com.meidusa.amoeba.monitor.net.MonitorClientConnectionFactory"></bean> </property> </service> <runtime class="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext"> <!-- proxy server net IO Read thread size --> <property name="readThreadPoolSize">200</property> <!-- proxy server client process thread size --> <property name="clientSideThreadPoolSize">300</property> <!-- mysql server data packet process thread size --> <property name="serverSideThreadPoolSize">300</property> <!-- per connection cache prepared statement size --> <property name="statementCacheSize">500</property> <!-- query timeout( default: 60 second , TimeUnit:second) --> <property name="queryTimeout">60</property> </runtime> </proxy> <!-- Each ConnectionManager will start as thread manager responsible for the Connection IO read , Death Detection --> <connectionManagerList> <connectionManager name="clientConnectioneManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper"> <property name="subManagerClassName">com.meidusa.amoeba.net.ConnectionManager</property> <!-- default value is avaliable Processors <property name="processors">5</property> --> </connectionManager> <connectionManager name="defaultManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper"> <property name="subManagerClassName">com.meidusa.amoeba.net.AuthingableConnectionManager</property> <!-- default value is avaliable Processors <property name="processors">5</property> --> </connectionManager> </connectionManagerList> <!-- default using file loader --> <dbServerLoader class="com.meidusa.amoeba.context.DBServerConfigFileLoader"> <property name="configFile">${amoeba.home}/conf/dbServers.xml</property> </dbServerLoader> <queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter"> <property name="ruleLoader"> <bean class="com.meidusa.amoeba.route.TableRuleFileLoader"> <property name="ruleFile">${amoeba.home}/conf/rule.xml</property> <property name="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property> </bean> </property> <property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property> <property name="LRUMapSize">1500</property> <property name="defaultPool">server1</property> <!-- 指定读取和写入的指定池 --> <property name="writePool">server1</property> <property name="readPool">server2</property> <property name="needParse">true</property> </queryRouter> </amoeba:configuration>
MySQL单机多实例的配置文件内容简单注释到此结束。