mycat垂直分库

本文涉及的产品
RDS AI 助手,专业版
RDS MySQL DuckDB 分析主实例,基础系列 4核8GB
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
简介: mycat垂直分库数据库的主从复制MySQL备份源数据库并记录事务点# master导出单个库的备份SET GLOBAL log_timestamps = SYSTEM;(/usr/local/mysql/bin/mysqldump -uroot -p123456 -hlocalhost -P3306 --socket=/usr/local/mysql/mysql.

mycat垂直分库

数据库的主从复制

  1. MySQL备份源数据库并记录事务点
# master导出单个库的备份
SET GLOBAL log_timestamps = SYSTEM;(
/usr/local/mysql/bin/mysqldump -uroot -p123456 -hlocalhost -P3306 --socket=/usr/local/mysql/mysql.sock --single-transaction --master-data=2  imooc_db --triggers --routines --events > /tmp/all.sql
  1. 源数据库中建立从库(复制用户)
grant replication slave on *.* to 'repl'@'%' identified by '123456';
  1. 在新的数据库实例上恢复数据库,并修改数据库名称。
mysql -uroot -p123456 -e"create database order_db";
mysql -uroot -p123456 order_db < /tmp/all.sql
mysql -uroot -p123456 -e"create database product_db";
mysql -uroot -p123456 product_db < /tmp/all.sql
mysql -uroot -p123456 -e"create database customer_db";
mysql -uroot -p123456 customer_db < /tmp/all.sql
######################################
ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty
Query OK, 0 rows affected (0.01 sec)

root@localhost 23:55:  [(none)]> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000001 |      154 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

root@localhost 23:55:  [(none)]> show master status;
+---------------+----------+--------------+------------------+-------------------------------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                         |
+---------------+----------+--------------+------------------+-------------------------------------------+
| binlog.000001 |      154 |              |                  | ad083f85-9264-11e8-9e91-005056a3fe0a:1-85 |
+---------------+----------+--------------+------------------+-------------------------------------------+
1 row in set (0.00 sec)
  1. 在新实例数据库上配置复制链路
\help change master to
CHANGE MASTER TO
  MASTER_HOST='172.16.10.141',
  MASTER_USER='repl',
  MASTER_PASSWORD='123456',
  MASTER_PORT=3306,
  master_auto_position=1;

\help change replication filter
CHANGE REPLICATION FILTER
  REPLICATE_REWRITE_DB = ((imooc_db, order_db));
CHANGE REPLICATION FILTER
  REPLICATE_REWRITE_DB = ((imooc_db, product_db));
CHANGE REPLICATION FILTER
  REPLICATE_REWRITE_DB = ((imooc_db, customer_db));
start slave;
# 主库插入数据验证
root@localhost 00:12:  [imooc_db]> insert into region_info(parent_id,region_name,region_level) values(1,'guangdong',2);
# 从库查看数据
root@localhost 00:13:  [order_db]> select * from region_info;
+-----------+-----------+-------------+--------------+
| region_id | parent_id | region_name | region_level |
+-----------+-----------+-------------+--------------+
|         1 |         1 | guangdong   |            2 |
+-----------+-----------+-------------+--------------+
1 row in set (0.00 sec)
# 其他2个也这么同步

配置mycat垂直分库

  1. 配置schema.xml,需要的权限
grant select,insert,update,delete on *.* to 'bm_mycat'@'%' identified by '123456';
  1. 配置schema.xml,数据库分库实现
<schema name="imooc_db" checkSQLschema="false" sqlMaxLimit="100">
        <table name="company" primaryKey="ID" dataNode="dn3,dn2,dn1" rule="mod-long"/>
        <table name="order_cart" primaryKey="cart_id" dataNode="dn_orderdb" />
        <table name="order_customer_addr" primaryKey="customer_addr_id" dataNode="dn_orderdb" />
        <table name="order_detail" primaryKey="order_detail_id" dataNode="dn_orderdb" />
        <table name="order_master" primaryKey="order_id" dataNode="dn_orderdb" />
        
        <table name="region_info" primaryKey="region_id" dataNode="dn_orderdb" />        
        <table name="serial" primaryKey="id" dataNode="dn_orderdb" />          
        <table name="shipping_info" primaryKey="ship_id" dataNode="dn_orderdb" />     
        <table name="warehouse_info" primaryKey="w_id" dataNode="dn_orderdb" />    
        <table name="warehouse_proudct" primaryKey="wp_id" dataNode="dn_orderdb" />
        
        <table name="product_brand_info" primaryKey="brand_id" dataNode="dn_productdb" />
        <table name="product_category" primaryKey="category_id" dataNode="dn_productdb" />
        <table name="product_comment" primaryKey="comment_id" dataNode="dn_productdb" />
        <table name="product_info" primaryKey="product_id" dataNode="dn_productdb" />
        <table name="product_pic_info" primaryKey="product_pic_id" dataNode="dn_productdb" />
        <table name="product_supplier_info" primaryKey="supplier_id" dataNode="dn_productdb" />
        
        <table name="customer_balance_log" primaryKey="balance_id" dataNode="dn_customerdb" />
        <table name="customer_inf" primaryKey="customer_inf_id" dataNode="dn_customerdb" />
        <table name="customer_level_inf" primaryKey="customer_level" dataNode="dn_customerdb" />
        <table name="customer_login" primaryKey="customer_id" dataNode="dn_customerdb" />
        <table name="customer_login_log" primaryKey="login_id" dataNode="dn_customerdb" />
        <table name="customer_point_log" primaryKey="point_id" dataNode="dn_customerdb" />
        
</schema>

<dataNode name="dn_orderdb" dataHost="mysql10142" database="order_db" />
<dataNode name="dn_productdb" dataHost="mysql10143" database="product_db" />
<dataNode name="dn_customerdb" dataHost="mysql10144" database="customer_db" />

<dataHost name="mysql10142" maxCon="1000" minCon="10" balance="3"
                          writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
        <heartbeat>show slave status</heartbeat>
        <writeHost host="172.16.10.142" url="172.16.10.142:3306" user="bm_mycat" password="123456">
        </writeHost>
</dataHost>
<dataHost name="mysql10143" maxCon="1000" minCon="10" balance="3"
                          writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
        <heartbeat>show slave status</heartbeat>
        <writeHost host="172.16.10.143" url="172.16.10.143:3306" user="bm_mycat" password="123456">
        </writeHost>
</dataHost>
<dataHost name="mysql10144" maxCon="1000" minCon="10" balance="3"
                          writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
        <heartbeat>show slave status</heartbeat>
        <writeHost host="172.16.10.144" url="172.16.10.144:3306" user="bm_mycat" password="123456">
        </writeHost>
</dataHost>

验证通过mycat访问db

使用命令行访问方式
[root@mycat01 logs]# mysql -uapp_imooc -p123456 -P8066
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'app_imooc'@'localhost' (using password: YES)
[root@mycat01 logs]# mysql -uapp_imooc -p123456 -P8066 -h172.16.10.142
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 1
Server version: 5.5.8-mycat-1.5.1-RELEASE-20160525110043 MyCat Server (OpenCloundDB)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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.

app_imooc@172.16.10.142 01:59:  [(none)]> show databases;
+----------+
| DATABASE |
+----------+
| imooc_db |
+----------+
1 row in set (0.00 sec)

app_imooc@172.16.10.142 01:59:  [(none)]> use imooc_db;
Database changed
app_imooc@172.16.10.142 01:59:  [imooc_db]> 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           |
| serial                |
| shipping_info         |
| warehouse_info        |
| warehouse_proudct     |
+-----------------------+
21 rows in set (0.01 sec)

app_imooc@172.16.10.142 01:59:  [imooc_db]> 
使用程序访问方式:
与java访问MySQL数据库方式一模一样,应用程序不需要做任何更改。只需要改变数据库链接地址,及用户口令即可。
相关实践学习
自建数据库迁移到云数据库
本场景将引导您将网站的自建数据库平滑迁移至云数据库RDS。通过使用RDS,您可以获得稳定、可靠和安全的企业级数据库服务,可以更加专注于发展核心业务,无需过多担心数据库的管理和维护。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
目录
相关文章
|
存储 编解码 监控
视频基础知识 3
视频基础知识
979 0
|
1月前
|
人工智能 自然语言处理 监控
企业级Agent解决方案:以AgentOne为核心的数智化实践
2026年,阿里云瓴羊推出企业级全域协同Agent方案:以AgentOne为智能中枢,融合Dataphin(数据治理)、Quick Audience(智能营销)、Quick Service(智能服务)与Quick BI“智能小Q”(敏捷分析),打通“感知—决策—执行—反馈”全链路,破解数据孤岛、工具断裂、闭环缺失难题,驱动企业从数字化迈向数智化跃迁。(239字)
|
9月前
|
人工智能 数据可视化 前端开发
前后端联调安排工具全景解析:让接口联调有序推进,项目节奏不再脱节
在开发节奏加快的今天,联调失控常导致项目延期。前后端联调安排工具通过接口管理、进度同步、角色权限配置等功能,提升协作效率,保障项目按时交付。
ly~
|
开发框架 小程序 前端开发
抖音小程序的开发难度大吗?
抖音小程序的开发难度因人而异,主要取决于开发者经验、技能及功能需求。技术上需掌握前端技术及抖音开发框架,了解平台生态与规则;设计上需符合用户审美和习惯,具备创新性和实用性。此外,严格的审核标准和激烈的市场竞争增加了开发难度,开发者需制定有效推广策略并持续优化小程序以保持竞争力。
ly~
606 4
|
前端开发 数据管理 数据处理
Django的MTV
【8月更文挑战第13天】
403 4
|
Java
Java中执行命令并使用指定配置文件的最佳实践
通过本文的介绍,您可以了解如何在Java中使用 `ProcessBuilder`执行系统命令,并通过指定配置文件、设置环境变量和重定向输入输出流来控制命令的行为。通过这些最佳实践,可以确保您的Java应用程序在执行系统命令时更加健壮和灵活。
454 7
|
机器学习/深度学习 运维 算法
大数据基础工程技术团队4篇论文入选ICLR,ICDE,WWW
大数据基础工程技术团队4篇论文入选ICLR,ICDE,WWW
212 0
|
人工智能 知识图谱
成熟的AI要学会自己搞研究!MIT推出科研特工
MIT推出科研特工SciAgents,结合生成式AI、本体表示和多代理建模,实现科学发现的自动化。通过大规模知识图谱和多代理系统,SciAgents能探索新领域、识别复杂模式,加速新材料发现,展现跨学科创新潜力。
298 12
|
安全 UED 开发者
鸿蒙开发:沉浸式效果实现
沉浸式效果实现后,一定要注意安全区域的内容避让,防止内容延伸后被导航条或者状态栏遮挡,具体是选择安全区域或者窗口管理方式,按照需求进行处理,如果仅仅是某个页面,直接安全区域即可。
478 0
鸿蒙开发:沉浸式效果实现
|
分布式计算 资源调度 Hadoop
Spark Standalone与YARN的区别?
本文详细解析了 Apache Spark 的两种常见部署模式:Standalone 和 YARN。Standalone 模式自带轻量级集群管理服务,适合小规模集群;YARN 模式与 Hadoop 生态系统集成,适合大规模生产环境。文章通过示例代码展示了如何在两种模式下运行 Spark 应用程序,并总结了两者的优缺点,帮助读者根据需求选择合适的部署模式。
714 3