1. 程式人生 > >Mysql 實戰之——讀寫分離方案

Mysql 實戰之——讀寫分離方案

Linux環境:Centos 6.8  64-bit

Mysql 版本:5.1.7

二、使用Amoeba資料庫代理來實現讀寫分離

Amoeba作為資料庫代理,以中介軟體的形式存在,拓撲圖如下所示:(Amoeba官網的一張圖)

1、下載Amoeba:https://sourceforge.net/projects/amoeba/files/

2、解壓並複製到local目錄

[[email protected] linux ~]# unzip amoeba-mysql-3.0.5-RC-distribution.zip

[[email protected] linux ~]# cp -rf amoeba-mysql-3.0.5-RC /usr/local

3、啟動Amoeba。

[root@chenllcentos ~]# /usr/local/amoeba-mysql-3.0.5-RC/bin/launcher

如果出現fatal exception:

The stack size specified is too small, Specify at least 228k
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

原因是由於stack size太小,導致JVM啟動失敗,要如何修改呢?

 解決:

[[email protected] linux ~]# vi /usr/local/amoeba-mysql-3.0.5-RC/jvm.properties

將內容:

JVM_OPTIONS="-server -Xms256m -Xmx1024m -Xss196k -XX:PermSize=16m -XX:MaxPermSize=96m"

替換為:

JVM_OPTIONS="-server -Xms1024m -Xmx1024m -Xss256k -XX:PermSize=16m -XX:MaxPermSize=96m"

4、修改配置檔案:Amoeba完成讀寫分離的實現主要是依靠2個非常重要的配置檔案:

dbServers.xml  和  amoeba.xml

4.1、首先,看看dbServers.xml檔案

[[email protected] conf]# cat dbServer.xml

這個檔案主要是配置資料庫連結,主庫,還有從庫(slave)節點的資訊,以及資料庫連線池資訊

如下內容就是這個檔案的配置

<?xml version="1.0" encoding="gbk"?><!DOCTYPE amoeba:dbServers SYSTEM “dbserver.dtd”><amoeba:dbServersxmlns: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節點abstractive="true",包含Mysql的公共配置資訊,其他dbServer節點都繼承該節點 --><!-- 設定節點配置的繼承結構,可以避免重複配置相同資訊,減少配置檔案冗餘 --><dbServername="abstractServer"abstractive="true"><factoryConfigclass="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory"><propertyname="connectionManager">${defaultManager}</property><propertyname="sendBufferSize">64</property><propertyname="receiveBufferSize">128</property><!-- mysql port --><!-- Mysql預設埠 --><propertyname="port">3306</property><!-- mysql schema --><!-- 預設連線的資料庫,若不存在需要事先建立,否則Amoeba啟動報錯 --><propertyname="schema">testdb</property><!-- mysql user --><propertyname="user">root</property><propertyname="password">222</property></factoryConfig><poolConfigclass="com.meidusa.toolkit.common.poolable.PoolableObjectPool"><propertyname="maxActive">500</property><propertyname="maxIdle">500</property><propertyname="minIdle">1</property><propertyname="minEvictableIdleTimeMillis">600000</property><propertyname="timeBetweenEvictionRunsMillis">600000</property><propertyname="testOnBorrow">true</property><propertyname="testOnReturn">true</property><propertyname="testWhileIdle">true</property></poolConfig></dbServer><!-- Mysql主伺服器master節點繼承abstractServer --><dbServername="master"parent="abstractServer"><factoryConfig><!-- master資料庫主機地址 --><propertyname="ipAddress">192.168.48.133</property></factoryConfig></dbServer><!-- Mysql從伺服器slave節點繼承abstractServer --><dbServername="slave1"parent="abstractServer"><factoryConfig><!-- slave資料庫主機地址 --><propertyname="ipAddress">192.168.48.132</property></factoryConfig></dbServer><!-- 如果有多個從伺服器節點, 可以複製多個dbServer --><!-- 配置資料庫讀取連線池 Amoeba實現讀寫分離根據readPool 連線池來實現--><dbServername="readPool"virtual="true"><poolConfigclass="com.meidusa.amoeba.server.MultipleServerPool"><!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA--><propertyname="loadbalance">1</property><!-- Separated by commas,such as: server1,server2,server1 --><propertyname="poolNames">slave1</property></poolConfig></dbServer>

配置完成之後:

4.2、接著配置amoeba.xml

vi amoeba.xml

內容如下:

<?xml version="1.0" encoding="gbk"?><!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd"><amoeba:configurationxmlns:amoeba="http://amoeba.meidusa.com/"><proxy><!-- service class must implements com.meidusa.amoeba.service.Service --><servicename="Amoeba for Mysql"class="com.meidusa.amoeba.mysql.server.MySQLService"><!-- port --><propertyname="port">8066</property><!-- bind ipAddress --><!-- 
			<property name="ipAddress">127.0.0.1</property>
			 --><propertyname="connectionFactory"><beanclass="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory"><propertyname="sendBufferSize">128</property><propertyname="receiveBufferSize">64</property></bean></property><propertyname="authenticateProvider"><beanclass="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator"><propertyname="user">root</property><propertyname="password"></property><propertyname="filter"><beanclass="com.meidusa.toolkit.net.authenticate.server.IPAccessController"><propertyname="ipFile">${amoeba.home}/conf/access_list.conf</property></bean></property></bean></property></service><runtimeclass="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext"><!-- proxy server client process thread size --><propertyname="executeThreadSize">128</property><!-- per connection cache prepared statement size  --><propertyname="statementCacheSize">500</property><!-- default charset --><propertyname="serverCharset">utf8</property><!-- query timeout( default: 60 second , TimeUnit:second) --><propertyname="queryTimeout">60</property></runtime></proxy><!-- 
		Each ConnectionManager will start as thread
		manager responsible for the Connection IO read , Death Detection
	--><connectionManagerList><connectionManagername="defaultManager"class="com.meidusa.toolkit.net.MultiConnectionManagerWrapper"><propertyname="subManagerClassName">com.meidusa.toolkit.net.AuthingableConnectionManager</property></connectionManager></connectionManagerList><!-- default using file loader --><dbServerLoaderclass="com.meidusa.amoeba.context.DBServerConfigFileLoader"><propertyname="configFile">${amoeba.home}/conf/dbServers.xml</property></dbServerLoader><queryRouterclass="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter"><propertyname="ruleLoader"><beanclass="com.meidusa.amoeba.route.TableRuleFileLoader"><propertyname="ruleFile">${amoeba.home}/conf/rule.xml</property><propertyname="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property></bean></property><propertyname="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property><propertyname="LRUMapSize">1500</property><propertyname="defaultPool">multiPool</property><!--
		<property name="writePool">server1</property>
		<property name="readPool">server1</property>
		--><propertyname="needParse">true</property></queryRouter></<