MYSQL主:192.168.26.210(centos6.6,Mariadb10)
MYSQL从:192.168.26.211(centos6.6,Mariadb10)
读写分离器mysql-proxy:192.168.26.212
主:mysql配置文件编辑:
vim /etc/my.cnf.d/server.cnf
配置文件中加入以下两项:
log-bin=mysql-bin
server-id = 210
从:mysql配置文件编辑:
vim /etc/my.cnf.d/server.cnf
配置文件中加入以下两项:
读写分离调度器:mysql-proxy(0.8.5版本)
直接YUM安装:yum list msyql-proxy
mysql-proxy安装完成后编辑配置文件:vim /etc/mysql-proxy
[mysql-proxy]
daemon = true
pid-file = /var/run/mysql-proxy.pid
log-file = /var/log/mysql-proxy.log
log-level = debug
max-open-files = 1024
plugins = admin,proxy
user = mysql-proxy
#
#Proxy Configuration
proxy-address = 0.0.0.0:3306 #分离器地址默认本是4040,若本机安装了MYSQL则改用其它端口
proxy-backend-addresses =192.168.26.210:3306 #可读写服务器地址
proxy-read-only-backend-addresses = 192.168.26.211:3306 #只读服务器地址列表,多台服务器用,分隔
proxy-lua-script = /usr/share/doc/mysql-proxy-0.8.5/examples/rw-splitting.lua #指定读写分离脚本路径位置
proxy-skip-profiling = true
#
# Admin Configuration
admin-address = 0.0.0.0:4041 #管理地址
admin-lua-script = /usr/lib64/mysql-proxy/lua/admin.lua #mysql-proxy不对用户做身份验证,而是下身份验证交予后端服务器进行验证的,因此需要在后端服务器上对mysql-proxy开放权限
admin-username = admin #管理账号
admin-password = admin #管理密码
保存退出,记得加入开机服务启动项:chkconfig mysql-proxy on
说明下:YUM安装后lua脚本路径: /usr/share/doc/mysql-proxy-0.8.5/examples/rw-splitting.lua
admin.lua脚本路径: /usr/lib64/mysql-proxy/lua/admin.lua
启动服务:service mysql-proxy start
用管理账号登陆mysql-proxy
mysql -uadmin -padmin -h192.168.26.212 --port=4041
执行命令查看:SELECT * FROM backends;
两台服务器主和从都已经添加进mysql-proxy,主服务器rw表示可读写,ro表示只读。
再回到主服务器:192.168.26.210创建一个访问账号和密码,供测试使用。
grant all on *.* to 'bdadmin'@'192.168.%.%' IDENTIFIED BY 'jerrypass';
为了测试效果可以用tcpdump进行抓包观察效果:
任一找一台同网络中的主机登陆mysql服务器(210,211,212都可以,注意登陆时端口号,这里我是我手动
改为3306了的):
执行创建数据库操作:
create database bdtest;
1 台主多台从配置:
[mysql-proxy]
daemon = true
pid-file = /var/run/mysql-proxy.pid
log-file = /var/log/mysql-proxy.log
log-level = debug
max-open-files = 1024
plugins = admin,proxy
user = mysql-proxy
#
#Proxy Configuration
proxy-address = 0.0.0.0:3306
proxy-backend-addresses = 192.168.26.210:3306
proxy-read-only-backend-addresses = 192.168.26.211:3306,192.168.26.212:3306
proxy-lua-script = /usr/share/doc/mysql-proxy-0.8.5/examples/rw-splitting.lua
#proxy-skip-profiling = true
#
# Admin Configuration
#admin-address = 0.0.0.0:4041
admin-lua-script = /usr/lib64/mysql-proxy/lua/admin.lua
admin-username = admin
admin-password = admin
特别提示,测试中需要多执行几次查询(因为操作要受连接数限制)操作,才能更好的观察效果。