Mysql5.6主從復制
1、特性分析說明:
mysql 5.6支持多線程復制的機制並且mysql 5.6還引用了GTID的概念,使得其復制功能的配置、監控及管理變得更加易於實現,且更加健壯。
TID:事務的ID號:也就是說在mysql復制中每一個事務都有自己的ID號(隨機數)
GTID:全局事務ID,在整個事務架構中每一個事務ID號是唯一的,不止是在一個節點上而是整個主從復制架構中每任何兩個事務的ID號都不會相同的,這就是全局事務ID。
全局事務ID是怎麼生成的?簡單來講是由mysql服務器自動管理的,在mysql5.6以後每一個mysql服務器都有一個全局唯一的ID號叫做uuid,而GTID就是由當前節點的UUID(一個128位的隨機數)和為當前節點生成的隨機數(TID)組成的,因此只要UUID不同再在此基礎上保證事務ID不同就保證全局不一樣了。
全局事務ID有何用處?簡單來講GTID能夠保證讓一個從服務器到其他的從服務器哪裡實現數據復制而且能夠實現數據整合的。GTID在分布式架構中可以保證數據的一致性。從而也實現了mysql的高可用性。
GTID相關操作:默認情況下將一個事務記錄進二進制文件時將首先記錄它的GTID而且GTID和事務相關信息一並要發送給從服務器由從服務器在在本地應用認證但是絕對不會改變原來的事務ID號。
因此在GTID的架構上就算有了N層架構,復制是N級架構、事務ID依然不會改變;有效的保證了數據的完整和安全性。
小拓展:
用於提升主從架構效率的第三方工具:
mysqlreplicate:用於實現快速啟動從服務器的,就是若原來的主節點A出現故障了原來的從節點B會轉換成主節點,而原來的從節點C會轉移到主節點B上進行復制但是其中的二進制日志的內容有可能不同,所以這個軟件會自己檢測那些應用過哪些未應用然後在本地應用最後實現快速啟動。
mysqlrplcheck:用來校驗mysql主從復制架構中所有節點上數據是否一致、某些節點是否啟動二進制日志的等等也就是說在將某個服務器提升為主節點時實現快速檢測的功能的
mysqlrplshow:用來顯示拓撲架構的,當前服務器中某一個從服務器是一個主服務器還是一個從服務器等等從而實現快速顯示。
mysqlfailover:故障轉移,快速的將一個從服務器提升為一個主服務器,可以手動提升也可以自動提升,在提升之前盡量先校驗一下數據。
mysqlrpladmin:快速一個從服務器切換為主將原來主服務器下線進行維護備份操作。
2、配置基於GTID主從復制:
I、環境介紹:
主節點:[root@node1 ~]:172.16.18.1
從節點:[root@node2 ~]: 172.16.18.2
II、安裝mysql
#############主節點安裝mysql:###########
[root@node1 ~]#tar xf mysql-5.6.13-linux-glibc2.5-x86_64.tar.gz -C /usr/local
[root@node1 ~]#cd /usr/local
[root@node1 ~]#ln -sv mysql-5.6.13-linux-glibc2.5-x86_64 mysql
[root@node1 ~]#cd mysql
[root@node1 ~]#useradd mysql -r
[root@node1 ~]#chown root.mysql *
[root@node1 ~]#mkdir /mydata/data -pv
[root@node1 ~]#chown mysql.mysql /mydata/data
[root@node1 ~]#scp /etc/my.cnf 172.16.18.1:/etc/my.cnf #由於mysql5.6上配置文件需要自動書寫,但是為了節約時間筆者從安裝了mysql-5.5.33的服務器上復制了。
[root@node1 ~]#scripts/mysql_install_db --user=mysql --datadir=/mydata/data
[root@node1 ~]#cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@node1 ~]#chmod +x /etc/rc.d/init.d/mysqld
[root@node1 ~]#vim /etc/my.cnf
datadir = /mydata/data
[root@node1 ~]#vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
[root@node1 ~]#. /etc/profile.d/mysql.sh
[root@node1 ~]#chkconfig --add mysqld
[root@node1 ~]#service mysqld start
#############從節點:###################
[root@node2 ~]#tar xf mysql-5.6.13-linux-glibc2.5-x86_64.tar.gz -C /usr/local
[root@node2 ~]#cd /usr/local
[root@node2 ~]#ln -sv mysql-5.6.13-linux-glibc2.5-x86_64 mysql
[root@node2 ~]#cd mysql
[root@node2 ~]#useradd mysql -r
[root@node2 ~]#chown root.mysql *
[root@node2 ~]#mkdir /mydata/data -pv
[root@node2 ~]#chown mysql.mysql /mydata/data
[root@node2 ~]#scripts/mysql_install_db --user=mysql --datadir=/mydata/data
[root@node2 ~]#cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@node2 ~]#chmod +x /etc/rc.d/init.d/mysqld
[root@node2 ~]#vim /etc/my.cnf
datadir = /mydata/data
[root@node2 ~]#vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
[root@node2 ~]#. /etc/profile.d/mysql.sh
[root@node2 ~]#chkconfig --add mysqld
[root@node2 ~]#service mysqld start
III、配置讀寫分離:
(1)、主節點:
[root@node1 ~]#vim /etc/my.cnf
[mysqld]
binlog_format=row #原配置文件中存在需改動
server-id = 1 #原配置文件中存在
log-slave-updates=true #添加以下這些選項
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=4
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
report-port=3306
report-host=node1.magedu.com
###########保存退出重啟##########
選項解析:
binlog-format:二進制日志的格式,有row、statement和mixed三種類型;需要注意的是:當設置隔離級別為READ-COMMITED必須設置二進制日志格式為ROW,現在MySQL官方認為STATEMENT這個已經不再適合繼續使用;但mixed類型在默認的事務隔離級別下,可能會導致主從數據不一致;
log-bin:啟用二進制日志;
server-id:同一個復制拓撲中的所有服務器的id號必須惟一;
log-slave-updates:slave更新時是否記錄到日志中;
gtid-mode:指定GTID的類型,否則就是普通的復制架構
enforce-gtid-consistency:是否強制GTID的一致性
report-port:產生復制報告時在哪個端口上提供相關功能
report-host:產生復制報告時在哪個主機上提供相關功能,一般為自己的主機名
master-info-repository和relay-log-info-repository:啟用此兩項,可用於實現在崩潰時保證二進制及從服務器安全的功能;
sync-master-info:啟用之可確保服務器崩潰時無信息丟失;
slave-paralles-workers:設定從服務器啟動幾個SQL復制線程數;0表示關閉多線程復制功能;數字太大也無意義最好與要復制的數據庫的數目相同
binlog-checksum:復制時是否校驗二進制文件的完整性等相關功能;binlog的校驗格式校驗算法(CRC32:循環冗余校驗碼32位)
master-verify-checksum:檢驗主服務器二進制日志的相關功能
slave-sql-verify-checksum:校驗從服務器中繼日志的相關功能的
binlog-rows-query-log-events:啟用之可用於在二進制日志詳細記錄事件相關的信息,可降低故障排除的復雜度;
##########復制配置文件至從節點######
[root@node1 ~]#scp /etc/my.cnf 172.16.18.2:/etc/my.cnf
##########創建復制用戶##############
mysql> GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO rpuser@'%' IDENTIFIED BY 'rppass';
mysql> FLUSH PRIVILEGES;
##########查看UUID##################
mysql> show global variables like '%UUID%';
+---------------+--------------------------------------+
| Variable_name | Value |
+---------------+--------------------------------------+
| server_uuid | 5f9e071b-3145-11e3-8426-000c2906a649 |
+---------------+--------------------------------------+
(2)、從節點:
############修改配置文件##########
[root@node2 ~]#vim /etc/my.cnf
server-id = 10
report-host=node2.magedu.com
保存退出重啟
###########連接主服務器###########
mysql> CHANGE MASTER TO MASTER_HOST='172.16.18.1', MASTER_USER='rpuser', MASTER_PASSWORD='rppass', MASTER_AUTO_POSITION=1;
###########啟動從服務器##########
mysql> start slave;
##########查看復制連接###########
mysql> show slave statusG
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.18.1
Master_User: rpuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 546
Relay_Log_File: node2-relay-bin.000002
Relay_Log_Pos: 756
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 546
Relay_Log_Space: 960
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 5f9e071b-3145-11e3-8426-000c2906a649
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 5f9e071b-3145-11e3-8426-000c2906a649:1-2
Executed_Gtid_Set: 5f9e071b-3145-11e3-8426-000c2906a649:1-2
Auto_Position: 1
IV、查看基於GTID復制連接狀況:
###########主節點:################
mysql> show processlist;
+----+--------+-------------------+------+------------------+------+-----------------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+--------+-------------------+------+------------------+------+-----------------------------------------------------------------------+------------------+
| 4 | root | localhost | NULL | Query | 0 | init | show processlist |
| 5 | rpuser | 172.16.18.2:40574 | NULL | Binlog Dump GTID | 1201 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL |
+----+--------+-------------------+------+------------------+------+-----------------------------------------------------------------------+------------------+
#############從節點:#################
mysql> show processlist;
+----+-------------+-----------+------+---------+-------+-----------------------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+-----------+------+---------+-------+-----------------------------------------------------------------------------+------------------+
| 1 | root | localhost | NULL | Sleep | 45572 | | NULL |
| 8 | root | localhost | NULL | Query | 0 | init | show processlist |
| 9 | system user | | NULL | Connect | 1394 | Waiting for master to send event | NULL |
| 10 | system user | | NULL | Connect | 1392 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL |
| 11 | system user | | NULL | Connect | 1394 | Waiting for an event from Coordinator | NULL |
| 12 | system user | | NULL | Connect | 1394 | Waiting for an event from Coordinator | NULL |
| 13 | system user | | NULL | Connect | 1394 | Waiting for an event from Coordinator | NULL |
| 14 | system user | | NULL | Connect | 1433 | Waiting for an event from Coordinator | NULL |
+----+-------------+-----------+------+---------+-------+-----------------------------------------------------------------------------+------------------+
V、測試:
###########主節點:#############
mysql> create database hellodb;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
###########從節點:#############
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
至此基於GTID的主從復制配置就完成了,關於單點故障模擬實現轉移這裡就不演示了,若有需要請參考:http://dev.mysql.com/doc/workbench/en/mysqlfailover.html
--------------------------------------------------------------------------------
利用amoeba實現mysql讀寫分離
1、基本知識概述:
Amoeba(變形蟲):該開源框架於2008年 開始發布一款 Amoeba for Mysql軟件。這個軟件致力於MySQL的分布式數據庫前端代理層,它主要在應用層訪問MySQL的 時候充當SQL路由功能,專注於分布式數據庫代理層(Database Proxy)開發。座落與 Client、DB Server(s)之間,對客戶端透明。具有負載均衡、高可用性、SQL 過濾、讀寫分離、可路由相關的到目標數據庫、可並發請求多台數據庫合並結果。它運行於JVM上,是淘寶公司前期的一個工程師研發的。國人也非常喜歡,其受人擁戴的原因是部署簡單並且由於是java開發也可以運行在windows平台上,其缺點就是對事務功能支持很差,尤其是不支持分布式事務。默認監聽在8066端口上。
2、模擬部署amoeba實現讀寫分離:
思路:假設有兩台Mysql服務器一主一從都工作在內網中,而用戶可以通過amoeba實現對數據的訪問和寫入,簡單架構如下:
I、環境介紹:
系統:CentOS6.4_x86_64
amoeba:amoeba2.2
mysql:mysql-5.6.13
JDK:jdk 1.6
II、部署過程:
(1)、配置IP地址:
#########前端amoeba服務器############
[root@node ~]#ifconfig eth0 172.16.18.6
[root@node ~]# ifconfig eth1 192.168.18.254/24
#########Mysql A(寫服務器)#########
Ifconfig eth0 192.168.18.1/24
route add default gw 192.168.18.254
#########Mysql B(讀服務器)#########
Ifconfig eth0 192.168.18.2/24
route add default gw 192.168.18.254
(2)、安裝JAVA程序:
由於amoeba是基於JVM運行的所以要想配置JAVA程序:
官方amoeba2.2的版本只測試與java1.6的程序包完美結合實現穩定工作
[root@node ~]# chmod +xjdk-6u31-linux-x64-rpm.bin #賦予執行權限
[root@node ~]# ./jdk-6u31-linux-x64-rpm.bin #執行此文件安裝
[root@node ~]# vim/etc/profile.d/java.sh #編輯執行腳本
export JAVA_HOME=/usr/java/latest
export PATH=$JAVA_HOME/bin:$PATH
[root@node ~]# . /etc/profile.d/java.sh #讀取執行腳本
[root@node ~]# java –version #查看安裝信息
java version "1.6.0_31"
Java(TM) SE Runtime Environment (build1.6.0_31-b04)
Java HotSpot(TM) 64-Bit Server VM (build20.6-b01, mixed mode)
(3)、解壓安裝amoeba:
###########創建目錄############
[root@node ~]# mkdir -pv/usr/local/amoeba-2.2.0 #保留版本信息便於後期識別
###########解壓################
[root@node ~]# tar xfamoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba-2.2.0
###########鏈接################
[root@node local]# ln -sv amoeba-2.2.0/ amoeba
###########編輯執行腳本########
[root@node conf]# vim /etc/profile.d/amoeba.sh
export AMOEBA_HOME=/usr/local/amoeba
export PATH=$AMOEBA_HOME/bin:$PATH
###########讀取執行腳本服務####
[root@node conf]# . /etc/profile.d/amoeba.sh
(4)、配置mysql服務器:
這裡依然使用上面的主從復制環境:
##########停止復制#######
mysql> stop slave;
##########連接主服務器###########
mysql> CHANGE MASTER TO MASTER_HOST='192.168.18.1', MASTER_USER='rpuser', MASTER_PASSWORD='rppass', MASTER_AUTO_POSITION=1;
##########啟動###########
mysql> start slave;
##########查看###########
mysql> show slave statusG
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.18.1
Master_User: rpuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 694
Relay_Log_File: node2-relay-bin.000002
Relay_Log_Pos: 778
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 694
Relay_Log_Space: 982
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 5f9e071b-3145-11e3-8426-000c2906a649
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 5f9e071b-3145-11e3-8426-000c2906a649:6-7
Executed_Gtid_Set: 5f9e071b-3145-11e3-8426-000c2906a649:1-7
Auto_Position: 1
(5)、授權Mysql用戶,用於實現前端amoeba連接
查看源代碼打印幫助1 mysql> GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY 'mypass';
(6)、配置amoeba服務
#############配置文件##############
[root@node ~]# cd /usr/local/amoeba/conf/
#############定義數據庫讀寫分離及節點管理信息########
[root@node conf]# vim amoeba.xml
?xml version="1.0" encoding="gbk"?>
<!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd">
<amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/">
<proxy> #定義代理
<!-- service class must implements com.meidusa.amoeba.service.Service -->
<service name="Amoeba for Mysql"class="com.meidusa.amoeba.net.ServerableConnectionManager"> #定義服務,由類實現
<!-- port --> #定義連接池
<property name="port">3306</property> #定義監聽端口,將默認改動為3306
<!-- bind ipAddress --> #定義代理服務器對外連接的監聽IP
<!--
<property name="ipAddress">127.0.0.1</property>
-->
<property name="ipAddress">0.0.0.0</property> #定義監聽IP地址,這裡定義為監聽所有IP
<property name="manager">${clientConnectioneManager}</property>
<property name="connectionFactory"> #連接池
<bean class="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory">
<property name="sendBufferSize">128</property> #定義發送緩沖大小(可根據主機內存可調整)
<property name="receiveBufferSize">64</property> #定義接受緩沖的大小(可根據主機內存可調整)
</bean>
</property>
<property name="authenticator"> #認證器
<bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">
<property name="user">root</property> #通過客戶端連接後端時的用戶名
<property name="password">mypass</property> #通過客戶端連接後端時的密碼
<property name="filter">
<bean class="com.meidusa.amoeba.server.IPAccessController">
<property name="ipFile">${amoeba.home}/conf/access_list.conf</property> #只允許哪些客戶端訪問,基於文件實現訪問控制
</bean>
</property>
</bean>
</property>
</service>
<!-- server class must implements com.meidusa.amoeba.service.Service -->
<service name="Amoeba Monitor Server" class="com.meidusa.amoeba.monitor.MonitorServer"> #amoeba的監控服務器,監測每一個後端的工作狀態
<!-- port -->
<!-- default value: random number
<property name="port">3306</property> #監控服務器監聽的端口,默認注釋表示使用隨機端口
-->
<!-- bind ipAddress -->
<property name="ipAddress">127.0.0.1</property> #定義監控服務器監聽的地址
<property name="daemon">true</property>
<property name="manager">${clientConnectioneManager}</property>
<property name="connectionFactory">
<bean class="com.meidusa.amoeba.monitor.net.MonitorClientConnectionFactory"></bean>
</property>
</service>
<runtime class="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext">
<!-- proxy server net IO Read thread size -->
<property name="readThreadPoolSize">20</property>
<!-- proxy server client process thread size -->
<property name="clientSideThreadPoolSize">30</property>
<!-- mysql server data packet process thread size -->
<property name="serverSideThreadPoolSize">30</property>
<!-- per connection cache prepared statement size -->
<property name="statementCacheSize">500</property>
<!-- query timeout( default: 60 second , TimeUnit:second) -->
<property name="queryTimeout">60</property>
</runtime>
</proxy>
<!--
Each ConnectionManager will start as thread
manager responsible for the Connection IO read , Death Detection
-->
<connectionManagerList> #定義連接池的列表
<connectionManager name="clientConnectioneManager"class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
<property name="subManagerClassName">com.meidusa.amoeba.net.ConnectionManager</property>
<!--
default value is avaliable Processors
<property name="processors">5</property>
-->
</connectionManager>
<connectionManager name="defaultManager"class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
<property name="subManagerClassName">com.meidusa.amoeba.net.AuthingableConnectionManager</property>
<!--
default value is avaliable Processors
<property name="processors">5</property>
-->
</connectionManager>
</connectionManagerList> #連接池的列表
<!-- default using file loader -->
<dbServerLoader class="com.meidusa.amoeba.context.DBServerConfigFileLoader">
<property name="configFile">${amoeba.home}/conf/dbServers.xml</property>
</dbServerLoader>
<queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter"> #定義查詢路由
<property name="ruleLoader">
<bean class="com.meidusa.amoeba.route.TableRuleFileLoader">
<property name="ruleFile">${amoeba.home}/conf/rule.xml</property>
<property name="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property>
</bean>
</property>
<property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property>
<property name="LRUMapSize">1500</property>
<property name="defaultPool">node1</property> #定義讀寫服務器,這裡默認定義到主服務器上
<property name="writePool">node1</property> #定義寫服務器,若有多個用逗號隔開,這些服務器名來自於dbservers.xml
<property name="readPool">node2</property> #定義讀服務器
<property name="needParse">true</property>
</queryRouter>
</amoeba:configuration>
###############定義連接後端Mysql服務器信息############
[root@node conf]# vim dbServers.xml
<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd">
<amoeba:dbServers xmlns: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 name="abstractServer" abstractive="true"> #定義服務器,名字叫抽象服務器,支持抽象功能:公共屬性的定義
<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
<property name="manager">${defaultManager}</property>
<property name="sendBufferSize">64</property> #定義發送緩沖大小
<property name="receiveBufferSize">128</property> #接受緩沖的大小
<!-- mysql port -->
<property name="port">3306</property> #監聽的端口
<!-- mysql schema -->
<property name="schema">test</property> #默認連接的數據庫服務器
<!-- mysql user -->
<property name="user">root</property> #用戶名
<!-- mysql password
<property name="password">redhat</property> #後端服務器的密碼,授權用戶密碼
-->
</factoryConfig>
<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
<property name="maxActive">500</property> #定義最大活動連接數
<property name="maxIdle">500</property> #空閒連接數
<property name="minIdle">10</property> #最少空閒連接數
<property name="minEvictableIdleTimeMillis">600000</property>
<property name="timeBetweenEvictionRunsMillis">600000</property>
<property name="testOnBorrow">true</property>
<property name="testOnReturn">true</property>
<property name="testWhileIdle">true</property>
</poolConfig>
</dbServer>
<dbServer name="node1" parent="abstractServer"> #定義服務器,其父服務器就是上面的"abstractServer"繼承其屬性,若不想請添加屬性實現定義
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.18.1</property> #server1的IP地址
</factoryConfig>
</dbServer>
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.18.2</property>
</factoryConfig>
</dbServer>
<dbServer name="multiPool" virtual="true"> #定義虛擬服務器組
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
<!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA--> #算法:1表示輪詢,2:基於權重做輪詢,3:高可用效果
<property name="loadbalance">1</property> #還可以實現負載均衡,算法為1
<!-- Separated by commas,such as: server1,server2,server1 -->
<property name="poolNames">node1,node2</property> #此處一定是定義的dbname
</poolConfig>
</dbServer>
</amoeba:dbServers>
(7)、啟動amoeba服務:
###########啟動#############
[root@node conf]# amoeba start
log4j:WARN log4j config load completed from file:/usr/local/amoeba/conf/log4j.xml
2013-09-27 10:01:48,419 INFO context.MysqlRuntimeContext - Amoeba for Mysql current versoin=5.1.45-mysql-amoeba-proxy-2.2.0 #檢測後端mysql服務器兼容版本
log4j:WARN ip access config load completed from file:/usr/local/amoeba/conf/access_list.conf
2013-09-27 10:01:49,622 INFO net.ServerableConnectionManager - Amoeba for Mysql listening on/0.0.0.0:3306. #監聽地址及端口
2013-09-27 10:01:49,701 INFO net.ServerableConnectionManager - Amoeba Monitor Server listening on /127.0.0.1:10540. #管理地址及端口(隨機)
##########停止amoeba###########
ctrl+C
#########使用後端啟動##########
[root@node conf]# amoeba start &
#########後端啟動服務時停止####
[root@node conf]# amoeba stop
###########查看監聽端口########
[root@node ~]# ss -tanl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 ::ffff:127.0.0.1:10540 :::*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::3306 :::*
(8)、連接測試:
[root@node ~]# yum -y install mysql
[root@node ~]# mysql -uroot -h172.16.18.6 -pmypass
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1516725303 #mysql隨機ID號
Server version: 5.1.45-mysql-amoeba-proxy-2.2.0 MySQL Community Server (GPL) #代理服務器amoeba
Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> #連接成功
mysql> select version(); #查看Mysql版本
+------------+
| version() |
+------------+
| 5.6.13-log | #顯示為後端Mysql版本,說明已發往後端服務器執行了
+------------+
(9)、使用抓包工具辨別讀寫分離:
############使用客戶端登陸############
[root@node ~]# mysql -uroot -h172.16.18.6 -pmypass
mysql> select User,Host from mysql.user;
+--------+------------------+
| User | Host |
+--------+------------------+
| root | % |
| rpuser | % |
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| | node2.magedu.com |
| root | node2.magedu.com |
+--------+------------------+
mysql> create database yong;
##################寫服務器抓包#########
[root@node1 ~]# tcpdump -i eth0 -s0 -nn -A tcp dst port 3306 and dst host 192.168.18.1
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
22:26:40.881799 IP 192.168.18.254.55994 > 192.168.18.1.3306: Flags [P.], seq107527731:107527750, ack 3858840995, win 601, options [nop,nop,TS val 71049923 ecr 84850364], length 19
E..G..@[email protected]..=....Y.......
.<"..........show databases #也是可以默認簡單收到讀請求
22:26:40.913496 IP 192.168.18.254.55994 > 192.168.18.1.3306: Flags [.], ack 189, win 638, options [nop,nop,TS val 71049955 ecr 84875929], length 0
E..4..@[email protected]..>_...~.s.....
.<".....
22:28:03.775063 IP 192.168.18.254.55994 > 192.168.18.1.3306: Flags [P.], seq 19:44, ack 189, win 638, options [nop,nop,TS val 71132817 ecr 84875929], length 25
E..M..@[email protected]..>_...~{......
.=f..........create database yong #抓取的寫命令
22:28:03.858065 IP 192.168.18.254.55994 > 192.168.18.1.3306: Flags [.], ack 200, win 638, options [nop,nop,TS val 71132900 ecr 84958874], length 0
E..4..@[email protected]._..>j...~.K.....
.=f...^.
22:28:03.862318 IP 192.168.18.2.48893 > 192.168.18.1.3306: Flags [.], ack 3171025205, win 758, options [nop,nop,TS val 84900440 ecr 84958875], length 0
E..4.*@.@.~>...............`...5.....K.....
..zX..^.
##################讀服務器上##############
[root@node2 ~]# tcpdump -i eth0 -s0 -nn -A tcp dst port 3306 and dst host 192.168.18.2
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
22:26:32.621522 IP 192.168.18.254.34032 > 192.168.18.2.3306: Flags [P.], seq174032882:174032919, ack 3891257330, win 457, options [nop,nop,TS val 71041301 ecr 84656394], length 37
E..Y.2@.@...............
_...........V.....
.<.....
!....select @@version_comment limit 1
22:26:32.622271 IP 192.168.18.254.34032 > 192.168.18.2.3306: Flags [.], ack 100, win 457, options [nop,nop,TS val 71041302 ecr 84808840], length 0
E..4.3@.@..?............
_.....U.....$.....
.<......
22:27:28.160838 IP 192.168.18.254.34032 > 192.168.18.2.3306: Flags [P.], seq 37:74, ack 100, win 457, options [nop,nop,TS val 71096840 ecr 84808840], length 37
E..Y.4@.@...............
_.....U...........
.<......!....select User,Host from mysql.user #抓取的查詢命令
22:27:28.384028 IP 192.168.18.254.34032 > 192.168.18.2.3306: Flags [.], ack 355, win 490, options [nop,nop,TS val 71097064 ecr 84864602], length 0
E..4.5@.@..=............
_.<...T.....:.....
.<.....Z
至此,使用amoeba實現讀寫分離試驗就結束了,有錯誤不足之處歡迎指正留言,筆者將盡最大努力幫你排憂解惑