• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Java语言编写的数据库间件Mycat

武飞扬头像
星光落入你灰蒙蒙的眼
帮助1

一个新颖的数据库中间件产品支持mysql集群,或者mariadb cluster,提供高可用性数据分片集群。基于Java语言编写的数据库中间件

什么是MyCat

MyCat是一个开源的分布式数据库系统,是一个实现了MySQL协议的服务器,前端用户可以把它看作是一个数据库代理,用MySQL客户端工具和命令行访问,而其后端可以用MySQL原生协议与多个MySQL服务器通信,也可以用JDBC协议与大多数主流数据库服务器通信。其核心功能是分表分库,即将一个大表水平分割为N个小表,存储在后端MySQL服务器里或者其他数据库里。配合数据库的主从模式还可实现读写分离。
学新通

mycat特点

•一个用于MySQL读写分离和与数据切分的高可用中间件
•一个模拟为MySQLServer的超级数据库代理
•一个能平滑扩展支持1000亿大表的分布式数据库系统 (普通单表1kw以下)
•一个可管控多种关系数据库的数据库路由器

实验步骤

Mariadb1 192.168.1.1
M2 192.168.1.2
M3 192.168.1.3
MyCat 192.168.1.10

MyCat的读写分离是建⽴在MySQL主从复制基础之上实现的,所以必须先搭建MySQL的主从复制

1.三台安装数据库

[root@localhost ~]# yum -y install mariadb-server mariadb-devel mariadb
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload

2.主

[root@localhost ~]# vim /etc/my.cnf
[mysqld]
server-id=1
log-bin=mysql-bin
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# mysql -u root -p
Enter password: 
MariaDB [(none)]> set password=password("123.com");
grant replication slave on *.* to slave@'192.168.1.%' identified by '123.com';
flush privileges;
MariaDB [(none)]> show master status;
 ------------------ ---------- -------------- ------------------ 
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
 ------------------ ---------- -------------- ------------------ 
| mysql-bin.000003 |      610 |              |                  |
 ------------------ ---------- -------------- ------------------ 
1 row in set (0.00 sec)
学新通

3.从-1

[root@localhost ~]# vim /etc/my.cnf
[mysqld]
server-id=2
log-bin=mysql-bin1
relay-log=relay-log-bin
relay-log-purge=0	   #如果一主多从,就需要这样配置(防止从节点变为主节点删除)
read-only=1			   #中继日志
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# mysql -u root -p
set password=password('123.com');
grant replication slave on *.* to slave@'192.168.1.%' identified by '123.com';
flush privileges;
MariaDB [(none)]> change master to master_host="192.168.1.1",
master_user="slave",
master_password="123.com",
master_log_file="mysql-bin.000003",
master_log_pos=610;
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G;
学新通

3.从-2

[root@localhost ~]# vim /etc/my.cnf
[mysqld]
server-id=3
log-bin=mysql-bin2
relay-log=relay-log-bin
relay-log-purge=0
read-only=1
[root@localhost ~]# systemctl start mariadb
···授权操作同上

4.配置MyCat

tar -zxf Mycat-server-1.6.7.6-release-20210730131311-linux.tar.gz -C /usr/src/
mv /usr/src/mycat/ /usr/local/mycat
yum -y install java-1.8.0-openjdk-devel &>/dev/null
ln -s /usr/local/mycat/bin/* /usr/local/bin/

MyCat账号密码和数据库名:
[root@localhost ~]# vim /usr/local/mycat/conf/server.xml

110         <user name="root" defaultAccount="true">			#读写功能的用户
111                 <property name="password">123456</property>
112                 <property name="schemas">mycatdb</property>
113                 <property name="defaultSchema">mycatdb</property>	#用户查看到数据库的名称    前段的数据库名称
114                 <!--No MyCAT Database selected 错误前会尝试使用该schema作为schema,不设置则为null,报错 -->
127         <user name="user">		//连接mycat实现只读功能的用户
128                 <property name="password">user</property>
129                 <property name="schemas">mycatdb</property>
130                 <property name="readOnly">true</property>
131                 <property name="defaultSchema">mycatdb</property>
132         </user>
user节点配置用户信息,详细说明如下:
user   用户配置节点
--name 登录的用户名,也就是连接Mycat的用户名
--password 登录的密码,也就是连接Mycat的密码
--schemas  数据库名,这里会和schema.xml中的配置关联,多个用逗号分开。例如需要这个用户需要管理两个数据库db1,db2,则配置db1,db2
学新通

Mariadb授权(主从)

MariaDB [(none)]> grant all on *.* to root@'192.168.1.10' identified by '123.com';
MariaDB [(none)]> flush privileges;

在 schema.xml 文件中配置读写分离:
[root@localhost ~]# vim /usr/local/mycat/conf/schema.xml

 <!-- 数据库配置,与server.xml中的数据库对应 -->
  5         <schema name="mycatdb" checkSQLschema="false" sqlMaxLimit="100" randomDataNode="dn1">
 <!-- 分片配置 -->
 17         <dataNode name="dn1" dataHost="localhost1" database="test" />
 <!-- 物理数据库配置 -->
 24         <dataHost name="localhost1" maxCon="1000" minCon="10" balance="3"
 25                           writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
 26                 <heartbeat>select user()</heartbeat
 28                 <writeHost host="hostM1" url="192.168.1.1:3306" user="root"
 29                                    password="123456">
 30                 <readHost host="hostS1" url="192.168.1.2:3306" user="root" password="123456" />
 31                 <readHost host="hostS2" url="192.168.1.3:3306" user="root" password="123456" />
 32                 </writeHost>
 33                  <writeHost host="hostM2" url="192.168.1.2:3306" user="root" password="123456"/>
 34                  <writeHost host="hostM3" url="192.168.1.3:3306" user="root" password="123456"/>
 35         </dataHost>
[root@localhost ~]# mycat start
[root@localhost ~]# mycat status
Mycat-server is running (8851).
[root@localhost ~]# yum -y install mariadb-server
[root@localhost ~]# mysql -u root -p123456 -h 192.168.1.10 -P 8066   mysql -u 用户名 -p 密码 -hmycat 主机 IP -P8066
MySQL [(none)]> show databases;
 ---------- 
| DATABASE |
 ---------- 
| mycatdb  |
 ---------- 
1 row in set (0.00 sec)

----------->schema.xml参数说明
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。
writeType="2",没实现。
-1 表示不自动切换
1 默认值,自动切换
2 基于MySQL 主从同步的状态决定是否切换
学新通

5.验证

1.mycat上创建表

MySQL [(none)]> use mycatdb
Database changed
MySQL [mycatdb]> create table aa (id int);

去主上查看
MariaDB [(none)]> use test
MariaDB [test]> show tables;

2.读取数据

在从上创建数据:

MariaDB [(none)]> use test;
MariaDB [test]> create table slave1 (id int);
MariaDB [(none)]> use test;
MariaDB [test]> create table slave2 (id int);

mycat:

MySQL [mycatdb]> show tables;
 ---------------- 
| Tables_in_test |
 ---------------- 
| aa             |
| slave1         |
 ---------------- 
MySQL [mycatdb]> show tables;
 ---------------- 
| Tables_in_test |
 ---------------- 
| aa             |
| slave2         |
 ---------------- 

3.验证高可用

主停止:

[root@localhost ~]# systemctl stop mariadb
MySQL [(none)]> use mycatdb;
MySQL [mycatdb]> create table bb (id int);
Query OK, 0 rows affected (0.01 sec)

Sometimes, you have to go alone. It’s not loneliness, it’s choice.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhiejakg
系列文章
更多 icon
同类精品
更多 icon
继续加载