mysql基于amoeba读写分离
1:基于Amoeba 讀寫(xiě)分離
?
?????? 在實(shí)際的生產(chǎn)環(huán)境中,如果對(duì)數(shù)據(jù)庫(kù)的讀和寫(xiě)都在同一個(gè)數(shù)據(jù)庫(kù)服務(wù)器中操作,無(wú)論是安全性,高可用還是并發(fā)等各個(gè)方面都不能完全滿足實(shí)際需求的,因此一般來(lái)說(shuō)都是通過(guò)主從復(fù)制的方式來(lái)同步數(shù)據(jù),再通過(guò)讀寫(xiě)分離來(lái)提供數(shù)據(jù)的高并發(fā)負(fù)載能力這樣的方案來(lái)進(jìn)行部署。
??????? 簡(jiǎn)單來(lái)說(shuō),讀寫(xiě)分離就是只在主服務(wù)器上寫(xiě),只在從服務(wù)器上讀,基本的原理是讓主數(shù)據(jù)庫(kù)處理事務(wù)性查詢,而從數(shù)據(jù)庫(kù)處理select查詢,數(shù)據(jù)庫(kù)復(fù)制被用來(lái)把事務(wù)性查詢導(dǎo)致的改變更新同步到集群中的從數(shù)據(jù)庫(kù)。
(1):目前最常見(jiàn)的MySQL讀寫(xiě)分離方案有兩種:
<1>:基于程序代碼內(nèi)部實(shí)現(xiàn)
在代碼中根據(jù)select,insert進(jìn)行路由分類,這類方法也是目前大型生產(chǎn)環(huán)境應(yīng)用最廣泛的,優(yōu)點(diǎn)是性能最好,因?yàn)樵诔绦虼a中實(shí)現(xiàn),不需要增加額外的設(shè)備作為硬件開(kāi)支,缺點(diǎn)是需要開(kāi)發(fā)人員來(lái)實(shí)現(xiàn),運(yùn)維人員無(wú)從下手
<2>:基于中間代理層實(shí)現(xiàn)
代理一般位于客戶端和數(shù)據(jù)庫(kù)服務(wù)器之間,代理服務(wù)器接到客戶端請(qǐng)求后通過(guò)判斷轉(zhuǎn)發(fā)到后端數(shù)據(jù)庫(kù),代表性程序:
《1》:mysql-proxy為mysql開(kāi)發(fā)早期開(kāi)源項(xiàng)目,通過(guò)其自帶的lua腳本進(jìn)行SQL判斷,雖然是mysql的官方產(chǎn)品,但是mysql官方不建議將其應(yīng)用到生產(chǎn)環(huán)境。
《2》:Amoeba(變形蟲(chóng))該程序由java語(yǔ)言及逆行開(kāi)發(fā),阿里巴巴將其應(yīng)用于生產(chǎn)環(huán)境,它不支持事物和存儲(chǔ)過(guò)程。
?????? 通過(guò)程序代碼實(shí)現(xiàn)mysql讀寫(xiě)分離自然是一個(gè)不錯(cuò)的選擇,但是并不是所有的應(yīng)用都適合在程序代碼中實(shí)現(xiàn)讀寫(xiě)分離,像一些大型復(fù)雜的Java應(yīng)用,如果在程序代碼中實(shí)現(xiàn)讀寫(xiě)分離對(duì)時(shí)代改動(dòng)就較大,像這種應(yīng)用一般會(huì)考慮使用代理層來(lái)實(shí)現(xiàn)。
?MySQL master 192.168.100.10
MySQL?? slave?? 192.168.100.20
MySQL?? slave?? 192.168.100.30
MySQL amoeba? 192.168.100.40
MySQL client???? 192.168.100.50
?????? Amoeba(變形蟲(chóng))項(xiàng)目開(kāi)源框架于2008年發(fā)布一款A(yù)moeba for mysql軟件,這個(gè)軟件致力于mysql的分布式數(shù)據(jù)庫(kù)前端代理層,主要為應(yīng)用層訪問(wèn)mysql的時(shí)候充當(dāng)SQL路由功能,并具有負(fù)載均衡,高可用性,SQL過(guò)濾,讀寫(xiě)分離,可路由到相關(guān)的目標(biāo)數(shù)據(jù)庫(kù),可并發(fā)請(qǐng)求多臺(tái)數(shù)據(jù)庫(kù),通過(guò)Amoeba能夠完成多數(shù)據(jù)源的高可用,負(fù)載均衡,數(shù)據(jù)切片的功能,目前Amoeba已經(jīng)在很多企業(yè)的生產(chǎn)線上使用。
部署:
(2):在主機(jī)amoeba 上安裝JDK環(huán)境
《1》:rz? 上傳JDK軟件包,amoeba 軟件包,epel? 軟件包
《2》:chmod +x jdk****? 加權(quán)限
《3》:./jdk-6u31-linux-x64.bin? 執(zhí)行
《4》:mv jdk1.6.0_31 /usr/local/jdk1.6?? 移動(dòng)
《5》:vim /etc/profile? 編輯文件
????? 《6》:添加:
export JAVA_HOME=/usr/local/jdk1.6
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$PATH:$JAVA_HOME/lib:$JAVA_HOME/jre/bin:$HOME/bin
export AMOEBA_HOME=/usr/local/amoeba
export PATH=$PATH:$AMOEBA_HOME/bin???????? 提前給amoeba 配置
《7》:rm -rf /usr/bin/java? 刪除
《8》:source /etc/profile? 生效
?《9》:java -version? 查看版本
(2): 安裝amoeba?
《1》:mkdir /usr/local/amoeba? 創(chuàng)建解壓文件
《2》:tar? xf? amoeba??? ….. -C /usr/local/amoeba
《3》: chmod -R 755 /usr/local/amoeba? 加權(quán)限
(3):配置amoeba 讀寫(xiě)分離,兩個(gè)slave 讀負(fù)載均衡
? 在 master ,slave1 ,slave2 服務(wù)中配置amoeba 的訪問(wèn)授權(quán)
《1》:grant all on *.* to 'test'@'192.168.100.%' identified by '123.com';
《2》:flush privileges;?
《3》:cd /usr/local/amoeba/conf? 切換目錄
《4》:vim amoeba.xml? 編輯文件
?? 修改:
30? ? ? ? ? ? ? ? ? <property name="user">amoeba</property>
32? ? ? ? ? ? ? ? ? ? <property name="password">123456</property>
115???????????????? <property name="defaultPool">master</property>
118???????????????? <property name="writePool">master</property>
119???????????????? <property name="readPool">slaves</property>
《5》:vim dbServers.sml? 編輯
?<!-- mysql port -->
??????????????????????? <property name="port">3306</property>
??????????????????????? <!-- mysql schema -->
??????????????????????? <property name="schema">test</property>
??????????????????????? <!-- mysql user -->
??????????????????????? <property name="user">test</property>
??????????????????????? <!--? mysql password -->
??????????????????????? <property name="password">123.com</property>
<dbServer name="master"? parent="abstractServer">
??????????????? <factoryConfig>
??????????????????????? <!-- mysql ip -->
??????????????????????? <property name="ipAddress">192.168.200.111</property>
??????????????? </factoryConfig>
??????? </dbServer>
??????? <dbServer name="slave1"? parent="abstractServer">
??????????????? <factoryConfig>
??????????????????????? <!-- mysql ip -->
??????????????????????? <property name="ipAddress">192.168.200.112</property>
??????????????? </factoryConfig>
??????? </dbServer>
??????? <dbServer name="slave2"? parent="abstractServer">
??????????????? <factoryConfig>
??????????????????????? <!-- mysql ip -->
??????????????????????? <property name="ipAddress">192.168.200.113</property>
??????????????? </factoryConfig>
??????? </dbServer>
??????? <dbServer name="slaves" 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">slave1,slave2</property>
??????????????? </poolConfig>
??????? </dbServer>
《6》:/usr/local/amoeba/bin/amoeba start &? 啟動(dòng)
《7》:netstat -lnpt |grep 8066? 查看端口
(4):安裝client 客戶端
《1》:yum -y installl mariadb mariadb-devel mariadb-server? 安裝
《2》:訪問(wèn)amoeba
在MySQL主服務(wù)器上創(chuàng)建一個(gè)表,會(huì)自動(dòng)同步到各個(gè)從服務(wù)器上,然后關(guān)掉各個(gè)服務(wù)器上的Slave功能,在分別插入語(yǔ)句測(cè)試。
主服務(wù)器
MariaDB [(none)]>? create database Rich;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use Rich;
Database changed
MariaDB [Rich]> create table student (id int(10),name varchar(10),address varchar(20));
Query OK, 0 rows affected (0.00 sec)
分別在兩臺(tái)從服務(wù)器上
MariaDB [(none)]> show databases;
+--------------------+
| Database?????????? |
+--------------------+
| information_schema |
| Rich?????????????? |
| mysql????????????? |
| performance_schema |
| sampdb???????????? |
| test?????????????? |
+--------------------+
6 rows in set (0.00 sec)
MariaDB [(none)]> use Rich;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [Rich]> show tables;
+----------------+
| Tables_in_Rich |
+----------------+
| student??????? |
+----------------+
1 row in set (0.00 sec)
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.01 sec)
Master
MariaDB [(none)]> use Rich;
MariaDB [Rich]> insert into student values('1','Rich','this_is_master');
Query OK, 1 row affected (0.00 sec)
Slave1
MariaDB [(none)]> use Rich;
MariaDB [Rich]> insert into student values('2','Rich','this_is_slave1');
Query OK, 1 row affected (0.00 sec)
Slave2
MariaDB [(none)]> use Rich;
MariaDB [Rich]> insert into student values('3','Rich','this_is_slave2');
Query OK, 1 row affected (0.00 sec)
操作測(cè)試:
在測(cè)試機(jī)上第1次查詢結(jié)果
MySQL [Rich]> select * from student;
+------+------+----------------+
| id?? | name | address??????? |
+------+------+----------------+
|??? 2 | Rich | this_is_slave1 |
+------+------+----------------+
1 row in set (0.00 sec)
在測(cè)試機(jī)上第2次查詢結(jié)果
MySQL [Rich] > select * from student;
+------+------+----------------+
| id?? | name | address??????? |
+------+------+----------------+
|??? 3 | Rich | this_is_slave2 |
+------+------+----------------+
1 row in set (0.02 sec)
在測(cè)試機(jī)上第3次查詢結(jié)果
MySQL [Rich]> select * from student;
+------+------+----------------+
| id?? | name | address??????? |
+------+------+----------------+
|??? 2 | Rich | this_is_slave1 |
+------+------+----------------+
1 row in set (0.03 sec)
測(cè)試寫(xiě)操作:
在Client上插入一條語(yǔ)句:
MySQL [Rich]> insert into student values('4','Rich','write_test');
Query OK, 1 row affected (0.01 sec)
MySQL [Rich]> select * from student;
+------+------+----------------+
| id?? | name | address??????? |
+------+------+----------------+
|??? 3 | Rich | this_is_slave2 |
+------+------+----------------+
1 row in set (0.01 sec)
MySQL [Rich]> select * from student;
+------+------+----------------+
| id?? | name | address??????? |
+------+------+----------------+
|??? 2 | Rich | this_is_slave1 |
+------+------+----------------+
1 row in set (0.00 sec)
但在Client上查詢不到,最終只有在Master上才能看到這條語(yǔ)句內(nèi)容,說(shuō)明寫(xiě)操作在master服務(wù)器上
MariaDB [Rich]> select * from student;
+------+------+----------------+
| id?? | name | address??????? |
+------+------+----------------+
|??? 1 | Rich | this_is_master |
|??? 4 | Rich | write_test???? |
+------+------+----------------+
3 rows in set (0.00 sec)
?? 由此驗(yàn)證,已經(jīng)實(shí)現(xiàn)了MySQL讀寫(xiě)分離,目前所有的寫(xiě)操作都在Master主服務(wù)器上,用來(lái)避免數(shù)據(jù)的不同步,所有的讀操作都平分給了Slave從服務(wù)器,用來(lái)分擔(dān)數(shù)據(jù)庫(kù)壓力。
分別在兩臺(tái)從服務(wù)器上啟用slave功能
MariaDB [Rich]> start slave;
Query OK, 0 rows affected (0.00 sec)
現(xiàn)在在Client測(cè)試機(jī)上查看
MySQL [Rich]> select * from student;
+------+------+----------------+
| id?? | name | address??????? |
+------+------+----------------+
|??? 3 | Rich | this_is_slave2 |
|??? 1 | Rich | this_is_master |
|??? 4 | Rich | write_test???? |
+------+------+----------------+
4 rows in set (0.00 sec)
總結(jié)
以上是生活随笔為你收集整理的mysql基于amoeba读写分离的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [MapReduce] Counter
- 下一篇: .Net与DirectX结合制作泡泡屏保