Oneproxy讀寫分離mysql
1、Oneproxy讀寫分離linux
主服務器(Master):192.168.32.150
從服務器(slave):192.168.32.129
oneproxy中間件服務器:192.168.32.150sql
一、首先要配置好主從複製而且在主服務器上建立test庫數據庫
mysql>create database test;vim
mysql> show databases;安全
添加oneproxy的鏈接用戶會自動replication到slave數據庫bash
mysql>grant select,delete,insert,update,create on test.* to oneproxy@'%' identified by '123456';服務器
二、關閉三臺服務器的防火牆或者配置防火牆策略ide
Mysql:3306加密
oneproxy鏈接端口3307
oneproxy管理端口4041
[root@localhost ~]# systemctl stop firewalld.service
①開啓oneproxy自動識別讀寫服務器(從服務器配置)
[root@localhost ~]# vim /etc/my.cnf
添加:read_only = 1
②重啓mysql服務
[root@localhost ~]# systemctl restart mysqld
mysql> show variables like "%read_only%";
或者 mysql>set global read_only = 1; //臨時開啓
三、配置oneproxy中間件(oneproxy服務器)
①安裝oneproxy
[root@localhost ~]# wget http://www.onexsoft.com/software/oneproxy-rhel6-linux64-v6.2.0-ga.tar.gz
[root@localhost ~]# tar zxf oneproxy-rhel6-linux64-v6.2.0-ga.tar.gz -C /usr/local/
②在預啓動腳本中修改oneproxy目錄
[root@localhost ~]# vim /usr/local/oneproxy/demo.sh
~
#/bin/bash
export ONEPROXY_HOME=/usr/local/oneproxy
ulimit -c unlimited
valgrind --leak-check=full \
${ONEPROXY_HOME}/bin/oneproxy --defaults-file=${ONEPROXY_HOME}/conf/proxy.conf
③修改啓動腳本oneproxy目錄
[root@localhost ~]#vim /usr/local/oneproxy/oneproxy.service
............................
OneProxy Settings
ONEPROXY_HOME=/usr/local/oneproxy
ONEPROXY_SBIN="${ONEPROXY_HOME}/bin/oneproxy"
ONEPROXY_CONF="${ONEPROXY_HOME}/conf/proxy.conf"
ONEPROXY_PID="${ONEPROXY_HOME}/log/oneproxy.pid"
RETVAL=0
prog="OneProxy"
④獲取oneproxy加密後的密文密碼
[root@localhost ~]# cd /usr/local/oneproxy/bin/
[root@localhost bin]# ls
[root@localhost bin]# ./mysqlpwd 123456
9D7E55EAF8912CCBF32069443FAC452794F8941B
⑤修改oneproxy主配文件
[root@localhost ~]# mysql --version
[root@localhost bin]# vim /usr/local/oneproxy/conf/proxy.conf
[oneproxy]
#proxy-license = A2FF461456A67F28,D2F6A5AD70C9042D
keepalive = 1
event-threads = 4
proxy-group-policy = test:read-slave
log-file = log/oneproxy.log
pid-file = log/oneproxy.pid
lck-file = log/oneproxy.lck
proxy-auto-readonly = 1
proxy-forward-clientip = 1
proxy-trans-debug = 1
mysql-version = 5.7.21
proxy-master-addresses.1 = 192.168.32.150:3306@test
proxy-slave-addresses.2 = 192.168.32.129:3306@test
proxy-user-list = oneproxy/9D7E55EAF8912CCBF32069443FAC452794F8941B@test
proxy-part-template = conf/template.txt
proxy-part-tables.1 = conf/part.txt
proxy-part-tables.2 = conf/part2.txt
proxy-part-tables.3 = conf/cust1.txt
proxy-charset = utf8_bin
proxy-secure-client = 127.0.0.1
proxy-httpserver = :8080
proxy-httptitle = OneProxy Monitor
proxy-auto-readonly:自動切換讀寫角色
proxy-group-policy: 預約義策略,對應真實要管理的數據庫
proxy-user-list: 指明鏈接數據庫用戶密文密碼要與真實數據庫用戶密碼對應@符後指的是數據庫啓動oneproxy並查看端口
[root@localhost bin]# cd ..
[root@localhost oneproxy]# chmod +x demo.sh
[root@localhost oneproxy]# ./demo.sh
[root@localhost oneproxy]# ./oneproxy.service start
[root@localhost oneproxy]# ss -anpt | grep 3307
[root@localhost oneproxy]# ss -anpt | grep 4041
五、驗證
因爲oneproxy爲了安全起見,初始靜止了DDl語句,須要去到4041管理端口打開底層數據庫權限(從服務器)
[root@localhost ~]# mysql -uadmin -pOneProxy -P4041 -h 192.168.32.149
mysql> set gaccess test 0 ;
使用客戶端登陸oneproxy鏈接端口3307(主服務器)
[root@localhost ~]# mysql -uoneproxy -p123456 -h 192.168.32.149 -P3307
mysql> create table test.tb (id int);
mysql> insert into test.tb values(1);
mysql> select * from test.tb;
觀察管理網頁master和slave中query的變化,讀負載的增長與寫負載的增長,驗證成功
訪問oneproxy管理網頁
格式:輸入http://192.168.32.149:8080
默認用戶名admin,密碼OneProxy
OneProxy簡單實現Mysql分庫分表
①修改配置文件proxy.conf
[root@localhost ~]#vim /usr/local/oneproxy/conf/proxy.conf
[oneproxy]
#proxy-license = A2FF461456A67F28,D2F6A5AD70C9042D
keepalive = 1
event-threads = 4
proxy-group-policy.1 = test:master-only
proxy-group-security = test:0log-file = log/oneproxy.log
pid-file = log/oneproxy.pid
lck-file = log/oneproxy.lck
proxy-forward-clientip = 1
proxy-trans-debug = 1
mysql-version = 5.7.21
proxy-master-addresses.1 = 192.168.32.187:3306@test
proxy-user-list = oneproxy/9D7E55EAF8912CCBF32069443FAC452794F8941B@test
proxy-part-template = conf/template.txt
proxy-part-tables.1 = conf/part.txt
proxy-part-tables.2 = conf/part2.txt
proxy-part-tables.3 = conf/cust1.txt
proxy-charset = utf8_bin
proxy-httpserver = :8080
proxy-httptitle = OneProxy Monitor
②修改配置文件part2.txt
[root@localhost oneproxy]#cd /usr/local/oneproxy/conf
[root@localhost conf]#vim part2.txt
[
{
"table" : "user",
"pkey" : "id",
"type" : "int",
"method" : "hash",
"partitions" :
[
{ "suffix" : "_0", "group": "test" },
{ "suffix" : "_1", "group": "test" },
{ "suffix" : "_2", "group": "test" },
{ "suffix" : "_3", "group": "test"}
]
}
]
③啓動oneproxy並查看端口
[root@localhost conf]# cd ..
[root@localhost oneproxy]# ./demo.sh
[root@localhost oneproxy]# ./oneproxy.service start
[root@localhost oneproxy]# ss -anpt | grep 3307
[root@localhost oneproxy]# ss -anpt | grep 4041
若是端口查詢不到,能夠查看oneproxy日誌文件oneproxy.log。
若是沒有日誌記錄,多是主配或part文件有錯誤
驗證
①從客戶端登陸鏈接端口(主服務器)[root@localhost ~]# mysql -h 192.168.32.149 -uoneproxy -p123456 -P3307
mysql> show databases;
②插入數據
mysql>create table user(id int ,c1 int);
mysql>insert into user(id,c1) values (1,1);
mysql>insert into user(id,c1) values (2,2);
mysql>insert into user(id,c1) values (3,3);
mysql>insert into user(id,c1) values (4,4);
mysql>insert into user(id,c1) values (5,5);③查看
mysql>select * from user;④回到mysql查看存儲位置
[root@localhost ~]# mysql -uroot -p123456
mysql> use test;
mysql> show tables ;
mysql> select * from user_0;
mysql> select * from user_1;
mysql> select * from user_2;
mysql> select * from user_3;