标签:mysql-proxy 读写分离
一. 环境描述操作系统版本:centos 7.2
主服务器:192.168.0.72 node2
从服务器:192.168.0.73 node3
调度服务器MySQL-Proxy:192.168.0.71 node1
node2和node3都需安装
[lirou@node2 ~]$ sudo yum -y install mariadb-server
master配置文件:
skip_name_resolve
innodb_file_per_table
server_id = 1
log_bin = /var/lib/mysql/bin_log
slave 配置文件:
skip_name_resolve
innodb_file_per_table
server_id = 2
log_bin = /var/lib/mysql/bin_log
read_only
注意:master服务器一定要开启二进制日志,因为主从复制依赖与master。而且主从服务器的server_id一定要不同。
node2和node3都要启动服务
[lirou@node3 ~]$ sudo systemctl start mariadb
Master服务器上
分配slave主机访问master的权限账号:
[lirou@node2 ~]$ mysql -uroot
MariaDB [(none)]> GRANT REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'repl_user'@'192.168.0.%' IDENTIFIED BY 'repl_pass';
查看master上二进制日志位置
MariaDB [(none)]> show master status ;
+---------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------+----------+--------------+------------------+
| binlog.000001 | 425 | | |
+---------------+----------+--------------+------------------+
slave服务器上
mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.72', MASTER_USER='repl_user', MASTER_PASSWORD='repl_pass', MASTER_LOG_FILE='binlog.000001', MASTER_LOG_POS=425;
mysql> slave start; # 开启从同步
mysql> show slave status \G; #查看同步是否成功
~]# wget wget https://downloads.mysql.com/archives/get/file/mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz
~]# sudo tar xf https://downloads.mysql.com/archives/get/file/mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz -C /usr/local
~]# mv /usr/local/mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit /usr/local/mysql-proxy
创建lua脚本目录
~]# cd /usr/local/mysql-proxy
mysql-proxy]# mkdir lua
创建日志目录
Mysql-proxy]# mdkir logs
拷贝读写分离配置文件和管理脚本到lua脚本目录
mysql-proxy]# cp share/doc/mysql-proxy/rw-splitting.lua ./lua
Mysql-proxy]# cp share/doc/mysqp-proxy/admin-sql.lua ./lua
创建配置文件
~]# vim /etc/mysql-proxy.conf
[mysql-proxy]
user = root 运行代理的用户
admin-username = mysql_proxy_user # mysql-proxy连接后端mysql服务器的用户
admin-password = mysql_proxy_pass # mysql-proxy连接后端mysql服务器的密码
proxy-address = 192.168.0.71:3307 # mysql-proxy监听的IP和端口,端口默认4040
proxy-read-only-backend-addresses = 192.168.0.73 #只读服务
proxy-backend-addresses = 192.168.0.72 # 读写服务器
proxy-lua-script = /usr/local/mysql-proxy/lua/rw-splitting.lua # 指明读写分离配置文件
admin-lus-script = /usr/local/mysql-proxy/lua/admin-sql.lua # 管理脚本路径
log-file = /usr/local/mysql-proxy/logs/mysql-proxy.log # 日志文件路径
log-level = debug #日志记录等级
daemon = true # 程序是否在后端启动
keepalive = true # mysql-proxy崩溃后,尝试重启
修改读写配置文件
vim /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
}
启动mysql-proxy
~]# /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/etc/mysql-proxy.cnf
在mysql上创建mysql-proxy连接的用户
mysql> GRANT ALL ON *.* TO 'mysql_proxy_user'@'192.168.0.%' IDENTIFIED BY 'mysql_proxy_pass';
测试写
mysql -umysql_proxy_user -pmysql_proxy_pass -h192.168.0.71 --port=3307
mysql> create databases master;
mysql> create table test (id int);
mysql> insert into master.test value (1);
查看从服务器:
可以看到从服务器同步了
测试读
主服务器查询语句条数
从服务器查询语句条数
连接mysql-proxy执行一条读语句
主服务器查询语句条数
从服务器查询语句条数
我们可以看到查询语句是在从服务器上执行。
标签:mysql-proxy 读写分离
原文地址:http://blog.51cto.com/13589448/2123185