超实用!!MySQL数据库——Amoeba读写分离
生活随笔
收集整理的這篇文章主要介紹了
超实用!!MySQL数据库——Amoeba读写分离
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
- MySQL讀寫分離原理
- 應(yīng)用場景
- 實驗前設(shè)計
- 一 、安裝Amoeba
- 1. 所有服務(wù)器關(guān)閉Firewalld或者進行規(guī)則設(shè)置并搭建Mysql主從復(fù)制環(huán)境
- 搭建主從復(fù)制詳細(xì)步驟鏈接
- 2.Amoeba服務(wù)器環(huán)境安裝
- 補充
- 3.解壓Amoeba
- 4.配置啟動腳本,并設(shè)置為系統(tǒng)服務(wù)
- 二、配置Amoeba讀寫分離,兩個Slave讀負(fù)載均衡
- 1.在master、slave1和slave2中執(zhí)行以下操作開放權(quán)限給Amoeba
- 2.修改amoeba.xml配置文件
- 3.修改代表dbServers.xml配置文件
- 4.啟動Amoeba
- 三、驗證試驗結(jié)果
- 1.首先關(guān)閉主從同步,去兩臺從服務(wù)器slave1和slave2,關(guān)閉同步復(fù)制,否則會影響試驗驗證
- 2.驗證兩臺從服務(wù)器的“只讀”效果
- 2.1在master中插入數(shù)據(jù)
- 2.2在slave1中插入數(shù)據(jù)
- 2.4去客戶機測試
- 3. 驗證master的“只寫”功能
- 3.1首先在客戶機寫入數(shù)據(jù)
- 3.2去master查看表數(shù)據(jù)
- 3.3去slave1和slave2查看表數(shù)據(jù)
MySQL讀寫分離原理
- 讀寫分離就是只在主服務(wù)器上寫,只在從服務(wù)器上讀
- 主數(shù)據(jù)庫處理事務(wù)性查詢,而從數(shù)據(jù)庫處理select查詢
- 數(shù)據(jù)庫復(fù)制被用來把事務(wù)性查詢導(dǎo)致的變更同步到集群中的從數(shù)據(jù)庫
應(yīng)用場景
- 在企業(yè)應(yīng)用中,在大量的數(shù)據(jù)請求下,單臺數(shù)據(jù)庫將無法承擔(dān)所有的讀寫操作
- 配置多臺數(shù)據(jù)庫服務(wù)器以實現(xiàn)讀寫分離
- 在主從復(fù)制的基礎(chǔ)上實現(xiàn)讀寫分離
實驗前設(shè)計
◆ master授權(quán)主從同步賬戶
◆ 所有數(shù)據(jù)庫服務(wù)器授權(quán)供Amoeba訪問的用戶
◆ Amoeba在amoeba.xml文件中授權(quán)客戶端訪問Amoeba的賬戶
一 、安裝Amoeba
1. 所有服務(wù)器關(guān)閉Firewalld或者進行規(guī)則設(shè)置并搭建Mysql主從復(fù)制環(huán)境
搭建主從復(fù)制詳細(xì)步驟鏈接
2.Amoeba服務(wù)器環(huán)境安裝
安裝jdk(因為amoeba是Java寫的,所以我們需要安裝jdk)
[root@localhost opt]# java -version //檢查java版本 -bash: java: command not found //沒有安裝 [root@localhost ~]# ls //查看jdk安裝包 amoeba-mysql-3.0.5-RC-distribution.zip jdk-8u144-linux-x64.tar.gz [root@localhost ~]# tar xzvf jdk-8u144-linux-x64.tar.gz -C /opt [root@localhost ~]# cd /opt [root@localhost opt]# cp -rv jdk1.8.0_144/ /usr/local/java //拷貝到/usr/local/目錄下便于管理 [root@localhost ~]# vi /etc/profile //設(shè)置環(huán)境變量 ……省略部分 export JAVA_HOME=/usr/local/java export JRE_HOME=/usr/local/java/jre export PATH=$PATH:/usr/local/java/bin export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib [root@localhost ~]# java -version java version "1.8.0_144" Java(TM) SE Runtime Environment (build 1.8.0_144-b01) Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)補充
因為Amoeba并不是支持所有的Java環(huán)境,支持1.5和1.6版本和少部分的1.8版本,因此在安裝前需要檢查Java版本,若不合適,則需要卸載重新安裝。以下是卸載辦法
卸載原有的java環(huán)境
3.解壓Amoeba
[root@localhost ~]# yum -y install unzip ##zip格式壓縮包需要unzip程序解壓 [root@localhost ~]# unzip amoeba-mysql-3.0.5-RC-distribution.zip -d /usr/local/ [root@localhost ~]# mv /usr/local/amoeba-mysql-3.0.5-RC/ /usr/local/amoeba [root@localhost ~]# chmod -R 755 /usr/local/amoeba/ ##提權(quán)限 [root@localhost ~]# vi /usr/local/amoeba/jvm.properties JVM_OPTIONS="-server -Xms1024m -Xmx1024m -Xss256k" //將32行JVM_OPTIONS="-server -Xms256m -Xmx1024m -Xss196k -XX:PermSize=16m -XX:MaxPermSize=96m" 進行修改4.配置啟動腳本,并設(shè)置為系統(tǒng)服務(wù)
[root@localhost ~]# vi /etc/init.d/amoeba ##編寫amoeba啟動程序腳本 #!/bin/bash #chkconfig: 35 62 62 # export JAVA_HOME=/usr/local/java export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH NAME=Amoeba AMOEBA_BIN=/usr/local/amoeba/bin/launcher SHUTDOWN_BIN=/usr/local/amoeba/bin/shutdown PIDFILE=/usr/local/amoeba/Amoeba-MySQL.pid SCRIPTNAME=/etc/init.d/amoebacase "$1" in start) echo -n "Starting $NAME... " $AMOEBA_BIN echo " done" ;; stop) echo -n "Stoping $NAME... " $SHUTDOWN_BIN echo " done" ;; restart) $SHUTDOWN_BIN sleep 1 $AMOEBA_BIN ;; *) echo "Usage: $SCRIPTNAME {start|stop|restart}" exit 1 ;; esac [root@localhost ~]# chmod +x /etc/init.d/amoeba [root@localhost ~]# chkconfig --add amoeba [root@localhost ~]# chkconfig --list amoeba ##成功加入啟動項 Note: This output shows SysV services only and does not include nativesystemd services. SysV configuration data might be overridden by nativesystemd configuration.If you want to list systemd services use 'systemctl list-unit-files'.To see services enabled on particular target use'systemctl list-dependencies [target]'.amoeba 0:off 1:off 2:off 3:on 4:off 5:on 6:off [root@localhost ~]# service amoeba start //開啟Amoeba Ctrl+c ##放到后臺 [root@localhost ~]# netstat -anpt | grep 8066 //檢查監(jiān)聽狀況,開啟成功 tcp6 0 0 :::8066 :::* LISTEN 28116/java二、配置Amoeba讀寫分離,兩個Slave讀負(fù)載均衡
1.在master、slave1和slave2中執(zhí)行以下操作開放權(quán)限給Amoeba
[root@localhost ~]# mysql -uroot -p ……省略部分 mysql> create database test; //注意,一定要創(chuàng)建和授權(quán)賬號同名的庫,否則后面會出錯 Query OK, 1 row affected (0.01 sec) mysql> GRANT ALL ON *.* TO test@'20.0.0.%' IDENTIFIED BY 'abc123'; //授權(quán)賬號test,密碼abc123 Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; //重新加載授權(quán) Query OK, 0 rows affected (0.01 sec)2.修改amoeba.xml配置文件
(代碼前面的數(shù)字是對應(yīng)配置文件中的行數(shù),方便查找)
[root@localhost ~]# cd /usr/local/amoeba/conf [root@localhost conf]# vim amoeba.xml28 <property name="user">amoeba</property>29 30 <property name="password">abc123</property>83 <property name="defaultPool">master</property>84 <property name="writePool">master</property> //去掉這里的注釋85 <property name="readPool">slaves</property> //去掉這里的注釋3.修改代表dbServers.xml配置文件
(代碼前面的數(shù)字是對應(yīng)配置文件中的行數(shù),方便查找)
22 <!-- mysql schema -->23 <property name="schema">mysql</property> //amoeba所屬庫名稱,默認(rèn)是test,這里指定的庫名在master數(shù)據(jù)庫中必須要存在,但是5.7版本共數(shù)據(jù)庫是沒有test數(shù)據(jù)庫的,因此使用5.7版本,要么在master中創(chuàng)建一個test庫,要么改成主數(shù)據(jù)庫中已有的庫名,如mysql。24 25 <!-- mysql user -->26 <property name="user">test</property>27 28 <property name="password">abc123</property>43 <dbServer name="master" parent="abstractServer">44 <factoryConfig>45 <!-- mysql ip -->46 <property name="ipAddress">20.0.0.12</property>47 </factoryConfig>48 </dbServer>49 50 <dbServer name="slave1" parent="abstractServer">51 <factoryConfig>52 <!-- mysql ip -->53 <property name="ipAddress">20.0.0.18</property>54 </factoryConfig>55 </dbServer>56 57 <dbServer name="slave2" parent="abstractServer">58 <factoryConfig>59 <!-- mysql ip -->60 <property name="ipAddress">20.0.0.19</property>61 </factoryConfig>62 </dbServer>64 <dbServer name="slaves" virtual="true">65 <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">66 <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->67 <property name="loadbalance">1</property>68 69 <!-- Separated by commas,such as: server1,server2,server1 -->70 <property name="poolNames">slave1,slave2</property>4.啟動Amoeba
[root@localhost ~]# service amoeba restart [root@localhost ~]# netstat -anpt | grep java //端口開啟成功 tcp6 0 0 :::8066 :::* LISTEN 19029/java ……省略部分三、驗證試驗結(jié)果
1.首先關(guān)閉主從同步,去兩臺從服務(wù)器slave1和slave2,關(guān)閉同步復(fù)制,否則會影響試驗驗證
[root@localhost ~]# mysql -uroot -p ……省略部分 mysql> stop slave; Query OK, 0 rows affected (0.00 sec)2.驗證兩臺從服務(wù)器的“只讀”效果
驗證設(shè)計:
在關(guān)閉主從同步的前提下,我們同時在三臺服務(wù)器中寫入數(shù)據(jù),客戶機應(yīng)當(dāng)只讀兩臺從服務(wù)器slave的數(shù)據(jù),而不會讀取master中的數(shù)據(jù)
2.1在master中插入數(shù)據(jù)
mysql> use test; mysql> create table list (name char(16),id char(16)); Query OK, 0 rows affected (0.00 sec) mysql> insert into list values('master',1); Query OK, 1 row affected (0.00 sec) mysql> select * from list; +-------------+------+ | name | id | +--------+------+ | master | 1 | +--------+------+ 1 row in set (0.00 sec)2.2在slave1中插入數(shù)據(jù)
mysql> use test; mysql> create table list (name char(16),id char(16)); Query OK, 0 rows affected (0.00 sec) mysql> insert into list values('slave1',2); Query OK, 1 row affected (0.00 sec) mysql> select * from list; +--------+------+ | name | id | +--------+------+ | slave1 | 2 | +--------+------+ 1 row in set (0.00 sec) (2.3)在slave2中插入數(shù)據(jù) mysql> use test; mysql> create table list (name char(16),id char(16)); Query OK, 0 rows affected (0.00 sec) mysql> insert into list values('slave2',3); Query OK, 1 row affected (0.00 sec) mysql> select * from list; +--------+------+ | name | id | +--------+------+ | slave2 | 3 | +--------+------+2.4去客戶機測試
MySQL [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | bbs | | myadm | | mysql | | performance_schema | | sys | | test | +--------------------+ 7 rows in set (0.01 sec)MySQL [(none)]> use test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -ADatabase changed MySQL [test]> select * from list; +--------+------+ | name | id | +--------+------+ | slave1 | 2 | +--------+------+ 1 row in set (0.01 sec)MySQL [test]> select * from list; +--------+------+ | name | id | +--------+------+ | slave2 | 3 | +--------+------+ 1 row in set (0.01 sec)3. 驗證master的“只寫”功能
驗證設(shè)計:
在客戶端寫入數(shù)據(jù),應(yīng)該只是在master服務(wù)器中寫入數(shù)據(jù),在沒有主從同步的前提下,從服務(wù)器應(yīng)該是沒有客戶機寫入的數(shù)據(jù)的。
3.1首先在客戶機寫入數(shù)據(jù)
MySQL [test]> create table zhang1 (id int(10),name varchar(10),address varchar(20)); Query OK, 0 rows affected (0.03 sec) MySQL [test]> insert into zhang1 values('4','zhang','write_test'); Query OK, 1 row affected (0.04 sec)3.2去master查看表數(shù)據(jù)
mysql> select * from zhang1; +------+-------+------------+ | id | name | address | +------+-------+------------+ | 4 | zhang | write_test | +------+-------+------------+ 1 row in set (0.00 sec)3.3去slave1和slave2查看表數(shù)據(jù)
mysql> select * from zhang1; ERROR 1146 (42S02): Table 'test.zhang1' doesn't exist總結(jié)
以上是生活随笔為你收集整理的超实用!!MySQL数据库——Amoeba读写分离的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PAYPAL使用虚拟卡的会有优势吗?
- 下一篇: linux cmake编译源码,linu