您的位置:首页 > 数据库 > MySQL

mysql系列之8------读写分离

2018-01-31 21:24 274 查看
一、前言:读写分离的基本原理
mysql的读写分离的基本原理是:让master(主数据库)来响应事务性操作,
让slave(从数据库)来响应select非事务性操作,
然后再采用主从复制来把master上的事务性操作同步到slave数据库中。实现简单的负载均衡。

二、前期准备工作:
1、准备两台服务器,我这里准备的是192.168.4.122(主),192.168.4.123(从)
另外准备一台服务器安装中间件服务器(192.168.4.125)
2、首先把两台服务器做好主从同步。
3、准备读写分离的软件:maxscale-2.1.2-1(中间件)
三、maxscale安装和配置(4.125)
1、安装:rpm -ivh maxscale-2.1.2-1.rhel.7.x86_64.rpm
2、修改配置文件:vim /etc/maxscale.cnf
54-60行注释掉,87-91注释掉
10 threads=auto
18 [server1]
19 type=server
20 address=192.168.4.122
21 port=3306
22 protocol=MySQLBackend
23 [server2]
24 type=server
25 address=192.168.4.123
26 port=3306
27 protocol=MySQLBackend
35 [MySQL Monitor] //监视数据库的配置
36 type=monitor
37 module=mysqlmon
38 servers=server1, server2
39 user=scalemon //监控
40 passwd=123456
41 monitor_interval=10000
63 [Read-Write Service] //配置查询读写权限的帐号
64 type=service
65 router=readwritesplit
66 servers=server1, server2
67 user=maxscale //接收客户端连接请求时,连接的用户名和密码在数据库服务上是否存在
68 passwd=123456
69 max_slave_connections=100%
104 port=4010
3、在主库(4.122)上添加上面需要的两个授权用户
grant replication slave,replication client on *.* to scalemon@'%' identified by "123456";
grant select on mysql.* to maxscale@'%' identified by "123456";
4、启动服务:maxscale -f /etc/maxscale.cnf
停止服务:ps -C maxscale(查进程) kill -9 13109(杀进程)
查看服务是否启动:netstat -natulp | grep maxscale
5、在主库(4.122)上添加用于客户端连接服务器的用户名
grant all on *.* to student@'%' identified by '123456';
6、在4.125上:maxadmin -P4010 -uadmin -pmariadb //访问控制后台
list servers //显示所有服务器主机,可以看到服务器的运行信息
7、用宿主机做客户端连接中间件的登陆方法:
mysql -h192.168.4.125 -P4006 -utest -p123456
8、测试方法:当从挂掉后,客户端可写可读;
但是当主挂掉后,客户端不可读不可写
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息