mysql读写分离-Maxscale

一、读写分离是什么

读写分离,基本的原理是让主数据库处理事务性增、改、删操作(INSERT、UPDATE、DELETE),而从数据库处理SELECT查询操作。数据库复制被用来把事务性操作导致的变更同步到集群中的从数据库。

二、作用

读写分离,解决由数据库的写入因素而影响了查询的效率的问题。
如果数据库的使用是更新少,查询多的情况下会考虑使用,利用数据库 主从同步,可以减少数据库压力,提高性能。

读写分离解析图:
这里写图片描述

三、读写分离中间件 maxscale

https://www.cnblogs.com/darren-lee/p/7591416.html

1、安装软件包

[root@host6 ~]# yum -y install maxscale-2.1.2-1.rhel.7.x86_64.rpm

2、修改配置文件

[root@host6 ~]# ls /etc/maxscale.cnf
/etc/maxscale.cnf
[root@host6 ~]# cp /etc/maxscale.cnf /etc/maxscale.cnf.bak
[root@host6 ~]# vim /etc/maxscale.cnf
[maxscale]
threads=1
[server1]
type=server
address=192.168.4.1
port=3306
protocol=MySQLBackend

[server2]
type=server
address=192.168.4.2
port=3306
protocol=MySQLBackend

[MySQL Monitor]
type=monitor
module=mysqlmon
servers=server1, server2
user=scalemon
passwd=123456
monitor_interval=10000

[Read-Write Service]
type=service
router=readwritesplit
servers=server1, server2
user=maxscale
passwd=123456
max_slave_connections=100%

[MaxAdmin Service]
type=service
router=cli

[Read-Write Listener]
type=listener
service=Read-Write Service
protocol=MySQLClient
port=4006

[MaxAdmin Listener]
type=listener
service=MaxAdmin Service
protocol=maxscaled
socket=default
port=4018

[root@host6 ~]#

3、根据配置文件做相应的设置(在2台数据库服务器上添加用户)

监控数据库服务器时,连接数据库服务器的用户**
mysql>
grant replication slave, replication client on .
to scalemon@’%’ identified by “123456”;

验证 访问数据时,连接数据库服务器使用的用户,是否在数据库服务器上存在的,连接用户
mysql>
grant select on mysql.*
to maxscale@’%’ identified by “123456”;

查看授权用户
mysql> select user,host from mysql.user where user in (“scalemon”,”maxscale”);
+———-+——+
| user | host |
+———-+——+
| maxscale | % |
| scalemon | % |
+———-+——+

4、启动服务
[root@host6 ~]# maxscale -f /etc/maxscale.cnf

5、查看服务进程和端口

查看端口
[root@host6 ~]# netstat -utnlp | grep :4006
tcp6 0 0 :::4006 :::* LISTEN 29688/maxscale

[root@host6 ~]# netstat -utnlp | grep :4018
tcp6 0 0 :::4018 :::* LISTEN 29688/maxscale

查看进程
[root@host56 ~]# ps -C maxscale
PID TTY TIME CMD
29688 ? 00:00:00 maxscale

停止服务
[root@host56 ~]# ps -C maxscale
PID TTY TIME CMD
29688 ? 00:00:00 maxscale
[root@host56 ~]# kill -9 29688

6、 测试配置

a在本机访问管理管端口查看监控状态

[root@host6 ~]# maxadmin -P4018 -uadmin -pmariadb
MaxScale> list servers
Servers.
——————-+—————–+——-+————-+——————–
Server | Address | Port | Connections | Status
——————-+—————–+——-+————-+——————–
server1 | 192.168.4.1 | 3306 | 0 | Master, Running
server2 | 192.168.4.2 | 3306 | 0 | Slave, Running
——————-+—————–+——-+————-+——————–
MaxScale> exit
[root@host6 ~]#

b 客户端访问数据读写分离服务器

]#which mysql
]# mysql -h192.168.4.6 -P4006 -uwebuser -p123456
mysql> select @@hostname;
mysql> 执行插入或查询 ( 在host1 和 host2 本机查看记录)

猜你喜欢

转载自blog.csdn.net/weixin_42972553/article/details/82425046