mycat-读写分离

article/2025/9/9 8:03:27

简介:

主从复制的工作机制:
① Master将改变记录到二进制日志(binary log)中,这些记录叫做二进制日志事件(binary log events);
② Slave将master的binary log events拷贝到它的中继日志(relay log);
③ Slave重做中继日志中的事件,将改变反映它自己的数据。

读写分离概念:
基本原理是让主数据库处理事务性增、改、删操作(INSERT、UPDATE、DELETE),而从数据库处理SELECT查询操作。
读写分离的作用:
可以解决数据库写入时影响查询效率的问题

构建读写分离的数据库

ip主机名 / 节点
10.30.59.193mycat / Mycat中间件服务节点
10.30.59.194db1 / MariaDB数据库集群主节点
10.30.59.195db2 / MariaDB数据库集群从节点

一、基础环境配置

(一)、修改主机名

hostnamectl set-hostname mycat
hostnamectl set-hostname db1
hostnamectl set-hostname db2

(二)、 所有节点编辑hosts节点

cat >>/etc/hosts << EOF
10.30.59.193 mycat
10.30.59.194 db1
10.30.59.195 db2
EOF

(三)、所有节点关闭防火墙

#关闭selinx
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config #关闭firewalld
systemctl stop firewalld && systemctl disable firewalld#关闭iptables
iptables -F
iptables -X
iptables -Z
iptables-save

(四)、所有节点配置Yum源

将gpmall-repo文件上传至虚拟机的/opt目录下


#移走yum源
mv /etc/yum.repos.d/* /media/#建立镜像挂载点并挂载镜像 
mkdir /opt/centos                 
mount /dev/sr0 /opt/centos/  #配置yum源
cat /etc/yum.repos.d/local.repo 
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[mariadb]
name=mariadb
baseurl=file:///opt/gpmall-repo
gpgcheck=0
enabled=1

(五)、mycat节点安装JDK环境

yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel[root@mycat ~]# java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)

二、部署MariaDB主从数据库集群服务

(一)、db1节点和db2节点安装mariadb服务

yum install -y mariadb mariadb-server

(二)、db1节点和db2节点启动mariadb服务

systemctl start mariadb
systemctl enable mariadb

(三)、db1节点和db2节点初始化MariaDB数据库

[root@db1 ~]# mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDBSERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.Enter current password for root (enter for none):                #回车
OK, successfully used password, moving on...Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.Set root password? [Y/n] y      
New password:                          #输入数据库root密码000000
Re-enter new password:                 #重复输入密码000000
Password updated successfully!
Reloading privilege tables..... Success!By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.Remove anonymous users? [Y/n] y... Success!Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.Disallow root login remotely? [Y/n] n... skipping.By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.Remove test database and access to it? [Y/n] y- Dropping test database...... Success!- Removing privileges on test database...... Success!Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.Reload privilege tables now? [Y/n] y... Success!Cleaning up...All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.Thanks for using MariaDB!

(四)、配置数据库集群主节点

在配置文件/etc/my.cnf中增添下面的内容:


[mysqld]
log_bin = mysql-bin                       #记录操作日志
binlog_ignore_db = mysql                  #不同步MySQL系统数据库
server_id = 194                           #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

注意:编译完成配置文件,重启mariadb服务

[root@db1 ~]# systemctl restart mariadb

(五)、配置主节点的数据库权限

[root@db1 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.18-MariaDB-log MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by '000000';     #授权在任何客户端机器上可以以root用户登录到数据库
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)]> grant replication slave on *.* to user@'db2' identified by '000000';   #创建一个user用户让从节点db2连接,并赋予从节点同步主节点数据库的权限
Query OK, 0 rows affected (0.001 sec)

(六)、配置从节点的数据库权限

[root@db2 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 15
Server version: 10.3.18-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> change master to master_host='db1',master_user='user',master_password='000000';    #配置从节点连接主节点的连接信息
Query OK, 0 rows affected (0.052 sec)MariaDB [(none)]> start slave;              #开启从节点服务
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)]> show slave status\G         #查看从节点服务状态
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: db1Master_User: userMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000001Read_Master_Log_Pos: 699Relay_Log_File: db2-relay-bin.000002Relay_Log_Pos: 998Relay_Master_Log_File: mysql-bin.000001Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 699Relay_Log_Space: 1305Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 194Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: NoGtid_IO_Pos: Replicate_Do_Domain_Ids: Replicate_Ignore_Domain_Ids: Parallel_Mode: conservativeSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update itSlave_DDL_Groups: 2
Slave_Non_Transactional_Groups: 0Slave_Transactional_Groups: 0
1 row in set (0.000 sec)

(七)、验证主从数据库的同步功能

`主节点db1的数据库中创建库test,并在库test中创建表company,插入表数据`MariaDB [(none)]> create database test;
Query OK, 1 row affected (0.001 sec)MariaDB [(none)]> use test
Database changed
MariaDB [test]> create table company(id int not null primary key,name varchar(50),addr varchar(255));
Query OK, 0 rows affected (0.014 sec)MariaDB [test]>  insert into company values(1,"facebook","usa");
Query OK, 1 row affected (0.003 sec)MariaDB [test]> select * from company;
+----+----------+------+
| id | name     | addr |
+----+----------+------+
|  1 | facebook | usa  |
+----+----------+------+
1 row in set (0.000 sec)`在从节点db2查询test数据库与表company`
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.002 sec)MariaDB [(none)]> select * from test.company;
+----+----------+------+
| id | name     | addr |
+----+----------+------+
|  1 | facebook | usa  |
+----+----------+------+
1 row in set (0.001 sec)可以查询到信息,说明主从数据库集群正常运行。

三、部署Mycat读写分离中间件服务

(一)、安装mycat服务

软件包Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz上传到mycat的/节点root目录下

[root@mycat ~]# tar -zxf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local
[root@mycat ~]# chmod -R 777 /usr/local/mycat/`在/etc/profile系统变量文件中添加Mycat服务的系统变量,并生效变量`
[root@mycat ~]# echo export MYCAT_HOME=/usr/local/mycat/ >> /etc/profile
[root@mycat ~]# source /etc/profile

(二)、编辑Mycat的逻辑库配置文件

[root@mycat ~]# cat >/usr/local/mycat/conf/schema.xml << EOF
> <?xml version="1.0"?>
> <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
> <mycat:schema xmlns:mycat="http://io.mycat/">
> <schema name="USERDB" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1"></schema>     #定义一个逻辑库schema,name为USERDB
> <dataNode name="dn1" dataHost="localhost1" database="test" />          #该逻辑库USERDB对应数据库database为test   
> <dataHost name="localhost1" maxCon="1000" minCon="10" balance="3" dbType="mysql" dbDriver="native" writeType="0" switchType="1"  slaveThreshold="100">  
>     <heartbeat>select user()</heartbeat>
>     <writeHost host="hostM1" url="10.30.59.194:3306" user="root" password="000000">   #设置数据库写入节点为主节点db1
>         <readHost host="hostS1" url="10.30.59.195:3306" user="root" password="000000" />   #设置数据库读取节点为从节点db2
>     </writeHost>
> </dataHost>
> </mycat:schema>
> EOF
 sqlMaxLimit:配置默认查询数量。
 database:为真实数据库名。
 balance="0":不开启读写分离机制,所有读操作都发送到当前可用的writeHost上。
 balance="1":全部的readHost与stand by writeHost参与select语句的负载均衡,简单来说,当双主双从模式(M1->S1,M2->S2,并且M1与M2互为主备),正常情况下,M2、S1、S2都参与select语句的负载均衡。
 balance="2":所有读操作都随机的在writeHost、readhost上分发。
 balance="3":所有读请求随机地分发到wiriterHost对应的readhost执行,writerHost不负担读压力,注意balance=3只在1.4及其以后版本有,1.3版本没有。
 writeType="0":所有写操作发送到配置的第一个writeHost,第一个挂了需要切换到还生存的第二个writeHost,重新启动后已切换后的为准,切换记录在配置文件dnindex.properties中。
 writeType="1":所有写操作都随机的发送到配置的writeHost。

(三)、修改Mycat的逻辑库配置文件权限

[root@mycat ~]# chown root:root /usr/local/mycat/conf/schema.xml 

(四)、编辑mycat的访问用户

修改/usr/local/mycat/conf/server.xml配置文件

`修改root用户的访问密码与数据库,密码设置为123456,访问Mycat的逻辑库为USERDB`····································
····························<user name="root"><property name="password">123456</property><property name="schemas">USERDB</property>`删除如下几行内容`<user name="user"><property name="password">user</property><property name="schemas">TESTDB</property><property name="readOnly">true</property></user>

(五)启动Mycat服务

[root@mycat ~]# /bin/bash /usr/local/mycat/bin/mycat start

使用netstat -ntpl命令查看虚拟机端口开放情况,如果有开放8066和9066端口,则表示Mycat服务开启成功。
在这里插入图片描述

四、验证数据库集群服务读写分离功能

(一)、在mycat节点上安装MariaDB-client服务。

yum install -y MariaDB-client

(二)、在Mycat节点使用mysql命令查看Mycat服务的逻辑库USERDB

[root@mycat ~]# mysql -h127.0.0.1 -P8066 -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB)Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MySQL [(none)]> show databases;    #查看所有库
+----------+
| DATABASE |
+----------+
| USERDB   |
+----------+
1 row in set (0.002 sec)MySQL [(none)]> use USERDB
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
MySQL [USERDB]> show tables;     # 查看所有表
+----------------+
| Tables_in_test |
+----------------+
| company        |
+----------------+
1 row in set (0.002 sec)MySQL [USERDB]> select * from company;  
+----+----------+------+
| id | name     | addr |
+----+----------+------+
|  1 | facebook | usa  |
+----+----------+------+
1 row in set (0.055 sec)

(三)、用Mycat服务添加表数据

MySQL [USERDB]>  insert into company values(2,"bastetball","usa");
Query OK, 1 row affected (3.541 sec)MySQL [USERDB]> select * from company;
+----+------------+------+
| id | name       | addr |
+----+------------+------+
|  1 | facebook   | usa  |
|  2 | bastetball | usa  |
+----+------------+------+
2 rows in set (0.002 sec)

(四)、验证Mycat服务对数据库读写操作分离

在mycat节点使用mysql命令,通过9066端口查询对数据库读写操作的分离信息。

[root@mycat ~]#  mysql -h127.0.0.1 -P9066 -uroot -p123456 -e 'show  @@datasource;'

在这里插入图片描述


http://chatgpt.dhexx.cn/article/UEx6ZkB0.shtml

相关文章

使用mycat进行读写分离

目录 一、读写分离 1.1、什么是读写分离 1.2、为什么要用读写分离 1.3、使用mycat读写分离 1.3.1安装mycat 1.3.2、配置mycat 简单的理解&#xff0c;mycat就是一个数据中间件&#xff0c;可以让开发人员直接访问mycat&#xff0c;然后由mycat去访问具体的数据库服务器&a…

数据库:mycat实现读写分离

目录 一、mycat 1、mycat实现读写分离原理 2、mycat应用场景 3、mycat作用 4、mycat实现读写分离实战 一、mycat 1、mycat实现读写分离原理 ①用户进行读操作则由mycat转给配置的从数据库。 ②用户进行写操作则由mycat转给配置的主数据库。 ③转发规则由mycat配置文件中…

mycat实现读写分离

1、在本地复制多一个数据库&#xff08;有条件拥有多个服务器的忽略&#xff09; 1&#xff09;找到mysql实例文件夹和数据文件夹&#xff08;通常在C:\Program Files下和C:\ProgramData下&#xff09; 直接都复制一份出来做从库 2&#xff09;修改从库配置my.ini [client]…

汉诺塔递归算法(Python编程)

一、问题描述。 汉诺塔是学习计算机递归算法的经典入门案例&#xff0c;是一个数学难题。其问题为如何将所有圆盘从A移动到C&#xff0c;要求一次只能移动一个盘子&#xff0c;盘子只能在3个标杆&#xff08;A/B/C&#xff09;之间移动&#xff0c;更大的盘子不能放在更小的盘子…

汉诺塔递归调用(C语言实现)

预备知识 1.递归算法 递归算法&#xff1a;是一种直接或者间接地调用自身的算法。在计算机编写程序中&#xff0c;递归算法对解决一大类问题是十分有效的&#xff0c;它往往使算法的描述简洁而且易于理解。 递归过程一般通过函数或子过程来实现。 递归算法的实质&#xff1…

python实现汉诺塔递归算法超详细过程

python实现汉诺塔递归算法 def hanoi(n, x, y, z):if n 1:print(x, -->, z)else:hanoi(n-1, x, z, y)print(x, -->, z)hanoi(n-1, y, x, z)n int(input(请输入层数&#xff1a;)) hanoi(n, x, y, z) if n 1: #如果n等于1时print(x, -->, z) #直接将x移…

多图详解汉诺塔递归实现思路--含实现代码

前言 为了节约大家的时间&#xff0c;本文对汉诺塔的定义就不做赘述了&#xff0c;如果有小伙伴不清楚汉诺塔的规则可以直接点蓝字跳转过去。 本篇博客内容 汉诺塔实现的思路用递归的方式实现汉诺塔 汉诺塔实现的思路 我们先以两个瓷盘为例&#xff1a; 由于小瓷盘1位于顶部&a…

汉诺塔递归算法C++实现

算法介绍 其实算法非常简单&#xff0c;当盘子的个数为n时&#xff0c;移动的次数应等于2^n – 1&#xff08;有兴趣的可以自己证明试试看&#xff09;。后来一位美国学者发现一种出人意料的简单方法&#xff0c;只要轮流进行两步操作就可以了。首先把三根柱子按顺序排成品字型…

汉诺塔递归算法(C语言)

汉诺塔递归算法 一、引言二、解决汉诺塔问题&#xff08;1&#xff09;递归算法 三、C语言实现&#xff08;1&#xff09;代码实现&#xff08;2&#xff09;算法思想 四、结论 一、引言 汉诺塔问题是一个经典的数学谜题&#xff0c;它是在印度流传下来的&#xff0c;传说中有…

汉诺塔递归调用

1.递归算法 递归算法&#xff1a;是一种直接或者间接地调用自身的算法。在计算机编写程序中&#xff0c;递归算法对解决一大类问题是十分有效的&#xff0c;它往往使算法的描述简洁而且易于理解。 递归过程一般通过函数或子过程来实现。 递归算法的实质&#xff1a;是把问题转…

汉诺塔递归的空间复杂度_算法之美:解读递归算法原理和效率

对于很多人来说,都知道递归,也能看的懂递归,但在实际项目过程中,却不知道如何使用递归,这里给递归做个总结。 递归的定义 在数学与计算机科学中,递归(Recursion)是指在函数的定义中使用函数自身的方法。实际上,递归,顾名思义,其包含了两个意思:递和归,这正是递归思想…

汉诺塔递归问题的分析与Python实现

背景 相传在古印度圣庙中&#xff0c;有一种被称为汉诺塔(Hanoi)的游戏。该游戏是在一块铜板装置上&#xff0c;有三根杆(编号A、B、C)&#xff0c;在A杆自下而上、由大到小按顺序放置64个金盘(如图)。游戏的目标&#xff1a;把A杆上的金盘全部移到C杆上&#xff0c;并仍保持原…

汉诺塔递归算法/搬金盘的婆罗门 - Python实现

汉诺塔递归算法/搬金盘的婆罗门 - Python实现 本文引用自作者编写的下述图书; 本文允许以个人学习、教学等目的引用、讲授或转载&#xff0c;但需要注明原作者"海洋饼干叔 叔"&#xff1b;本文不允许以纸质及电子出版为目的进行抄摘或改编。 1.《Python编程基础及应用…

汉诺塔递归的空间复杂度_学习算法绕不开的~~汉诺塔

大家好,我是老郝。本文就汉诺塔问题向大家阐述递归的思想。 【问题描述】 有三根柱子,最左边的柱子上从大到小放着很多的圆盘,要求把圆盘一个一个的放到最右边的柱子上并且只能小盘子压在大盘子上。(据说古代阿三要他们的和尚把64个圆盘从左到右放一遍,看到最后你就知道阿三…

汉诺塔递归的空间复杂度_【干货】Java算法复杂度

同一问题可用不同算法解决&#xff0c;而一个算法的质量优劣将影响到算法乃至程序的效率。算法分析的目的在于选择合适算法和改进算法。 算法复杂度分为时间复杂度和空间复杂度。其作用&#xff1a; 时间复杂度是度量算法执行的时间长短&#xff1b;而空间复杂度是度量算法所需…

汉诺塔递归问题

汉诺塔问题&#xff1a; 这是一道著名的算法题&#xff0c;也是递归思想的典型体现。 可以总结&#xff0c;当圆盘数为n时&#xff0c;将最下层圆盘和其余上部份所有圆盘看作两个整体&#xff0c;则满足以下步骤&#xff1a; 1、把n-1个圆盘从A经过C移动到B 2、把第n个圆盘从…

汉诺塔递归算法python详细解析图_汉诺塔递归算法的图解(自我总结)

汉诺塔介绍 汉诺塔简单介绍&#xff1a; 有三根相邻的柱子&#xff0c;假定从左到右为A,B,C&#xff0c;A柱子上从下到上按金字塔状叠放着n个不同大小的圆盘&#xff0c;要把所有盘子一个一个移动到柱子B上&#xff0c;并且每次移动同一根柱子上都不能出现大盘子在小盘子上方。…

C#汉诺塔递归算法实现

目录: 一、什么是递归1.先来看一下一个递归的例子2.递归的基本原理 二、汉诺塔问题1.汉诺塔的故事2.回到编程&#xff0c;汉诺塔问题主要就是解决这个问题&#xff1a;3.怎么解决汉诺塔问题要解决汉诺塔问题就要用到递归思想&#xff0c;这里拿四层汉诺塔举例子&#xff1a; 4.…

递归算法 —— Hanoi汉诺塔游戏

前言 博客主页&#xff1a;干脆面la的主页 gitte链接&#xff1a;干脆面la的gitee仓库 刚学习完递归函数接触汉诺塔问题的时候&#xff0c;汉诺塔问题困扰了我很久。博主花了很长时间理解这道题目&#xff0c;因此整理出了用递归解决汉诺塔问题的思路&#xff0c;希望对大家有所…

计算任意位数的黑洞数

黑洞数是指这样的整数&#xff1a; 由这个数字每位数字组成的最大数减去每位数字组成的最小数仍然得到这个数自身。 例如3位黑洞数是495&#xff0c;因为954-459495&#xff0c;4位数字是6174&#xff0c;因为7641-14676174。 def max( x ):data[]while x/1!0:kx%10xx//10data.…