mysql+amoeba讀寫分離
一 簡介:
Amoeba是一個以MySQL為底層資料存儲,并對應用提供MySQL協定接口的proxy。它集中地響應應用的請求,依據使用者事先設定的規則,将SQL請求發送到特定的資料庫上執行。基于此可以實作負載均衡、讀寫分離、高可用性等需求。與MySQL官方的MySQL Proxy相比,作者強調的是amoeba配置的友善(基于XML的配置檔案,用SQLJEP文法書寫規則,比基于lua腳本的MySQL Proxy簡單)。
Amoeba相當于一個SQL請求的路由器,目的是為負載均衡、讀寫分離、高可用性提供機制,而不是完全實作它們。使用者需要結合使用MySQL的 Replication等機制來實作副本同步等功能。amoeba對底層資料庫連接配接管理和路由實作也采用了可插撥的機制,第三方可以開發更進階的政策類來替代作者的實作。這個程式總體上比較符合KISS原則的思想。
優勢
Amoeba主要解決以下問題:
a). 資料切分後複雜資料源整合
b). 提供資料切分規則并降低資料切分規則給資料庫帶來的影響
c). 降低資料庫與用戶端連接配接
d). 讀寫分離路由
不足
a)、目前還不支援事務
b)、暫時不支援存儲過程(近期會支援)
c)、不适合從amoeba導資料的場景或者對大資料量查詢的query并不合适(比如一次請求傳回10w以上甚至更多資料的場合)
d)、暫時不支援分庫分表,amoeba目前隻做到分資料庫執行個體,每個被切分的節點需要保持庫表結構一緻:
二 準備
1三台centos7系統(注意我是安裝的minimal的系統,需要通yum安裝一些依賴包如:,yum -y groupinstall development tools,現網建議安裝DVD版)
Amoeba:192.168.161.141
Msysql master:192.168.161.142
Mysql slave:192.168.161.143
2 Amoeba上安裝jdk-7u80-linux-x64.tar.gz、amoeba-mysql-3.0.5-RC-distribution.zip
3 Mysql上面安裝MySQL-client-5.6.6_m9-1.rhel5.x86_64.rpm、
MySQL-server-5.6.6_m9-1.rhel5.x86_64.rpm
三 關閉防火牆和selinux
systemctl stop firewalld
systemctl disable firewalld
四 在161.142和161.143上面安裝mysql,并配置主從同步。
1 161.142和161.143上面都安裝mysql
rpm -ivh MySQL-server-5.6.6_m9-1.rhel5.x86_64.rpm
rpm -ivh MySQL-client-5.6.6_m9-1.rhel5.x86_64.rpm
2修改配置檔案
Mysql master(161.142):
[root@localhost home]# cat /etc/my.cnf
[mysqld]
log-bin=mysql-bin
server-id=1
binlog-ignore-db=information_schema #新增的配置,忽略information_schema 的同步
Mysql slave(161.143):
server-id=2
3 啟動兩台mysql,配置主從
(1)啟動mysql:
systemctl start mysql
(2)修改mysql的預設密碼:
mysqladmin -uroot password '123'
(3)進入161.142 mysql master:
[root@localhost home]# mysql -uroot -p123
mysql> show master status;
+------------------+----------+--------------+--------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
| mysql-bin.000004 | 120 | | information_schema | |
1 row in set (0.00 sec)
(4)進入161.143 mysql slave:
執行:
配置slave伺服器:
change master to master_host='192.168.161.142',master_user='root',master_password='123',
master_log_file='mysql-bin.000004',master_log_pos=120;
啟動從伺服器的複制功能:
start slave;
檢視從伺服器複制的狀态:
mysql> show slave status\G'
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.161.142
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 120
Relay_Log_File: localhost-relay-bin.000009
Relay_Log_Pos: 283
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: 120
Relay_Log_Space: 623
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: f2e02c4b-f6c3-11e7-a14e-66ab28c66abe
Master_Info_File: /var/lib/mysql/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:
Executed_Gtid_Set:
Slave_IO_Running: Yes,Slave_SQL_Running: Yes 兩個都為也是則沒有問題。
(5)測試:略
五 在161.141上面配置amoeba
1 161.141上面搭建Java環境
(1)解壓Java包
tar -xzvf jdk-7u80-linux-x64.tar.gz
(2)配置環境變量
修改配置檔案:
Vi /etc/profile
JAVA_HOME=/home/jdk1.7.0_80
CLASSPATH=.:$JAVA_HOME/lib.tools.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH
使配置檔案立即生效:
source /etc/profile
驗證Java:
[root@localhost home]# java -version
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
2 配置amoeba配置檔案
(1)下載下傳
wget -c
https://ncu.dl.sourceforge.net/project/amoeba/Amoeba%20for%20mysql/3.x/amoeba-mysql-3.0.5-RC-distribution.zip
(2)解壓
unzip amoeba-mysql-3.0.5-RC-distribution.zip -d /usr/local/amoeba
(如沒有unzip指令請自行安裝:yum install -y unzip zip)
(3)給amoeba授權mysql遠端賬戶(不推薦使用root)
grant all on . to amoeba@"%" identified by "amoeba";
flush privileges;
(4)修改配置檔案
Amoeba做讀寫分離隻需要修改dbServers.xml、amoeba.xml 這兩個配置檔案
[root@localhost amoeba-mysql-3.0.5-RC]# cd /usr/local/amoeba/amoeba-mysql-3.0.5-RC/conf/
[root@localhost conf]# ls
access_list.conf amoeba.dtd amoeba.xml dbserver.dtd dbServers.xml function.dtd functionMap.xml log4j.dtd log4j.xml rule.dtd ruleFunctionMap.xml rule.xml
修改dbServers.xml
[root@localhost conf]# cat dbServers.xml
<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd">
<amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/">;
</amoeba:dbServers>
[root@localhost conf]#
修改amoeba.xml
[root@localhost conf]# more amoeba.xml
<!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd">
<amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/">;
</amoeba:configuration>
(5)啟動amoeba
[root@server3 amoeba]# bin/launcher
The stack size specified is too small, Specify at least 228k
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
錯誤文字上看,應該是由于stack size太小,導緻JVM啟動失敗,修改jvm.properties檔案JVM_OPTIONS參數
jvm.properties(/usr/local/amoeba/amoeba-mysql-3.0.5-RC)
将
JVM_OPTIONS="-server -Xms256m -Xmx1024m -Xss196k -XX:PermSize=16m -XX:MaxPermSize=96m"
改為
JVM_OPTIONS="-server -Xms1024m -Xmx1024m -Xss256k"
解決jdk7以上要求的啟動xss參數。
[root@server3 amoeba]# netstat -unlpt | grep java ##檢視監聽的端口
tcp 0 0 :::8066 :::* LISTEN 1506/java
啟動/關閉amoeba
最好先前台啟動,檢查沒有錯誤之後再背景啟動。
關閉
# /usr/local/amoeba/amoeba-mysql-3.0.5-RC/bin/shutdown
啟動
# /usr/local/amoeba/amoeba-mysql-3.0.5-RC/bin/launcher
背景啟動并把日志儲存到/var/log/amoeba.log
# /usr/local/amoeba/amoeba-mysql-3.0.5-RC/bin/launcher > /var/log/amoeba.log 2>&1 &
(6)注意:
dbServers.xml、amoeba.xml 配置檔案中的賬号密碼都是mysql資料庫中的賬号,填寫不對會出錯:
ERROR 1000 (42S02): Access denied for user 'amoeba'@'192.168.161.142:57952'(using password: YES)
六測試:
1 在161.142上面連接配接amoeba
(1)關掉master 資料庫161.142,測試是否隻讀
systemctl stop mysql
[root@localhost etc]# mysql -uamoeba -pamoeba -h192.168.161.141 -P8066
mysql> use test;
Database changed
mysql> select * from b ;
+------+------+
| sf | ff |
| 1 | 1 |
| 3 | 3 |
| 5 | 5 |
3 rows in set (0.01 sec)
mysql> insert into b values(5,5);
ERROR 1044 (42000): Amoeba could not connect to MySQL server[192.168.161.142:3306],Connection refused
mysql>
(2)關掉slave資料庫161.143,測試是否隻能寫,不能讀
ERROR 1044 (42000): Amoeba could not connect to MySQL server[192.168.161.143:3306],Connection refused
Query OK, 1 row affected (0.03 sec)
七 借鑒網址
http://blog.csdn.net/oufua/article/details/77373851
http://blog.csdn.net/sds15732622190/article/details/69262632
http://blog.csdn.net/Mryiyi/article/details/73521861