mycat2读写分离配置
Posted w329636271
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mycat2读写分离配置相关的知识,希望对你有一定的参考价值。
一. 准备工作
已经安装好的mycat,以及做好主从的mysql,mysql中有db2这个库,且已经存在表和数据.
mycat可以根据mycat2一分钟入门启动进行安装.
二. 直接通过命令进行配置.
连接mycat8066
mysql -uroot -p123456 -P8066 -h192.168.28.130
#创建db2逻辑库
create database db2;
#linux下查看
[root@localhost mycat]# cat conf/schemas/db2.schema.json
{
"customTables":{},
"globalTables":{},
"normalTables":{},
"schemaName":"db2",
"shardingTables":{},
"views":{}
}
#指定nacos逻辑库默认的targetName, mycat会自动加载db2下已经有的物理表或者视图作为单表.
/*+ mycat:createSchema{
"customTables":{},
"globalTables":{},
"normalTables":{},
"schemaName":"db2",
"shardingTables":{},
"targetName":"prototype"
} */;
[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":{}
}
此時的架構圖
进行查询
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":"JyUtivYS1bYIjaAk"
} */;
查询配置数据源结果
/*+ mycat:showDataSources{} */;
更新集群信息,添加dr0从节点.
/*! mycat:createCluster{"name":"prototype","masters":["prototypeDs"],"replicas":["dr0"]} */;
/*+ mycat:showClusters{} */;
此時架構圖:
配置完成.
三. 进行数据库表结构的新增,修改,查询,插入.
新建表
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)
以上是关于mycat2读写分离配置的主要内容,如果未能解决你的问题,请参考以下文章