amoeba mysql binary_amoeba安装与实现amoeba for mysql读写分离
運行環(huán)境
l? CentOS6.3
l? Jdk1.6.0_30
l? amoeba-mysql-binary-2.2.0
l? amoeba:192.168.88.17
l? master1:192.168.88.10
l? slave1:192.168.88.11
一: 安裝jdk1.5以上版本
1.1??? 卸載centos服務器自帶版本jdk
1.1.1????????? 查看服務器自帶jdk版本號
[root@amoeba1 ~]#java –version
1.1.2 查看java信息
[root@amoeba1 ~]#rpm -qa | grep java
1.1.3????????? 卸載java文件
[root@amoeba1 ~]#rpm -e --nodeps java-1.6.0-openjdk-1.6.0.0-1.45.1.11.1.el6.x86_64
1.1.4????????? 再次查看java版本,已經(jīng)刪除
[root@amoeba1 ~]#java –version
1.2??? 安裝jdk
1.2.1????????? 創(chuàng)建/usr/java文件夾,將jdk安裝文件拷貝到此目錄
[root@amoeba1 ~]#mkdir /usr/java
[root@amoeba1 ~]#cd /usr/java
1.2.2????????? 賦予權限
[root@amoeba1 java]#chmod 777 jdk-6u30-linux-x64-rpm.bin
1.2.3????????? 安裝jdk
[root@amoeba1 java]#./jdk-6u30-linux-x64-rpm.bin
1.2.4????????? 配置環(huán)境變量
[root@amoeba1 java]#vi /etc/profile
在配置文件最后面添加下面3條語句
export JAVA_HOME=/usr/java/jdk1.6.0_30
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
1.2.5????????? 配置完成后,重啟服務器
[root@amoeba1 java]#reboot
1.3?????重啟完成后查看新安裝jdk版本
[root@amoeba1 ~]#java –version
二: 安裝amoeba
2.1下載amoeba,http://sourceforge.net/projects/amoeba/files/,我用的版本是amoeba-mysql-binary-2.2.0.tar
2.2?? 創(chuàng)建amoeba文件夾,將文件解壓到此文件夾
[root@amoeba1 ~]#mkdir /usr/local/amoeba
[root@amoeba1 ~]#cd /usr/local/amoeba/
[root@amoeba1 amoeba]#tar -zxvf amoeba-mysql-binary-2.2.0.tar.gz
2.3?? 驗證是否安裝成功
[root@amoeba1 amoeba]#/usr/local/amoeba/bin/amoeba start
三:參數(shù)配置
[root@amoeba1 amoeba]#cd /usr/local/amoeba/conf/
[root@amoeba1 conf]#ls
l? Amoeba.xml:主配置文件,配置數(shù)據(jù)源和amoeba的自身參數(shù)
l? dbServer.xml:需要至少配置一個dbServer,每個dbServer將是物理數(shù)據(jù)庫Server的衍射
l? log4j.xml:日志文件
l? rule.xml:配置所有Query路由規(guī)則的信息
l? functionMap.xml:配置用于解析Query中的函數(shù)所對應的Java實現(xiàn)類
l? rullFunctionMap.xml:配置路由規(guī)則中需要使用到的特定函數(shù)的實現(xiàn)類
這里我們通過修改amoeba.xml和dbServer.xml來實現(xiàn)讀寫分離,修改log4j.xml來節(jié)約服務器資源。
3.1 修改amoeba.xml
3.1.1 配置server
[root@amoeba1 amoeba]#cd /usr/local/amoeba/conf/
[root@amoeba1 conf]#?vi amoeba.xml
配置項
是否必選
默認值
說明
port
否
8066
amoeba server綁定的對外端口
ipAddress
否
空
Amoeba綁定的IP
user
是
空
客戶端連接到Amoeba的用戶名
password
否
空
客戶端連接到Amoeba的密碼
readThreadPoolSize
否
16
負責讀客戶端、database server端網(wǎng)絡數(shù)據(jù)包線程數(shù)
clientSideThreadPoolSize
否
16
負責讀執(zhí)行客戶端請求的線程數(shù)
serverSideThreadPoolSize
否
16
負責處理服務端返回數(shù)據(jù)包的線程數(shù)
我配置的數(shù)據(jù)是:將端口號修改為了3306,用戶root,密碼123456,ip:192.168.88.17
3.1.2 注釋amoeba for monitor,因為我們不需要這個功能,只需要amoeba for mysql
3.1.3 配置runtime,具體意義參看3.1.1圖標
3.1.4 配置connectionManager:需要至少配置一個ConnectionManager,每個ConnectionManager將作為一個線程啟動,ConnectionManager負責管理所注冊在自身的Connection,負責他們的空閑檢測、死亡檢測、IO Event
3.1.5 配置默認引用其他配置文件,這里需要修改讀寫分離的數(shù)據(jù)庫name值
配置項
是否必選
默認值
說明
class
是
空
QueryRouter實現(xiàn)類,Amoeba for Mysql (com.meidusa.amoeba.mysql.parser.MysqlQueryRouter)
functionConfig
否
空
用于解析sql函數(shù)的配置文件,如果不配置則將不解析包含函數(shù)sql或者解析的不完整
ruleConfig
否
空
數(shù)據(jù)切分規(guī)則配置文件,如果不配置則sql數(shù)據(jù)切分功能將不能用
needParse
否
true
是否對sql進行parse,如果false則將不能使用數(shù)據(jù)切分、讀寫分離等功能
defaultPool
是
空
needParse=false,無法解析query,不滿足切分規(guī)則的,writePool | ? readPool = null情況。所有sql將在默認的dbServer上面執(zhí)行。
writePool
否
空
啟用needParse功能,并且沒有匹配到數(shù)據(jù)切分規(guī)則,則update、inster、delete語句將在這個pool中執(zhí)行
readPool
否
空
啟用needParse功能,并且沒有匹配到數(shù)據(jù)切分規(guī)則,則select語句將在這個pool中執(zhí)行
LRUMapSize
否
1000
statment cache , 存放sql解析后得到的statment
3.2 配置dbServer.xml
3.2.1 配置文件信息:
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
一臺mysqlServer 需要配置一個pool,如果多臺 平等的mysql需要進行l(wèi)oadBalance,平臺已經(jīng)提供一個具有負載均衡能力的objectPool:com.meidusa.amoeba.mysql.server.MultipleServerPool,簡單的配置是屬性加上 virtual="true",該Pool 不允許配置factoryConfig或者自己寫一個ObjectPool。
3.2.2 配置 factoryconfig,配置amoeba訪問信息,注意默認的注釋問題,要把密碼注釋去掉
3.2.3 配置連接池信息
配置項
是否必選
默認值
說明
className
否
com.meidusa.amoeba.net.poolable.PoolableObjectPool
連接池實現(xiàn)類
maxActive
否
8
最大活動連接數(shù),如果達到最大活動連接數(shù),則會等待
maxIdle
否
8
最大空閑連接數(shù),如果超過則將會關閉多余的空閑連接
minIdle
否
0
最小空閑連接數(shù),連接池將保持最小的空閑連接,即使這些連接長久不用
testOnBorrow
否
false
當連接在使用前
testWhileIdle
否
false
是否檢測空閑連接數(shù),這個參數(shù)啟動的時候下列兩個參數(shù)才有效
minEvictableIdleTimeMillis
否
30分鐘
連接空閑數(shù)多少時間將被驅逐(關閉)(time Unit:ms)
timeBetweenEvictionRunsMillis
否
-1
用于關閉空閑連接每間隔多少時間檢查一次空閑連接(time Unit:ms)
3.2.4 配置讀寫數(shù)據(jù)庫name名和ip地址
3.2.5 配置讀寫分離連接池:Amoeba提供讀寫分離pool相關配置。并且提供負載均衡配置。可配置slave1、slave2形成一個虛擬的virtualSlave,該配置提供負載均衡、failOver、故障恢復。
3.3 配置log4j.xml文件
3.3.1 在可用性測試已經(jīng)完成的情況下, 建議將log4j.xml 中關于日志輸出level為info的全部設置成warn或者error級別. 日志是非常消耗系統(tǒng)性能的, 在沒有必要的情況下可以不使用debug.
本人暫時還是測試環(huán)境,所以未修改此項配置
3.4 開啟防火墻amoeba設置的端口,本人amoeba服務器沒有安裝mysql,因此自定義的amoeba是3306端口,開啟如下
[root@amoeba1 ~]#vi /etc/sysconfig/iptables
編輯文件,在
-A INPUT -j REJECT --reject-with icmp-host-prohibited之前
加入
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
重啟防火墻服務
[root@amoeba1 ~]#service iptables restart
四:讀寫分離測試
4.1 測試思路:因為兩個是在兩個主從數(shù)據(jù)庫上實現(xiàn)讀寫分離,向主數(shù)據(jù)庫寫入操作,從數(shù)據(jù)庫通過數(shù)據(jù)庫復制同步主數(shù)據(jù)庫內容,讀取時讀取從數(shù)據(jù)庫數(shù)據(jù),因此,我在主從數(shù)據(jù)庫上建立同樣的表,然后斷開從數(shù)據(jù)庫的復制,在主從數(shù)據(jù)庫分別插入不同數(shù)據(jù),再連接amoeba服務器,寫入第三條語句,此時主數(shù)據(jù)庫有1,3兩條數(shù)據(jù),從數(shù)據(jù)庫只有2一條數(shù)據(jù),證明寫入是master數(shù)據(jù)庫,讀取是slave數(shù)據(jù)庫。
4.1.1 開啟amoeba
[root@amoeba1 ~]#/usr/local/amoeba/bin/amoeba start
4.1.2 在主從數(shù)據(jù)庫復制成功的前提下,在主數(shù)據(jù)庫創(chuàng)建一張測試表,從數(shù)據(jù)庫自動復制此操作
#主數(shù)據(jù)庫,進入mysql
[root@mysql1 ~]#/usr/local/mysql/bin/mysql -u root -p123456
mysql>use zf_db;
mysql>create table test_amoeba(id int);
mysql>select * from test_amoeba;
4.1.3 從數(shù)據(jù)庫查看是否成功創(chuàng)建test_amoeba測試表
#從數(shù)據(jù)庫
[root@mysql2 ~]#/usr/local/mysql/bin/mysql -u root -p123456
mysql>use zf_db;
mysql>select * from test_amoeba;
證明:主從復制成功
4.1.4 從數(shù)據(jù)庫斷開主從數(shù)據(jù)庫復制
#從數(shù)據(jù)庫
mysql>stop slave;
4.1.5 主數(shù)據(jù)庫插入一條語句,數(shù)值1
#主數(shù)據(jù)庫
mysql>INSERT INTO test_amoeba VALUES (1);
mysql>SELECT * FROM test_amoeba;
4.1.6 從數(shù)據(jù)庫查看test_amoeba數(shù)據(jù),因為已經(jīng)斷開復制,所以此時從數(shù)據(jù)庫應該是沒有值的
#從數(shù)據(jù)庫
mysql>select * from test_amoeba;
4.1.7 從數(shù)據(jù)庫在test_amoeba表中插入數(shù)值2
#從數(shù)據(jù)庫
mysql>INSERT INTO test_amoeba VALUES (2);
mysql>select * from test_amoeba;
4.1.8 查詢主數(shù)據(jù)庫test_amoeba數(shù)據(jù)情況
#主數(shù)據(jù)庫
mysql>select * from test_amoeba;
4.1.9 查詢從數(shù)據(jù)test_amoeba數(shù)據(jù)情況
#從數(shù)據(jù)庫
mysql>select * from test_amoeba;
注:此時主從數(shù)據(jù)庫的數(shù)據(jù)是不一致的,以便我們證明amoeba的讀寫分離
4.1.10 使用mysql可視化軟件連接amoeba服務器
連接測試
注:如果沒有成功,請注意amoeba服務器的iptables是否開啟amoeba的端口
4.1.11 查看amoeba數(shù)據(jù)庫顯示內容
4.1.12 在amoeba服務器,向test_amoeba表寫入數(shù)據(jù)
#amoeba服務器
4.1.13 查詢主數(shù)據(jù)test_amoeba數(shù)據(jù)
#主數(shù)據(jù)庫
mysql>select * from test_amoeba;
證明:通過amoeba服務器,我們寫入數(shù)據(jù)進入到主數(shù)據(jù)庫test_amoeba表中
4.1.14 查詢從數(shù)據(jù)庫test_amoeba數(shù)據(jù)
#從數(shù)據(jù)庫
mysql>select * from test_amoeba;
證明:在未開啟主從數(shù)據(jù)庫復制的情況下,向amoeba服務器寫入數(shù)據(jù)時,從數(shù)據(jù)庫未寫入數(shù)據(jù)
4.1.15 連接amoeba服務器,查詢test_amoeba數(shù)據(jù)
只能看到id為2的一條數(shù)據(jù)
證明:我們連接amoeba服務器時,讀的數(shù)據(jù)是slave服務器的數(shù)據(jù),因此,我們主從數(shù)據(jù)庫的讀寫分離成功!
總結
以上是生活随笔為你收集整理的amoeba mysql binary_amoeba安装与实现amoeba for mysql读写分离的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蓝桥杯攻略大全 | 学习路线 | 注意事
- 下一篇: amoeba mysql_从零开始:My