Mysql-Oneproxy读写分离

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql-Oneproxy读写分离相关的知识,希望对你有一定的参考价值。

Oneproxy读写分离

一、Oneproxy读写分离

主服务器(Master):192.168.32.150
从服务器(slave):192.168.32.129
oneproxy中间件服务器:192.168.32.150


1、首先要配置好主从复制并且在主服务器上创建test库

mysql>create database test;

mysql> show databases;

添加oneproxy的连接用户会自动replication到slave数据库

mysql>grant select,delete,insert,update,create on test.* to [email protected]‘%‘ identified by ‘123456‘;


2、关闭三台服务器的防火墙或者配置防火墙策略

Mysql:3306

oneproxy连接端口3307

oneproxy管理端口4041

[[email protected] ~]# systemctl stop firewalld.service

①开启oneproxy自动识别读写服务器(从服务器配置)

[[email protected] ~]# vim /etc/my.cnf

添加:read_only = 1

②重启mysql服务

[[email protected] ~]# systemctl restart mysqld

mysql> show variables like "%read_only%";

或者 mysql>set global read_only = 1; //临时开启


3、配置oneproxy中间件(oneproxy服务器)

①安装oneproxy

[[email protected] ~]# wget http://www.onexsoft.com/software/oneproxy-rhel6-linux64-v6.2.0-ga.tar.gz

[[email protected] ~]# tar zxf oneproxy-rhel6-linux64-v6.2.0-ga.tar.gz -C /usr/local/

②在预启动脚本中修改oneproxy目录

[[email protected] ~]# 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目录

[[email protected] ~]#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加密后的密文密码

[[email protected] ~]# cd /usr/local/oneproxy/bin/

[[email protected] bin]# ls

[[email protected] bin]# ./mysqlpwd 123456

9D7E55EAF8912CCBF32069443FAC452794F8941B

⑤修改oneproxy主配文件

[[email protected] ~]# mysql --version

[[email protected] 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:[email protected]

proxy-slave-addresses.2 = 192.168.32.129:[email protected]

proxy-user-list = oneproxy/[email protected]

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并查看端口

[[email protected] bin]# cd ..

[[email protected] oneproxy]# chmod +x demo.sh

[[email protected] oneproxy]# ./demo.sh

[[email protected] oneproxy]# ./oneproxy.service start

[[email protected] oneproxy]# ss -anpt | grep 3307

[[email protected] oneproxy]# ss -anpt | grep 4041


5、验证

由于oneproxy为了安全起见,初始静止了DDl语句,需要去到4041管理端口打开底层数据库权限(从服务器)

[[email protected] ~]# mysql -uadmin -pOneProxy -P4041 -h 192.168.32.149

mysql> set gaccess test 0 ;

使用客户端登录oneproxy连接端口3307(主服务器)

[[email protected] ~]# 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

技术分享图片

以上是关于Mysql-Oneproxy读写分离的主要内容,如果未能解决你的问题,请参考以下文章

c#mongo 怎么设置读写分离

一台电脑上怎么实现mysql读写分离

MySQL主从复制与读写分离

Redis——Sentinel 高可用读写分离

什么是数据库的读写分离

mysql读写分离实现方式