接上一篇:企业实战_10_MyCat Mysql 主复制总结
https://gblfy.blog.csdn.net/article/details/118652742
文章目录
- 一、准备工作
- 1. 实现思路
- 2. 配置MyCat垂直分库
- 3. 环境配置
- 4. 在master节点创建mycat用户
- 5. 赋予操作数据库权限
- 二、schema.xml文件配置
- 2.1. 基础模板 样例
- 2.2. 实际配置
- 2.3. dataHost标签
- 2.4. dataNode标签
- 2.5. schema标签
- 三、server.xml文件配置
- 四、启动mycat调试
- 4.1. 启动mycat
- 4.2. 启动mycat异常
- 4.2. 解决方案
- 4.3. 重新启动mycat
- 4.4. Mycat验证配置
- 4.5. 操作日志
一、准备工作
1. 实现思路
第一步 | 由后台3个物理数据库抽取为一个逻辑数据路 |
---|---|
第二步 | 各个数据节点只保存自己模块的表数据 |
第三步 | 删除冗余数据 |
2. 配置MyCat垂直分库
步骤 | 说明 |
---|---|
① | 使用schema.xml配置逻辑库 |
② | 使用server.xml配置系统变量及用户权限 |
③ | 由于没有用到水平分片顾不许配置rule.xml |
3. 环境配置
主机名 | IP地址 | 角色 | 数据库 |
---|---|---|---|
node1 | 192.168.92.101 | MYSQL、mycat | imooc_db(主机物理) |
node2 | 192.168.92.102 | MYSQL | order_db(从机物理) |
node3 | 192.168.92.103 | MYSQL | product_db(主机物理) |
node4 | 192.168.92.104 | MYSQL | customer_db(从机物理) |
4. 在master节点创建mycat用户
create user im_mycat@'192.168.92.%' identified by '123456';
5. 赋予操作数据库权限
grant select,insert,update,delete,execute on *.* to im_mycat@'192.168.92.%';
注:在主机上创建用户,从机也会创建,这个用户是让mycat来操作数据库的
二、schema.xml文件配置
2.1. 基础模板 样例
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/"><!-- 配置逻辑库和逻辑表 --><schema name="逻辑库" checkSQLschema="false" sqlMaxLimit="100"><table name="逻辑表" primaryKey="逻辑表主键" dataNode="数据节点名"/></schema><!-- 配置数据节点 --><dataNode name="数据节点名" dataHost="主机节点名" database="物理数据库" /><!-- 配置主机节点 --><dataHost name="物理数据库主机节点" maxCon="1000" minCon="10" balance="3" writeType="0" dbType="数据库类型" dbDriver="native" switchType="1" ><heartbeat>select user()</heartbeat><writeHost host="物理数据库ip" url="物理数据库ip:端口" user="操作物理数据库的mysql用户" password="密码"></writeHost></dataHost>
</mycat:schema>
2.2. 实际配置
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<!--
name:逻辑库名
checkSQLschema=false:查询sql中是否包含数据库名,忽略校验
sqlMaxLimit:定义了默认返回前端的数据的行数
-->
<schema name="imooc_db" checkSQLschema="false" sqlMaxLimit="100">
<!--
name:逻辑表名
primaryKey:逻辑主键和后端真是表中的主键保持一致
dataNode:引用的数据节点
-->
<table name="order_master" primaryKey="order_id" dataNode="ordb" />
</schema><!--数据节点
name:数据节点名称
dataHost:引用下面配置的dataHost
database:后端mysql的物理数据库名称 -->
<dataNode name="ordb" dataHost="mysql92102" database="order_db" /><!--mycat连接后端mysql的数据节点的连接信息
name:数据主机名 (自定义数据库+节点简写)
maxCon=1000:后端mysql允许mycat连接的最大连接数1000
maxCon=10:后端mysql允许mycat连接的最小连接数10
balance=3:在从机中负载均衡分发请求
writeType=0:写的类型,后端多个writeType怎样处理读请求
dbType:数据库类型
dbDriver:原生mysql驱动
switchType=1:交换类型,如果不是主从复制,此参数无意义-->
<dataHost name="mysql92102" maxCon="1000" minCon="10" balance="3" writeType="0" dbType="mysql" dbDriver="native" switchType="1"><!--心跳检测--> <heartbeat>select user()</heartbeat><!--写主机user:操作读写的mycat用户password:操作读写的mycat用户密码--> <writeHost host="192.168.92.102" url="192.168.92.102:3306" user="im_mycat" password="123456"></writeHost></dataHost>
</mycat:schema>
2.3. dataHost标签
dataHost标签:主要是配置mycat访问后端mysql数据节点的连接信息,咱们是一主三从,下面是配置3个从节点的连接信息。
<dataHost name="mysql92103" maxCon="1000" minCon="10" balance="3"writeType="0" dbType="mysql" dbDriver="native" switchType="1"><heartbeat>select user()</heartbeat><writeHost host="192.168.92.103" url="192.168.92.103:3306" user="im_mycat" password="123456"></writeHost></dataHost><dataHost name="mysql92104" maxCon="1000" minCon="10" balance="3"writeType="0" dbType="mysql" dbDriver="native" switchType="1"><heartbeat>select user()</heartbeat><writeHost host="192.168.92.104" url="192.168.92.104:3306" user="im_mycat" password="123456"></writeHost></dataHost>
2.4. dataNode标签
dataNode依赖于 dataHost标签标签,dataNode有几个是后端有几个数据库决定的。
<dataNode name="ordb" dataHost="mysql92102" database="order_db" /><dataNode name="prodb" dataHost="mysql92103" database="product_db" /><dataNode name="custdb" dataHost="mysql92104" database="customer_db" />
2.5. schema标签
关键配置:逻辑库和逻辑表
<schema name="imooc_db" checkSQLschema="false" sqlMaxLimit="100"><table name="order_master" primaryKey="order_id" dataNode="ordb" /><table name="order_detail" primaryKey="order_detail_id" dataNode="ordb" /><table name="order_cart" primaryKey="cart_id" dataNode="ordb" /><table name="order_customer_addr" primaryKey="customer_addr_id" dataNode="ordb" /><table name="region_info" primaryKey="region_id" dataNode="ordb" /><table name="shipping_info" primaryKey="ship_id" dataNode="ordb" /><table name="warehouse_info" primaryKey="w_id" dataNode="ordb" /><table name="warehouse_proudct" primaryKey="wp_id" dataNode="ordb" /><table name="product_brand_info" primaryKey="brand_id" dataNode="prodb" /><table name="product_category" primaryKey="category_id" dataNode="prodb" /><table name="product_comment" primaryKey="comment_id" dataNode="prodb" /><table name="product_info" primaryKey="product_id" dataNode="prodb" /><table name="product_supplier_info" primaryKey="supplier_id" dataNode="prodb" /><table name="product_pic_info" primaryKey="product_pic_id" dataNode="prodb" /><table name="customer_balance_log" primaryKey="balance_id" dataNode="custdb" /><table name="customer_inf" primaryKey="customer_inf_id" dataNode="custdb" /><table name="customer_level_inf" primaryKey="customer_level" dataNode="custdb" />
</schema>
三、server.xml文件配置
配置系统参数
应用程序通过mycat连接数据库的账号
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/"><system><property name="serverPort">8066</property><property name="managerPort">9066</property><property name="nonePasswordLogin">0</property><property name="bindIp">0.0.0.0</property><property name="frontWriteQueueSize">2048</property><property name="txIsolation">2</property><property name="processors">8</property><property name="idleTimeout">1800000</property><property name="sqlExecuteTimeout">300</property><property name="useSqlStat">0</property><property name="useGlobleTableCheck">0</property><property name="sequnceHandlerType">2</property><property name="defaultMaxLimit">100</property><property name="maxPacketSize">104857600</property></system><user defaultAccount="true" name="app_imooc"><property name="usingDecrypt">1</property><property name="password">bDbWr7bVMgszTe82oMo8NaUsmFFdPCNl/lYXzOYoG8anTpQLvdx5e+LYJEmT0IAeSVp1loyxSZPyv1GoHbHFHg==</property><property name="schemas">order_db,product_db,customer_db</property></user>
</mycat:server>
四、启动mycat调试
4.1. 启动mycat
# 监控MyCat运行状态
ps -ef |grep mycat# 重新启动mycat
mycat stop#启动MyCat
mycat start# 查看mycat启动日志
tail -f /app/mycat/logs/wrapper.log
4.2. 启动mycat异常
#异常信息如下
Caused by: io.mycat.config.util.ConfigException: SelfCheck### schema product_db refered by user app_imooc is not exist!
# 日志的异常信息收的就是app_imooc用不存在
4.2. 解决方案
此处应该配置逻辑数据库而不是物理数据库
# 编辑server.xml
将<property name="schemas">order_db,product_db,customer_db</property>
替换为
<property name="schemas">imooc_db</property>
4.3. 重新启动mycat
# 重新启动mycat
mycat stop#启动MyCat
mycat start# 查看mycat启动日志
tail -f /app/mycat/logs/wrapper.log
4.4. Mycat验证配置
# 在mysql的任意节点,使用mysql客户端连接mycat
# 怎样证明这是通过登录的mysql数据库还是登录的mycat服务端呢?在操作日志中有答案mysql -uapp_imooc -p123456 -h192.168.92.101 -P8066 # 查看当前用户可以查看的逻辑库有哪些show databases;# 使用imooc_db数据库
use imooc_db;# 查看在这个逻辑库下面的逻辑表包含有哪些
show tabels;# 查询逻辑表,验证是否可以真实访问后端屋里库中的物理表的数据
select * from product_info limit 10;如果返回数据正常,说明mycat配置是没有问题了。
从上面可以看出逻辑表和我们真实的屋里库中的标的数量一致,现在通过逻辑库访问后端3个物理数据库中的物理表。
4.5. 操作日志
[root@node1 ~]# mysql -uapp_imooc -p123456 -h192.168.92.101 -P8066
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.29-mycat-1.6.5-release-20180122220033 MyCat Server (OpenCloundDB)Copyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;
+----------+
| DATABASE |
+----------+
| imooc_db |
+----------+
1 row in set (0.01 sec)mysql> use imooc_db;
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> show tables;
+-----------------------+
| Tables in imooc_db |
+-----------------------+
| customer_balance_log |
| customer_inf |
| customer_level_inf |
| customer_login |
| customer_login_log |
| customer_point_log |
| order_cart |
| order_customer_addr |
| order_detail |
| order_master |
| product_brand_info |
| product_category |
| product_comment |
| product_info |
| product_pic_info |
| product_supplier_info |
| region_info |
| shipping_info |
| warehouse_info |
| warehouse_proudct |
+-----------------------+
20 rows in set (0.00 sec)mysql> select * from product_info limit 10;
+------------+------------------+---------------------------------------+------------+----------+-----------------+-----------------+-------------------+-------------+--------+--------------+----------------+--------------+--------+--------+-------+-------+------------+---------------------+------------+----------+---------------------+---------------------+
| product_id | product_code | product_name | bar_code | brand_id | one_category_id | two_category_id | three_category_id | supplier_id | price | average_cost | publish_status | audit_status | weight | length | heigh | width | color_type | production_date | shelf_life | descript | indate | modified_time |
+------------+------------------+---------------------------------------+------------+----------+-----------------+-----------------+-------------------+-------------+--------+--------------+----------------+--------------+--------+--------+-------+-------+------------+---------------------+------------+----------+---------------------+---------------------+
| 1 | 1700000000000001 | [凯乐石]连衣裙示例商品-1 | 771998885 | 4 | 1 | 9 | 17 | 2 | 269.79 | 269.79 | 0 | 0 | NULL | NULL | NULL | NULL | NULL | 2021-07-27 09:55:18 | 180 | | 2021-07-10 19:38:55 | 2021-07-10 19:38:56 |
| 2 | 1800000000000001 | [金狐狸]蕾丝裙示例商品-1 | 9075354188 | 12 | 1 | 9 | 18 | 2 | 252.03 | 252.03 | 0 | 0 | NULL | NULL | NULL | NULL | NULL | 2021-10-01 07:08:03 | 180 | | 2021-07-10 19:38:55 | 2021-07-10 19:38:56 |
| 3 | 1900000000000001 | [Columbia]套装裙示例商品-1 | 8069751364 | 2 | 1 | 9 | 19 | 17 | 350.30 | 350.30 | 0 | 0 | NULL | NULL | NULL | NULL | NULL | 2021-09-23 05:21:25 | 180 | | 2021-07-10 19:38:55 | 2021-07-10 19:38:56 |
| 4 | 2000000000000001 | [李宁]棉麻连衣裙示例商品-1 | 1633719161 | 16 | 1 | 9 | 20 | 4 | 326.11 | 326.11 | 0 | 0 | NULL | NULL | NULL | NULL | NULL | 2021-09-26 19:34:04 | 180 | | 2021-07-10 19:38:55 | 2021-07-10 19:38:56 |
| 5 | 2100000000000001 | [LOWA]针织裙示例商品-1 | 3911261209 | 8 | 1 | 9 | 21 | 2 | 189.07 | 189.07 | 0 | 0 | NULL | NULL | NULL | NULL | NULL | 2021-10-18 02:33:42 | 180 | | 2021-07-10 19:38:55 | 2021-07-10 19:38:56 |
| 6 | 2200000000000001 | [Columbia]a字裙示例商品-1 | 7220463071 | 2 | 1 | 9 | 22 | 10 | 875.60 | 875.60 | 0 | 0 | NULL | NULL | NULL | NULL | NULL | 2021-08-18 14:58:48 | 180 | | 2021-07-10 19:38:55 | 2021-07-10 19:38:56 |
| 7 | 2300000000000001 | [Columbia]长裙示例商品-1 | 496033971 | 2 | 1 | 9 | 23 | 11 | 541.57 | 541.57 | 0 | 0 | NULL | NULL | NULL | NULL | NULL | 2021-09-13 00:45:24 | 180 | | 2021-07-10 19:38:55 | 2021-07-10 19:38:56 |
| 8 | 2400000000000001 | [北极狐]针织衫示例商品-1 | 1492949456 | 5 | 1 | 10 | 24 | 12 | 303.93 | 303.93 | 0 | 0 | NULL | NULL | NULL | NULL | NULL | 2021-08-10 03:30:02 | 180 | | 2021-07-10 19:38:55 | 2021-07-10 19:38:56 |
| 9 | 2500000000000001 | [NB]衬衫示例商品-1 | 3983564931 | 17 | 1 | 10 | 25 | 16 | 18.28 | 18.28 | 0 | 0 | NULL | NULL | NULL | NULL | NULL | 2021-07-12 13:30:45 | 180 | | 2021-07-10 19:38:55 | 2021-07-10 19:38:56 |
| 10 | 2600000000000001 | [LOWA]T恤示例商品-1 | 205288381 | 8 | 1 | 10 | 26 | 12 | 607.17 | 607.17 | 0 | 0 | NULL | NULL | NULL | NULL | NULL | 2021-08-25 09:14:44 | 180 | | 2021-07-10 19:38:55 | 2021-07-10 19:38:56 |
+------------+------------------+---------------------------------------+------------+----------+-----------------+-----------------+-------------------+-------------+--------+--------------+----------------+--------------+--------+--------+-------+-------+------------+---------------------+------------+----------+---------------------+---------------------+
10 rows in set (0.10 sec)mysql>exit
ERROR 2003 (HY000): Can’t connect to MySQL server on ‘192.168.92.101’ (113)
下一篇:企业实战_12_MyCat水平扩展_分库分表
https://gblfy.blog.csdn.net/article/details/100059793