docker run -tid -p 3307:3306 --name mysql_master -e MYSQL_ROOT_PASSWORD=123456 mysql:5.6
docker run -tid -p 3308:3306 --name mysql_slave -e MYSQL_ROOT_PASSWORD=123456 mysql:5.6
2. 配置mysql-proxymysql
wget https://downloads.mysql.com/archives/get/file/mysql-proxy-0.8.5-linux-debian6.0-x86-64bit.tar.gz
tar zxvf mysql-proxy-0.8.5-linux-debian6.0-x86-64bit.tar.gz
mv mysql-proxy-0.8.5-linux-debian6.0-x86-64bit mysql-proxy cd /usr/local/mysql-proxy mkdir lua #建立腳本存放目錄 mkdir logs #建立日誌目錄 touch logs/mysql-proxy.log chmod 777 logs/mysql-proxy.log cp share/doc/mysql-proxy/rw-splitting.lua ./lua #複製讀寫分離配置文件 cp share/doc/mysql-proxy/admin-sql.lua ./lua #複製管理腳本
vi /etc/mysql-proxy.cnf #建立配置文件
3. 配置文件linux
[mysql-proxy]
user=root #運行mysql-proxy用戶 log-level=info #定義log日誌級別,由高到低分別有(error|warning|info|message|debug) log-file=/usr/local/mysql-proxy/logs/mysql-proxy.log #日誌位置 proxy-address=172.17.0.5:4040 #mysql-proxy運行ip和端口,不加端口,默認4040 proxy-backend-addresses=172.17.0.2:3306 #指定後端主master寫入數據 proxy-read-only-backend-addresses=172.17.0.3:3306 #指定後端從slave讀取數據 proxy-lua-script=/usr/local/mysql-proxy/lua/rw-splitting.lua #指定讀寫分離配置文件位置 admin-lua-script=/usr/local/mysql-proxy/lua/admin-sql.lua #指定管理腳本 daemon=true #以守護進程方式運行 keepalive=true #mysql-proxy崩潰時,嘗試重啓
chmod 660 /etc/mysql-porxy.cnf
vi /usr/local/mysql-proxy/lua/rw-splitting.lua
找到下面的代碼,覆蓋對應部分便可,其他代碼均保留不變。
if not proxy.global.config.rwsplit then proxy.global.config.rwsplit = { min_idle_connections = 1, #默認超過4個鏈接數時,纔開始讀寫分離,改成1 max_idle_connections = 1, #默認8,改成1 is_debug = false } end
/usr/local/mysql-proxy/bin/mysql-proxy --proxy-backend-addresses=172.17.0.5:3306 --proxy-backend-addresses=172.17.0.3:3306 --proxy-backend-addresses=172.17.0.2:3306
/usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/etc/mysql-proxy.cnf &
加&是置於後臺運行。