mycat2读写分离配置

一. 准备工作    
    已经安装好的mycat,以及做好主从的mysql,mysql中有db2这个库,且已经存在表和数据.
    mycat可以根据mycat2快速安装进行安装.    


二. 直接通过命令进行配置. 
连接mycat端口8066 账号root 密码123456

mysql -uroot -p123456 -P8066 -h192.168.28.130


#创建db2逻辑库
create database db2;

查看创建的schema的配置

customTables放置的是mycat默认的表的配置
globalTables放置的是全局表的配置
shardingTables放置分片表的配置
normalTables放置普通表的配置

#linux下查看

[root@localhost mycat]# cat conf/schemas/db2.schema.json
{
    "customTables":{},
    "globalTables":{},
    "normalTables":{},
    "schemaName":"db2",
    "shardingTables":{},
    "views":{}
}

修改schema的配置

#指定db2逻辑库默认的targetName, mycat会自动加载db2下已经有的物理表或者视图作为单表.

/*+ mycat:createSchema{
  "customTables":{},
  "globalTables":{},
  "normalTables":{},
  "schemaName":"db2",
  "shardingTables":{},
  "targetName":"prototype"
} */;

查看修改之后的schema的配置,已经自动加载单表boradtable了在normalTables中了

[root@localhost mycat]# cat conf/schemas/db2.schema.json
{
    "customTables":{},
    "globalTables":{},
    "normalTables":{
        "broadtable":{
            "createTableSQL":"CREATE TABLE db2.broadtable (\n\t`id` bigint(20) NOT NULL AUTO_INCREMENT,\n\t`user_id` varchar(100) DEFAULT NULL,\n\t`traveldate` date DEFAULT NULL,\n\t`fee` decimal(10, 0) DEFAULT NULL,\n\t`days` int(11) DEFAULT NULL,\n\t`blob` longblob,\n\tPRIMARY KEY (`id`),\n\tKEY `id` (`id`)\n) ENGINE = InnoDB AUTO_INCREMENT = 1129569 CHARSET = utf8",
            "locality":{
                "schemaName":"db2",
                "tableName":"broadtable",
                "targetName":"prototype"
            }
        }
    },
    "schemaName":"db2",
    "shardingTables":{},
    "targetName":"prototype",
    "views":{}
}

此時的架構圖

mycat2读写分离配置_第1张图片
进行查询

mysql> select * from db2.broadtable limit 10;
+----+---------+------------+------+------+------------+
| id | user_id | traveldate | fee  | days | blob       |
+----+---------+------------+------+------+------------+
|  1 | 2       | 2021-08-09 |    2 | NULL | NULL       |
|  2 | 414457  | 2021-08-04 |    2 |    2 | NULL       |
|  3 | 9242657 | 2021-08-04 |    2 |    2 | NULL       |
|  4 | 609056  | 2021-08-04 |    2 |    2 | NULL       |
|  5 | 5145822 | 2021-08-04 |    2 |    2 | NULL       |
|  6 | 1032249 | 2021-08-04 |    2 |    2 | NULL       |
|  7 | 1797571 | 2021-08-04 |    2 |    2 | NULL       |
|  8 | 2337723 | 2021-08-04 |    2 |    2 | NULL       |
|  9 | 5774224 | 2021-08-04 |    2 |    2 | NULL       |
| 10 | 3463940 | 2021-08-04 |    2 |    2 | NULL       |
+----+---------+------------+------+------+------------+
10 rows in set (0.01 sec)

注解添加数据源

/*+ mycat:createDataSource{
"name":"dr0",
"url":"jdbc:mysql://10.0.9.32:3306/?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true",
"user":"root",
"password":""
} */;

查询配置数据源结果

/*+ mycat:showDataSources{} */;

更新集群信息,添加dr0从节点.

/*! mycat:createCluster{"name":"prototype","masters":["prototypeDs"],"replicas":["dr0"]} */;


/*+ mycat:showClusters{} */;

此時架構圖:

mycat2读写分离配置_第2张图片
配置完成.

三. 进行数据库表结构的新增,修改,查询,插入.

新建表

CREATE TABLE db2.`travelrecord` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `user_id` varchar(100) DEFAULT NULL,
  `traveldate` date DEFAULT NULL,
  `fee` decimal(10,0) DEFAULT NULL,
  `days` int DEFAULT NULL,
  `blob` longblob,
  PRIMARY KEY (`id`),
  KEY `id` (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

修改表

mysql> ALTER TABLE db2.`travelrecord`
    -> ADD COLUMN `col`  varchar(255) NULL AFTER `blob`;
Query OK, 0 rows affected (0.52 sec)

插入数据

mysql> insert into db2.travelrecord(user_id)values ("1");
Query OK, 1 row affected (0.03 sec)

查询

mysql> select * from db2.travelrecord limit 10;
+----+---------+------------+------+------+------------+------+
| id | user_id | traveldate | fee  | days | blob       | col  |
+----+---------+------------+------+------+------------+------+
|  1 | 1       | NULL       | NULL | NULL | NULL       | NULL |
+----+---------+------------+------+------+------------+------+
1 row in set (0.01 sec)


 

你可能感兴趣的:(mycat)