mycat垂直分库

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 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数据库方式一模一样,应用程序不需要做任何更改。只需要改变数据库链接地址,及用户口令即可。
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
4月前
|
存储 大数据 数据库
分库分表知识总结(三)之水平分表
分库分表知识总结(三)之水平分表
46 0
|
4月前
|
存储 监控 数据库
分库分表知识总结(二)之垂直分表
分库分表知识总结(二)之垂直分表
50 1
|
7月前
|
存储 关系型数据库 MySQL
Mysql分表分库背景知识(2)
Mysql分表分库背景知识(2)
56 0
|
7月前
|
存储 架构师 关系型数据库
Mysql分表分库背景知识(1)
Mysql分表分库背景知识(1)
26 0
|
9月前
|
存储 缓存 数据库
什么是垂直分表、垂直分库、水平分表、水平分库?
什么是垂直分表、垂直分库、水平分表、水平分库?
95 0
|
9月前
|
算法 Java 数据库连接
垂直分表和垂直分库
垂直分表和垂直分库
|
9月前
|
SQL 算法 Java
水平分库和水平分表
水平分库和水平分表
|
存储 大数据 数据库
水平分库
水平分库
88 0
水平分库
|
监控 数据库
垂直分库
垂直分库
64 0
垂直分库
|
SQL 存储 关系型数据库
MyCat - 分片 - 垂直拆分 - 测试 | 学习笔记
快速学习 MyCat - 分片 - 垂直拆分 - 测试
77 0
MyCat - 分片 - 垂直拆分 - 测试 | 学习笔记