【技术】MySQL数据读写分离怎么实现

news2024/9/30 13:25:42

、概念:

  • MySQL数据读写分离是存储数据的一种服务架构
  • 执行select命令必须连接 slave角色服务器
  • 执行insert命令必须连接 maste角色服务器
  • 提供数据读写分离功能的中间件软件有: mysql-proxy maxscale mycat
  • 拓扑架构只支持一主一从或者一主多从架构

二、实现读写分离的拓扑图:

三、MaxScale相关配置:

指令/路径/...说明
maxscale-2.1.2-1.rhel.7.x86_64.rpm软件包
/etc/maxscale.cnf主配置文件
maxscale /etc/maxscale.cnf启动服务
/var/log/maxscale/maxscale.log日志路径(可查看报错信息)
4006读写分离服务使用端口号
4016管理服务使用端口号

四、读写分离的配置流程:

  • 配置Mysql服务器一主一从
  • 配置代理服务器(读写分离服务器)
  • 启动读写分离服务
  • 客户机50测试配置读写分离服务的配置

五、实操:

第一步:配置Mysql服务器一主一从

  • 把host61配置为master数据库服务器
[root@host61 ~]# vim /etc/my.cnf
[mysqld]
Server_id = 61
log_bin=master61
:wq

[root@host61 ~]# systemctl restart mysqld

[root@host61 ~]# mysql -uroot –p123qqq...A
Mysql>   grant replication slave on  *.* to repluser@"%" identified by "123qqq...A";
Mysql>  show  master status ;
+-----------------+----------+--------------+------------------+-------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master61.000001 |      441 |              |                  |                   |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
  • 把host62 配置为slave数据库服务器
[root@host62 ~]# vim /etc/my.cnf
[mysqld]
Server_id = 62
:wq

[root@host62 ~]# systemctl  restart  mysqld

[root@host62 ~]# mysql -uroot -p密码
Mysql> change master to  master_host="192.168.88.61" ,
Master_user="repluser" ,  
Master_password="123qqq...A" ,
Master_log_file="master61.000001" ,
Master_log_pos=441 ;

Mysql>  start  slave;

Mysql> show  slave status \G
             Slave_IO_Running: Yes
             Slave_SQL_Running: Yes

第二步:配置代理服务器(读写分离服务器)

  • 安装软件
[root@host60 ~]# yum -y install maxscale-2.1.2-1.rhel.7.x86_64.rpm  
  • 修改主配置文件
[root@host60 ~]# cp /etc/maxscale.cnf /root/  备份主配置文件
[root@host60 ~]# vim /etc/maxscale.cnf

[maxscale]
threads=auto # 服务启动后线程的数量,根据CPU 核数创建

[server1]    
type=server
address=192.168.88.61 # 指定第1台数据库服务器的ip地址
port=3306
protocol=MySQLBackend

[server2]   
type=server
address=192.168.88.62 # 指定第2台数据库服务器的ip地址
port=3306
protocol=MySQLBackend

[MySQL Monitor]   # 定义监视的数据库服务器
type=monitor
module=mysqlmon
servers=server1,server2    # 监视server1和server2
user=mysqla      # 监控用户账号
passwd=123qqq...A  # 监控用户连接密码
monitor_interval=10000

#禁止只读服务(注释)
#[Read-Only Service]
#type=service
#router=readconnroute
#servers=server1
#user=myuser
#passwd=mypwd
#router_options=slave

[Read-Write Service]   # 启用读写分离服务
type=service
router=readwritesplit
servers=server1,server2   # 读写分离在server1和server2服务器之间进行
user=mysqlb   # 路由用户
passwd=123qqq...A  # 连接密码
max_slave_connections=100%

[MaxAdmin Service]   # 管理服务(通过访问管理服务可以查看监控信息)
type=service
router=cli

# 因为只读服务没有启用 ,不需要定义服务使用的端口号(注释)
#[Read-Only Listener]
#type=listener
#service=Read-Only Service
#protocol=MySQLClient
#port=4008

[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=4016  # 端口号
:wq
  • 配置数据库服务器(在数据库服务器上添加监控用户和路由用户)
  • 注意:因为是主从结构 ,所以只需要在主服务器添加,从服务器会自动同步
[root@host61 ~]# mysql -uroot -p123qqq...A                     

# 添加监控用户 mysqla 用户
mysql> grant  replication   slave , replication  client  on  *.*  to  
mysqla@"%" identified by "123qqq...A";
                
# 权限说明:
# replication client   监视数据库服务的运行状态  
# replication slave      数据库服务器的主从角色    

# 添加路由用户 mysqlb 用户
mysql> grant  select on  mysql.*  to  mysqlb@"%" identified by "123qqq...A";  # 对授权库下的表有查询权限

# 在从服务器查看用户是否同步
[root@host62 ~]# mysql -uroot -p123qqq...A 
select user from mysql.user where user="mysqla";
select user from mysql.user where user="mysqlb";

第三步:启动读写分离服务

  • 验证数据库服务器的授权用户 mysqla 和 mysqlb
# 安装提供mysql命令的软件
[root@host60 ~]# which  mysql || yum -y install mariadb   
[root@host60 ~]# mysql -h192.168.88.61 -umysqla -p123qqq...A
[root@host60 ~]# mysql -h192.168.88.62 -umysqla -p123qqq...A
[root@host60 ~]# mysql -h192.168.88.61 -umysqlb -p123qqq...A
[root@host60 ~]# mysql -h192.168.88.62 -umysqlb -p123qqq...A
# 说明:能连接成功才是对的,如果连接失败:执行如下操作
# 在主数据库服务器host61 把添加 mysqla用户 和 mysqlb 用户的命令再执行一遍

# 启动服务 
[root@host60 ~]# maxscale   /etc/maxscale.cnf     
# 查看日志文件
[root@host60 ~]# ls /var/log/maxscale/   
maxscale.log 
# 查看读写分离服务端口号
[root@host60 ~]# netstat  -utnlp  | grep 4006     
tcp6       0      0 :::4006       :::*            LISTEN      1580/maxscale       
# 查看读写分离服务端口号
[root@host60 ~]# netstat  -utnlp  | grep 4016  
tcp6       0      0 :::4016       :::*        LISTEN      1580/maxscale       


#把服务杀死 再启动  相当于重启服务 (修改了配置文件后要重启服务使其配置生效)
# 通过杀进程的方式停止服务 
[root@host60 ~]# killall -9 maxscale   
# 启动服务
[root@host60 ~]# maxscale  /etc/maxscale.cnf     
# 在host60本机访问管理服务查看数据库服务的监控信息
[root@host60 ~]# maxadmin -uadmin -pmariadb -P4016
MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server             | Address         | Port  | Connections | Status              
-------------------+-----------------+-------+-------------+--------------------
server1            | 192.168.88.61    |  3306 |           0 | Master, Running
server2            | 192.168.88.62    |  3306 |           0 | Slave, Running
-------------------+-----------------+-------+-------------+--------------------
MaxScale> exit      
  • 排错方法 : 查看日志里的报错信息 vim /var/log/maxscale/maxscale.log

第四步:测试配置读写分离服务的配置

  • 客户端能够连接读写分离服务器访问数据库服务

# 首先在主数据库服务器host61 添加客户端连接使用的用户
[root@host61 ~]# mysql -uroot -p密码 
create database  bbsdb;
create  table bbsdb.a(id int);
grant select,insert on bbsdb.* to  yaya@"%"  identified by "123qqq...A";

# 在从服务器host62查看存储数据库表和添加用户
[root@host62 ~]# mysql -uroot -p密码
desc  bbsdb.a;
select  user from mysql.user where user="yaya";
                    
# 客户端host50连接读写分离服务器host60访问数据库服务
mysql -h读写分离服务器的ip   -P读写分离服务的端口号  -u数据库授权用户名  -p密码 
[root@host50 ~]# mysql -h192.168.88.60 -P4006 -uyaya  -p123qqq...A  
  • 连接读写分离服务后,可以对数据做查询和存储操作
mysql> select  * from bbsdb.a;
Empty set (0.00 sec)

mysql> insert into bbsdb.a values(8888);
Query OK, 1 row affected (0.06 sec)

mysql> select  * from bbsdb.a;
+------+
| id   |
+------+
| 8888 |
+------+
1 row in set (0.00 sec)

第五步:验证

  • 怎么验证查询select 访问就在host62从服务器获取的数据呢?
  • 在从服务本机向表里添加1条记录(在从服务添加的新数据主服务器不会同步)
# 从服务器插入1条数据
[root@host62 ~]# mysql -uroot -p123qqq...A -e 'insert into bbsdb.a values(6262)'
[root@host62 ~]# mysql -uroot -p123qqq...A -e 'select * from bbsdb.a'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+
| id   |
+------+
| 8888 |
| 6262 |
+------+

# 主服务器查询
[root@host11 ~]# mysql -uroot -p123qqq...a  -e 'select  * from bbsdb.a'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+
| id   |
+------+
| 8888 |
+------+

# 客户端访问读写分离服务器查询数据(查询结果为从服务器数据源)        
[root@host50 ~]# mysql -h192.168.88.60 -P4006 -uyaya  -p123qqq...A -e 'select * from bbsdb.a'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+
| id   |
+------+
| 8888 |
| 6262 |
+------+
  • 怎么验证存储数据insert 访问 就是存储在了主机服务器host61上?

# 客户端机插入数据
[root@host50 ~]# mysql -h192.168.88.60 -P4006 -uyaya  -p123qqq...A -e 'insert into bbsdb.a values(666)' 

# 在主服务器本机查看数据
[root@host61 ~]# mysql -uroot -p123qqq...a  -e 'select  * from bbsdb.a'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+
| id   |
+------+
| 8888 |
|  666 |
+------+

[root@host50 ~]# mysql -h192.168.88.60 -P4006 -uyaya  -p123qqq...A -e 'select * from bbsdb.a'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+
| id   |
+------+
| 8888 |
| 6262 |
|  666 |
+------+
  • 还可以通过查看主服务器的position是否在客户端服务器插入数据后改动来确定是不是在主服务器中进行操作过数据。
  • 注:文中部分内容来源于网络

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1395767.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Docker搭建MySQL主从数据库-亲测有效

1、测试环境概述 1、使用MySQL5.7.35版本 2、使用Centos7操作系统 3、使用Docker20版本 案例中描述了整个测试的详细过程 2、安装Docker 2.1、如果已经安装docker,可以先卸载 yum remove -y docker \ docker-client \ docker-client-latest \ docker-common \ docker-l…

Spring-BeanPostProcessor PostConstruct init InitializingBean 执行顺序

执行顺序探究 新建一个对象用于测试 Component public class Student implements InitializingBean {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}pu…

Red Hat Enterprise Linux 7.9 安装图解

引导和开始安装 选择倒计时结束前,通过键盘上下键选择下图框选项,启动图形化安装过程。 安装语言选择 这里要区分一下,当前选中的语言作为安装过程中安装器所使用的语言,这里我们选择中文简体。不过细心的同学可能发现&#xff0c…

Js-WebAPIs-事件流(三)

• 事件流与两个阶段说明 事件流指的是事件完整执行过程中的流动路径 说明:假设页面里有个div,当触发事件时,会经历两个阶段,分别是捕获阶段、冒泡阶段 简单来说:捕获阶段是 从父到子 冒泡阶段是从子到父 实际工作都是…

Debian系统写Mysql时中文出现乱码无法定入的问题解决方案

原因是操作系统可能精简安装,没有GBK字符集,只有UTF8在转换或使用的时候有问题。 使用locale -a查看系统支持的字符集。正常的比较全的字符集的操作系统如下: 有问题的操作系统字符集如下: 解决方案: 步骤1&#…

LiveNVR监控流媒体Onvif/RTSP功能-服务接收RTSP推流RTSPServer可以拉转配置到通道中视频直播

LiveNVR服务接收RTSP推流RTSPServer可以拉转配置到通道中视频直播 1、需求背景2、开启RTSP2.1、基础配置配置RTSP端口 3、获取RTSP推流地址3.1、RTSP推流地址格式3.3、RTSP推流地址示例 4、配置设备推流5、配置拉转RTSP5.1、直播流地址格式5.2、直播流地地址示例5.3、通道配置直…

滑雪挑战H5小游戏

欢迎来到程序小院 滑雪挑战 玩法&#xff1a;点击鼠标左键控制人物滑雪的方向&#xff0c;注意不同的弯道&#xff0c;滑雪过程中会有⭐️&#xff0c;看你会获得⭐️&#xff0c;快去滑雪吧^^。开始游戏https://www.ormcc.com/play/gameStart/254 html <canvas id"c…

文件系统和IO流

目录 ​文件系统和IO流 一:文件的认知 认识文件 树型结构组织和⽬录: 文件路径&#xff08;Path): 文件形式: 二:File的方法 File的概述: File的属性 File的构造方法 File常用的get系列方法 ⽰例一:观察get系列的特点和差异 File常用的增,删方法 示例二:普通文件…

基于振弦采集仪的地下工程振动监测技术研究

基于振弦采集仪的地下工程振动监测技术研究 地下工程振动监测技术是为了监测地下工程施工过程中产生的振动而进行的研究。振弦采集仪是一种常用的地下工程振动监测设备&#xff0c;它通过固定在地下工程附近的振弦仪来实时采集工程施工过程中产生的振动信号。 基于振弦采集仪的…

5.文本文件编辑命令

1.cat 用于查看纯文本文件&#xff08;内容较少的&#xff09; 加上-n参数&#xff0c;显示内容加行号 [rootlocalhost ~]# cat -n /etc/sysconfig/network-scripts/ifcfg-ens160 2.more 用于查看纯文本文件&#xff08;内容较多的&#xff09; 还可以使用空格键或回车 键…

Qt/C++中英输入法/嵌入式输入法/小数字面板/简繁切换/特殊字符/支持Qt456

一、前言 在嵌入式板子上由于没有系统层面的输入法支持&#xff0c;所以都绕不开一个问题&#xff0c;那就是在需要输入的UI软件中&#xff0c;必须提供一个输入法来进行输入&#xff0c;大概从Qt5.7开始官方提供了输入法的源码&#xff0c;作为插件的形式加入到Qt中&#xff…

Spring Boot整合Druid(druid 和 druid-spring-boot-starter)

引言 在现代的Web应用开发中&#xff0c;高性能的数据库连接池是确保应用稳定性和响应性的关键因素之一。Druid是一个开源的高性能数据库连接池&#xff0c;具有强大的监控和统计功能&#xff0c;能够在Spring Boot应用中提供出色的数据库连接管理。本文将研究在Spring Boot中…

L1-048 矩阵A乘以B(Java)

给定两个矩阵A和B&#xff0c;要求你计算它们的乘积矩阵AB。需要注意的是&#xff0c;只有规模匹配的矩阵才可以相乘。即若A有Ra行、Ca列&#xff0c;B有Rb行、Cb列&#xff0c;则只有Ca与Rb相等时&#xff0c;两个矩阵才能相乘。 输入格式&#xff1a; 输入先后给出两个矩阵…

Redis--Geo指令的语法和使用场景举例(附近的人功能)

文章目录 前言Geo介绍Geo指令使用使用场景&#xff1a;附近的人参考文献 前言 Redis除了常见的五种数据类型之外&#xff0c;其实还有一些少见的数据结构&#xff0c;如Geo&#xff0c;HyperLogLog等。虽然它们少见&#xff0c;但是作用却不容小觑。本文将介绍Geo指令的语法和…

jrebel IDEA 热部署

1 下载 2022.4.1 JRebel and XRebel - IntelliJ IDEs Plugin | Marketplace 2 选择下载好的zip 离线安装IDEA 插件 重启IDEA 3 打开 [Preference -> JRebel & XRebel] 菜单&#xff0c;输入 GUID address 为 https://jrebel.qekang.com/1e67ec1b-122f-4708-87d…

尚硅谷Nginx高级配置笔记

写在前面&#xff1a;本笔记是学习尚硅谷nginx可成的时候的笔记&#xff0c;不是原创&#xff0c;如有需要&#xff0c;可以去官网看视频&#xff0c;以下是pdf文件 Nginx高级 第一部分&#xff1a;扩容 通过扩容提升整体吞吐量 1.单机垂直扩容&#xff1a;硬件资源增加 云…

[HTML]Web前端开发技术14(HTML5、CSS3、JavaScript )鼠标经过图片显示大图 网页标题:表格标签的综合应用——喵喵画网页

希望你开心&#xff0c;希望你健康&#xff0c;希望你幸福&#xff0c;希望你点赞&#xff01; 最后的最后&#xff0c;关注喵&#xff0c;关注喵&#xff0c;关注喵&#xff0c;佬佬会看到更多有趣的博客哦&#xff01;&#xff01;&#xff01; 喵喵喵&#xff0c;你对我真的…

Flink TaskManager内存管理机制介绍与调优总结

内存模型 因为 TaskManager 是负责执行用户代码的角色&#xff0c;一般配置 TaskManager 内存的情况会比较多&#xff0c;所以本文当作重点讲解。根据实际需求为 TaskManager 配置内存将有助于减少 Flink 的资源占用&#xff0c;增强作业运行的稳定性。 TaskManager 内…

Docker(二)安装指南:主要介绍 Docker 在 Linux 、Windows 10 和 macOS 上的安装

作者主页&#xff1a; 正函数的个人主页 文章收录专栏&#xff1a; Docker 欢迎大家点赞 &#x1f44d; 收藏 ⭐ 加关注哦&#xff01; 安装 Docker Docker 分为 stable test 和 nightly 三个更新频道。 官方网站上有各种环境下的 安装指南&#xff0c;这里主要介绍 Docker 在…

没有统计学背景的人学六西格玛:挑战与机遇并存

在当今追求高效率、高品质的时代&#xff0c;六西格玛方法已成为企业提升竞争力的关键法宝。但对于众多没有统计学背景的朋友来说&#xff0c;这一方法仿佛高不可攀。今天&#xff0c;就让我们一同探索&#xff0c;没有统计学背景的人学习六西格玛到底难不难&#xff1f; 一、难…