2

OneProxy实现Mysql读写分离

环境:

master主机:192.168.1.10
slave主机:192.168.1.12
oneproxy中间件:192.168.1.11

一、配置主从(略)

①注意关闭三端防火墙,也可以开放端口,数据库端口3306,oneproxy连接端口3307,oneproxy管理端口4041

②当配置同步之后需要在master主机创建test库并添加oneproxy的连接用户会自动replication到slave数据库
mysql>create database test;
mysql>grant select,delete,insert,update,create on test.* to oneproxy@'%' identified by '123.com';

③在slave数据库中设置read_only项,read_only=1只读模式,不会影响slave同步复制的功能,它可以限定普通用户进行数据修改的操作,但不会限定具有super权限的用户的数据修改操作,这里开启它让oneproxy自动识别读写服务器
mysql>show variables like ‘%read_only%’;

Variable_name Value
innodb_read_only OFF
read_only OFF
super_read_only OFF
tx_read_only OFF

mysql>set global read_only = 1;

二、配置OneProxy中间件

步骤:
①安装oneproxy
[root@192 ~ ]# wget http://www.onexsoft.com/softw...
[root@192 ~ ]# tar zxf oneproxy-rhel6-linux64-v6.2.0-ga.tar.gz -C /usr/local

②在预启动脚本中修改oneproxy目录
[root@192 ~ ]# 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@192 ~ ]# vim /usr/local/oneproxy/oneproxy.service

....
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"
....

④获取oneproxy加密后的密文密码
[root@192 ~]# cd /usr/local/oneproxy/bin
[root@192 bin]# ls
mysqlpwd oneproxy
[root@192 bin]# ./mysqlpwd 123.com
7FB703DA3682A0CCC20168D44E8A7E92FE676A51

⑤修改oneproxy主配文件
[root@192 ~ ]# 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.18
proxy-master-addresses.1 = 192.168.1.10:3306@test
proxy-slave-addresses.2 = 192.168.1.12:3306@test
proxy-user-list = oneproxy/7FB703DA3682A0CCC20168D44E8A7E92FE676A51@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@192 oneproxy]# chmod +x demo.sh
[root@192 oneproxy]# ./demo.sh
[root@192 oneproxy]# ./oneproxy.service start
Starting OneProxy ... [ OK ]
[root@192 conf]# ss -anpt | grep 3307
LISTEN 0 128 :3307 :* users:(("oneproxy",pid=3401,fd=11))
[root@192 conf]# ss -anpt | grep 4041
LISTEN 0 128 :4041 :* users:(("oneproxy",pid=3401,fd=13))

⑦访问oneproxy管理网页
输入http://oneproxy_ip:8080
默认用户名admin,密码OneProxy
图片描述

⑧验证
使用客户端登录oneproxy连接端口3307
[root@192 ~]# mysql -uoneproxy -p123.com -h 192.168.1.11 -P3307
mysql> create table test.tb (id int);
ERROR 1044 (42000): Access denied due to security policy, DDL disabled or DML restricted!
由于oneproxy为了安全起见,初始静止了DDl语句,需要去到4041管理端口打开底层数据库权限
[root@192 ~]# mysql -uadmin -pOneProxy -P4041 -h 192.168.1.11
mysql> set gaccess test 0 ;
[root@192 ~]# mysql -uoneproxy -p123.com -h 192.168.1.11 -P3307
mysql> create table test.tb (id int);
mysql> insert into test.tb values(1);
mysql> insert into test.tb values(2);
.....多插入几条
mysql> select * from test.tb;
.....多查看几次
图片描述
观察管理网页master和slave中query的变化,读负载的增加与写负载的增加,验证成功

OneProxy实现Mysql分库分表

环境:

master主机:192.168.1.12
oneproxy中间件:192.168.1.11

思路:

客户端→oneproxy→master主机
          ↘       ↓
             test库→user表→user_0子表
                          →user_1子表
                          →user_2子表
                          →user_3子表

一、配置master主机

步骤:
①分配连接用户权限并创建数据库
mysql> grant all on . to oneproxy@'%' identified by '123.com';
mysql>create database test;

②关闭防火墙或开启端口
[root@192 ~]# systemctl stop firewalld

二、配置OneProxy中间件

安装部署过程略

步骤:
①配置proxy.conf
[root@192 ~ ]# 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:0
log-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.18
proxy-master-addresses.1 = 192.168.1.12:3306@test
proxy-user-list = oneproxy/7FB703DA3682A0CCC20168D44E8A7E92FE676A51@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

[
{
    "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@192 oneproxy]# sh demo.sh
[root@192 oneproxy]# ./oneproxy.service start
Starting OneProxy ... [ OK ]
[root@192 oneproxy]# ss -anpt | grep 3307
LISTEN 0 128 :3307 :* users:(("oneproxy",pid=37997,fd=11))
[root@192 oneproxy]# ss -anpt | grep 4041
LISTEN 0 128 :4041 :* users:(("oneproxy",pid=37997,fd=13))
如果端口查询不到,请查看oneproxy日志文件oneproxy.log,如果没有日志记录,则可能主配或part文件有错误

三、验证

步骤:
①从客户端登录连接端口
[root@192 ~]# mysql -h 192.168.1.11 -u oneproxy -p123.com -P3307
mysql>show databases;

Database
test

②插入数据
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;

id c1
4 4
1 1
5 5
2 2
3 3

③回到master主机查看存储位置
[root@192 ~]# mysql
mysql> use test
mysql> show tables ;

Tables_in_test
user
user_0
user_1
user_2
user_3

mysql> select * from user_0;

id c1
4 4

1 row in set (0.00 sec)

mysql> select * from user_1;

id c1
1 1
5 5

2 rows in set (0.00 sec)

mysql> select * from user_2;

id c1
2 2

1 row in set (0.00 sec)

mysql> select * from user_3;

id c1
3 3

1 row in set (0.00 sec)

通过oneproxy实现了分离数据,验证成功


汀云社
60 声望10 粉丝

欢迎来访汀云社,大家共同学习,一起成长