目前有很多mysql中間層,baidu的 dbproxy,taobao的 tddl ,126 ,alibaba的 corba,360的altas ,mysql-proxy,樓方鑫的oneproxy,sohu的dbproxy,fabric等。有的支持分庫分表,有的不支持。選擇一款合適的就好。但是,如果做技術選型,還是要謹慎小心。選擇自主研發還是用現有的第三方產品要慎重。
#安裝dbproxy altas 基本上按照朱超 寫的這個,就能安裝完 http://github.com/Qihoo360/Atlas/wiki/Atlas的安裝
一.下載
mkdir -p /data/software && cd /data/software
wget https://github.com/Qihoo360/Atlas/archive/2.2.1.tar.gz
mv 2.2.1.tar.gz atlas.2.2.1.tar.gz
tar zxvf atlas.2.2.1.tar.gz
二.安裝庫文件
#安裝庫文件,gcc等編譯環境,需要提前配好,這裡就掠過了。
yum install -y libevent-devel lua-devel openssl-devel flex mysql-devel
yum install -y xz gettext-devel
# libffi,glib(>2.32)
yum -y install libffi*
cd /data/software
wget http://ftp.acc.umu.se/pub/gnome/sources/glib/2.33/glib-2.33.6.tar.xz
xz -d glib-2.33.6.tar.xz
cd glib-2.33.6
./configure
make
make install
ldconfig
# jemalloc
cd /data/software
wget http://www.canonware.com/download/jemalloc/jemalloc-3.1.0.tar.bz2
tar xvf jemalloc-3.1.0.tar.bz2
make
make install
cp /data/software/jemalloc-3.1.0/lib/libjemalloc.so.1 /usr/local/lib/
ldconfig
三.編譯安裝Atlas
cd /data/software/Atlas-2.2.1
#修改安裝目錄 --prefix=/home/mysql/dbproxy
vim bootstrap.sh
#!/bin/sh
base=$(cd "$(dirname "$0")"; pwd)
cd $base
PKG_CONFIG_PATH=/usr/local/lib/pkgconfig ./configure --with-mysql=/usr --prefix=/home/mysql/dbproxy CFLAGS="-DHAVE_LUA_H -O2" LDFLAGS="-lm -ldl -lcrypto -ljemalloc" LUA_CFLAGS="-I/usr/local/include/" LUA_LIBS="-L/usr/local/lib -llua"
#用慣了,之前我們把dbproxy 安裝在 /home/mysql/dbproxy,習慣了
./bootstrap.sh
make && make install
#編譯過程中,如果有問題,看 config.log 記錄的信息,一般是 jemalloc沒安裝、版本低,libffi*,glib-2.32,等庫版本低或者沒安裝的問題。
四.配置
1.配置停起腳本mysql-proxyd
vim /home/mysql/dbproxy/bin/mysql-proxyd
修改 proxydir=/usr/local/mysql-proxy 為 proxydir=/home/mysql/dbproxy
#或者 sed -i 's%proxydir=/usr/local/mysql-proxy%proxydir=/home/mysql/dbproxy%g' /home/mysql/dbproxy/bin/mysql-proxyd
2.修改配置文件,後綴名必須是.cnf,如果不想用.cnf,也可以自己去改mysql-proxyd這個腳本
vim /home/mysql/dbproxy/conf/dbproxy.cnf
[mysql-proxy]
admin-username = user
admin-password = pwd
admin-address = 0.0.0.0:2345
proxy-backend-addresses = 192.168.0.220:9858
proxy-read-only-backend-addresses = 192.168.1.221:9858,192.168.0.14:9858
pwds = dbproxy_longxibendi_w: /iZxz+0GRoA=,dbproxy_longxibendi_r:/iZxz+0GRoA=
daemon = true
keepalive = true
event-threads = 4
log-level = message
log-path = /home/mysql/dbproxy/log
sql-log = ON
sql-log-slow = 10
wait-timeout = 10
proxy-address = 0.0.0.0:12340
charset = utf8
client-ips = 127.0.0.1, 192.168.1,192.168.0.220,192.168.1.221
3.mysql,slave上添加賬號
master: 192.168.0.220
slave: 192,168.1.221,192.168.0.14
master上:mysql -uroot -p -h127.0.0.1 -P5858
mysql-> grant select,update,delete,insert on longxibendi.* to [email protected] identified by '123456';
slave 上,相同授權,注意ip地址是dbproxy的地址
五.啟動
1.啟動dbproxy
cd /home/mysql/dbproxy/bin
./mysql-proxyd dbproxy start
2.訪問測試
mysql -udbproxy_longxibendi_w -h192.168.0.220 -p123456 -P10240
mysql-> show databases;
#可以測試下事務,讀寫分離,主庫宕機、從庫宕機故障轉移等。