您好,登錄后才能下訂單哦!
一.MySQL讀寫分離
主數(shù)據(jù)庫處理事務(wù)性查詢,從數(shù)據(jù)庫處理select查詢。數(shù)據(jù)庫復(fù)制用來把事務(wù)性查詢導(dǎo)致的變更同步到從數(shù)據(jù)庫中。
二.最為常見的讀寫分離有兩種:
1.基于程序代碼實(shí)現(xiàn)
在代碼中根據(jù)select,insert進(jìn)程路由分類;
優(yōu)點(diǎn):性能好,因?yàn)樵诖a中實(shí)現(xiàn),不需要額外的硬件設(shè)備;
缺點(diǎn):需要開發(fā)人員來實(shí)現(xiàn),對代碼改動(dòng)比較大,不適合大型復(fù)雜應(yīng)用;
2.基于代理層實(shí)現(xiàn)
MySQL-Proxy:隸屬于MySQL開源項(xiàng)目;
Amoeba:Amoeba是一個(gè)以MySQL為底層數(shù)據(jù)存儲(chǔ),并對應(yīng)用提供MySQL協(xié)議接口的proxy。它集中地響應(yīng)應(yīng)用的請求,依據(jù)用戶事先設(shè)置的規(guī)則,將SQL請求發(fā)送到特定的數(shù)據(jù)庫上執(zhí)行?;诖丝梢詫?shí)現(xiàn)負(fù)載均衡、讀寫分離、高可用性等需求;
三.實(shí)驗(yàn)環(huán)境
OS:CentOS 6.5 x64
Amoeba:192.168.0.128
master:192.168.0.134
slave:192.168.0.135
client:192.168.0.137
三.配置主從復(fù)制
1.MySQL讀寫分離是基于主從復(fù)制配置的,先配置好主從復(fù)制,再配置讀寫分離。
主從復(fù)制博文連接:http://guoxh.blog.51cto.com/10976315/1922643
2.添加授權(quán)賬號(hào)供Amoeba訪問
master和slave:
mysql> grant all on *.* to 'proxy'@'192.168.0.128' identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
四.Amoeba安裝配置
1. Amoeba是基于JDK開發(fā)的,所有先安裝JAVA環(huán)境
[root@amoeba ~]# chmod +x jdk-6u14-linux-x64.bin [root@amoeba ~]# ./jdk-6u14-linux-x64.bin [root@amoeba ~]# cat /etc/profile.d/java.sh export JAVA_HOME=/usr/local/jdk1.6 export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/lib:$PATH:$HOME/bin [root@amoeba ~]# source /etc/profile.d/java.sh [root@amoeba ~]# java -version java version "1.6.0_14" Java(TM) SE Runtime Environment (build 1.6.0_14-b08) Java HotSpot(TM) 64-Bit Server VM (build 14.0-b16, mixed mode) #JAVA環(huán)境配置完成
2.安裝Amoeba
[root@amoeba ~]# mkdir /usr/local/amoeba [root@amoeba ~]# tar xf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba [root@amoeba ~]# ls /usr/local/amoeba/ benchmark bin changelogs.txt conf lib LICENSE.txt README.html [root@amoeba ~]# cat /etc/profile.d/amoeba.sh export AMOEBA_HOME=/usr/local/amoeba/ export PATH=$PATH:$AMOEBA_HOME/bin [root@amoeba ~]# source /etc/profile.d/amoeba.sh [root@amoeba ~]# /usr/local/amoeba/bin/amoeba amoeba start|stop # Amoeba安裝成功
3.編輯amoeba.xml配置文件
[root@amoeba conf]# cd /usr/local/amoeba/conf/ [root@amoeba conf]# cp amoeba.xml amoeba$(date +"%Y_%m_%d").xml #修改前備份一份 [root@amoeba conf]# vim amoeba.xml 27 <property name="authenticator"> 28 <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator"> 29 30 <property name="user">amoeba</property> #設(shè)置client連接的用戶 31 32 <property name="password">amoeba</property> #設(shè)置client連接的密碼 33 34 <property name="filter"> 35 <bean class="com.meidusa.amoeba.server.IPAccessController"> 36 <property name="ipFile">${amoeba.home}/conf/access_list.conf</property> 37 </bean> 38 </property> 39 </bean> 40 </property> 112 </property> 113 <property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property> 114 <property name="LRUMapSize">1500</property> 115 <property name="defaultPool">master/property> #指定默認(rèn)為master 116 117 <!-- #刪除注釋 118 <property name="writePool">master</property> #指定寫操作為master 119 <property name="readPool">slaves</property> #指定讀操作為slave 120 --> #刪除注釋 121 <property name="needParse">true</property>
3.編輯dbServers.xml
25 <!-- mysql user --> 26 <property name="user">proxy</property> #填mysql授權(quán)的用戶 27 28 <!-- mysql password --> 29 <property name="password">123456</property> #填MySQL授權(quán)的用戶密碼 30 45 <dbServer name="master" parent="abstractServer"> 46 <factoryConfig> 47 <!-- mysql ip --> 48 <property name="ipAddress">192.168.0.134</property> #指定master的IP 49 </factoryConfig> 50 </dbServer> 51 52 <dbServer name="slave" parent="abstractServer"> 53 <factoryConfig> 54 <!-- mysql ip --> 55 <property name="ipAddress">192.168.0.135</property> #指定slave的IP 56 </factoryConfig> 57 </dbServer> 58 <dbServer name="slaves" virtual="true"> 59 <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool"> 60 <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA--> 61 <property name="loadbalance">1</property> 62 63 <!-- Separated by commas,such as: server1,server2,server1 --> 64 <property name="poolNames">slave</property> 65 </poolConfig> 66 </dbServer>
4.啟動(dòng)服務(wù)
[root@amoeba ~]# amoeba start & [1] 2666 [root@amoeba ~]# log4j:WARN log4j config load completed from file:/usr/local/amoeba/conf/log4j.xml 2017-05-07 00:42:16,692 INFO context.MysqlRuntimeContext - Amoeba for Mysql current versoin=5.1.45-mysql-amoeba-proxy-2.2.0 log4j:WARN ip access config load completed from file:/usr/local/amoeba/conf/access_list.conf 2017-05-07 00:42:17,064 INFO net.ServerableConnectionManager - Amoeba for Mysql listening on 0.0.0.0/0.0.0.0:8066. 2017-05-07 00:42:17,067 INFO net.ServerableConnectionManager - Amoeba Monitor Server listening on /127.0.0.1:56460. [root@amoeba ~]# netstat -anptl | grep java tcp 0 0 ::ffff:127.0.0.1:56460 :::* LISTEN 2666/java tcp 0 0 :::8066 :::* LISTEN 2666/java tcp 0 0 ::ffff:192.168.0.128:37361 ::ffff:192.168.0.135:3306 ESTABLISHED 2666/java tcp 0 0 ::ffff:192.168.0.128:35241 ::ffff:192.168.0.134:3306 ESTABLISHED 2666/java #Amoeba默認(rèn)端口為8066
五.測試
1.客戶端連接Amoeba
[root@client ~]# mysql -uamoeba -p123456 -h 192.168.0.128 -P8066
2.新建一個(gè)數(shù)據(jù)庫
mysql> create database guoxh; Query OK, 1 row affected (0.01 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | aaa | | guoxh | | mysql | | test | +--------------------+ 5 rows in set (0.01 sec)
3.master和slave查看數(shù)據(jù)庫
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | aaa | | guoxh | #剛建的數(shù)據(jù)庫已經(jīng)同步 | mysql | | test | +--------------------+ 5 rows in set (0.00 sec)
五.測試讀寫分離:
1.在master新建一個(gè)表,同步到slave上,然后關(guān)掉slave功能,在slave上插入?yún)^(qū)別語句
master:創(chuàng)建名為student的表
mysql> show tables; Empty set (0.00 sec) mysql> create table student (id int(10),name varchar(10),info varchar(50)); Query OK, 0 rows affected (0.01 sec) mysql> show tables; +-----------------+ | Tables_in_guoxh | +-----------------+ | student | +-----------------+ 1 row in set (0.00 sec)
slave:停止同步
mysql> show tables; +-----------------+ | Tables_in_guoxh | +-----------------+ | student | +-----------------+ 1 row in set (0.00 sec) mysql> stop slave; Query OK, 0 rows affected (0.00 sec)
master:插入?yún)^(qū)別數(shù)據(jù)
mysql> insert into student values('1','zhangsan','Mysql_master'); Query OK, 1 row affected (0.01 sec) mysql> select * from student; +------+----------+--------------+ | id | name | info | +------+----------+--------------+ | 1 | zhangsan | Mysql_master | +------+----------+--------------+ 1 row in set (0.00 sec)
slave:插入?yún)^(qū)別數(shù)據(jù)
mysql> select * from student; Empty set (0.00 sec) mysql> insert into student values('2','lisi','Mysql_slave'); Query OK, 1 row affected (0.00 sec) mysql> select * from student; +------+------+-------------+ | id | name | info | +------+------+-------------+ | 2 | lisi | Mysql_slave | +------+------+-------------+ 1 row in set (0.00 sec)
3.測試讀操作:
client:
mysql> show tables; +-----------------+ | Tables_in_guoxh | +-----------------+ | student | +-----------------+ 1 row in set (0.00 sec) mysql> select * from student; +------+------+-------------+ | id | name | info | +------+------+-------------+ | 2 | lisi | Mysql_slave | +------+------+-------------+ 1 row in set (0.00 sec) mysql> select * from student; +------+------+-------------+ | id | name | info | +------+------+-------------+ | 2 | lisi | Mysql_slave | +------+------+-------------+ 1 row in set (0.00 sec)
# 此時(shí),讀操作已經(jīng)已經(jīng)全部分配到slave上面。
4.測試寫操作
client:插入一條數(shù)據(jù),這里是看不到數(shù)據(jù)的,得到master查看
mysql> insert into student values('3','wangwu','Mysql_client'); Query OK, 1 row affected (0.00 sec) mysql> select * from student; +------+------+-------------+ | id | name | info | +------+------+-------------+ | 2 | lisi | Mysql_slave | +------+------+-------------+ 1 row in set (0.00 sec)
master:
mysql> select * from student; +------+----------+--------------+ | id | name | info | +------+----------+--------------+ | 1 | zhangsan | Mysql_master | | 3 | wangwu | Mysql_client | +------+----------+--------------+ 2 rows in set (0.00 sec)
#此時(shí),寫操作全部分給了master。
到此為止,MySQL已經(jīng)實(shí)現(xiàn)了讀寫分離!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。