MyCAT实战环节
MyCAT实战环节
前言
我们前面说了MyCAT的基本安装 和配置文件的作用 我相信各位同学有了一个大概的概念 所有我想 这篇文章我就不写那么细了 接下来 我们去进入到实战环节
环境搭建
server_1(Centos7):
(MyCAT)192.168.3.167
sever_2(Centos7):
(MySQL)192.168.3.163
server_3(Ubuntu):
(MySQL)192.168.3.162/24
主从复制部署:
因为MyCAT只能路由、分发,不能把多个数据库里面的数据进行同步,所以要数据同步必须还要使用MySQL的读写分离,主从复制。
server_2(主库):
安装Mysql
//前面的文章有关于MySQL的安装
修改配置文件
vim /etc/my.conf//基本配置
[mysqld]
port = 3306
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
log-error = /usr/local/mysql/data/error.log
pid-file = /usr/local/mysql/data/mysql.pid
socket = /tmp/mysql.sock
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
default-storage-engine=InnoDB
default-authentication-plugin=mysql_native_password
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES//二级制日志
server-id = 1 //它是主库
log_bin = /usr/local/mysql/data/mysql-bin
binlog_format = row //row模式 从库要和主库格式一样
expire_logs_days = 7
max_binlog_size = 100M
创建远程登录的用户
mysql> create user 'aa'@'%' identified by '123';
Query OK, 0 rows affected (0.29 sec)mysql> grant all on *.* to 'aa'@'%';
Query OK, 0 rows affected (0.10 sec)//PS:我们是测试环境 所有设置的是 "aa" 密码的 "123" 成产环境中要不同的库要设置特殊权限的用户
重启MySQL服务
systemctl restart mysql
查看主库的状态
SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 446 | | | |
+------------------+----------+--------------+------------------+-------------------+//PS:File和Positiion值记住
sever_3(从库):
安装MySQL
//前面的文章有关于MySQL的安装
修改配置文件
//基本配置
[mysqld]
port = 3306
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
log-error = /usr/local/mysql/data/error.log
pid-file = /usr/local/mysql/data/mysql.pid
socket = /tmp/mysql.sock
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
default-storage-engine=InnoDB
default-authentication-plugin=mysql_native_password
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES//二级制日志
server-id = 2 //它是从库
log_bin = /usr/local/mysql/data/mysql-bin
binlog_format = row //row模式 从库要和主库格式一样
expire_logs_days = 7
max_binlog_size = 100M//PS:对于ubuntu来说 MySQL配置文件安装的时候尽量默认 因为它的文件下面有关于二级制的部分 直接修改就好了
测试远程链接
mysql -u aa -p -h 192.168.3.163 -P 3306
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.20 MySQL Community Server - GPLCopyright (c) 2000, 2025, 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>
//成功
配置从配置
mysql -u root -p //登录
CHANGE MASTER TO-> MASTER_HOST = '192.168.3.163',-> MASTER_USER = 'aa',-> MASTER_PASSWORD = '123',-> MASTER_LOG_FILE = 'mysql-bin.000002', //File-> MASTER_LOG_POS = 156; //Positiion
Query OK, 0 rows affected, 8 warnings (0.02 sec)START SLAVE; //启动
SHOW SLAVE STATUS\G //查看
SHOW SLAVE STATUS\G
*************************** 1. row ***************************Slave_IO_State: Waiting for source to send eventMaster_Host: 192.168.3.163Master_User: aaMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000002Read_Master_Log_Pos: 156Relay_Log_File: ws-virtual-machine-relay-bin.000003Relay_Log_Pos: 325Relay_Master_Log_File: mysql-bin.000002Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 156Relay_Log_Space: 728Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1Master_UUID: 45dcbb89-7331-11f0-8df5-000c29573c74Master_Info_File: mysql.slave_master_infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Replica has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0Network_Namespace:
1 row in set, 1 warning (0.00 sec)
-
检查以下两个参数是否为 Yes,如果是则表示配置成功:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes -
PS:
- 配置必须重启生效:修改
my.cnf
后,务必重启 MySQL 服务,否则binlog_format
不会更新。 - 主从格式严格一致:
ROW
格式是 MySQL 8.0 主从复制的最佳实践,避免混合使用不同格式。 - 避免从库写数据:确保从库
read_only = 1
(只读模式),防止手动写入导致的数据冲突。 -
RESET SLAVE ALL;
清除 - PS:如果错误 先停止 START SLAVE; 再清除 再重新写入 再启动
- 配置必须重启生效:修改
主从完整测试
-
在主库创建一个测试数据库和表:
CREATE DATABASE test_repl; USE test_repl; CREATE TABLE test_table (id INT, name VARCHAR(20)); INSERT INTO test_table VALUES (1, 'test');
-
在从库查看是否同步成功:
SHOW DATABASES; USE test_repl; SELECT * FROM test_table;
- 如果从库中能看到主库创建的数据,则说明主从复制配置成功。
配置MyCAT
server_1
安装MyCAT
//由于时间关系 不去讲述怎么安装和配置文件 大家可以看前面的文章
配置prototypeDs.datasource.json
vim 。。/mycat/conf/datasources/prototypeDs.datasource.json{"dbType":"mysql","idleTimeout":60000,"initSqls":[],"initSqlsGetConnection":true,"instanceType":"READ_WRITE","maxCon":1000,"maxConnectTimeout":3000,"maxRetryCount":5,"minCon":1,"name":"prototypeDs","password":"123", //远程登录用户的密码"type":"JDBC","url":"jdbc:mysql://192.168.3.163:3306/mysql?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8", //远程MySQL的IP"user":"aa", //远程登录用户"weight":0
}//如果有多台MySQL主机可选
{"dbType":"mysql","idleTimeout":60000,"initSqls":[],"initSqlsGetConnection":true,"instanceType":"READ_WRITE","maxCon":1000,"maxConnectTimeout":3000,"maxRetryCount":5,"minCon":1,"name":"prototypeDs","password":"321","type":"JDBC","url":"jdbc:mysql://192.168.3.164:3306/mysql?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8","user":"aa1","weight":0
}
(可选)修改MyCAT的登录用户
vim /usr/local/mycat/conf/users/root.user.json
{"dialect":"mysql","ip":null,"password":"123.com","transactionType":"proxy","username":"root"
}
补充
在较新的 MyCat 版本(如 MyCat 2.x)中,配置文件结构发生了变化,schema.xml
可能已被拆分或替换,不再以单一文件形式存在,而是通过 conf
目录下的子目录(如 schemas
、datasources
等)中的文件来管理配置。
[root@localhost schemas]# ls
information_schema.schema.json mysql.schema.json
information_schema.schema.json
和 mysql.schema.json
,是 MyCat 2.x 版本中对逻辑库和表的配置文件,它们替代了旧版本 schema.xml
中关于逻辑库与物理表映射的功能。
这些文件的作用:
-
mysql.schema.json
:通常对应 MySQL 系统库mysql
的逻辑映射配置,定义了该逻辑库下包含哪些表,以及这些表如何映射到底层物理数据库。 -
information_schema.schema.json
:对应 MySQL 系统库information_schema
的逻辑配置,用于 MyCat 兼容 MySQL 的元数据查询(如show tables
、show databases
等)。
PS:但我们实际情况可以走默认 不用去管
配置示例(参考):
若要创建一个名为 testdb
的逻辑库,可在 schemas
目录下新建 testdb.schema.json
,内容大致如下(根据实际需求调整):
json
{"schemaName": "testdb","tables": [{"tableName": "user","dataNode": "dn1", // 关联到数据源配置中的数据节点"primaryKey": "id"}]
}
启动MyCAT
start mycat
完整测试
--server_1 创建库和表格
mysql -u root -h 127.0.0.1 -p123.com -P8066 //登录Mycatmysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.91 sec)
o database selectedmysql> create database lzjy;
Query OK, 0 rows affected (1.70 sec)mysql> use lzjy;
Database changed
mysql> create table t201(id int,name char(10));
Query OK, 0 rows affected (0.42 sec)
--server_2 在主库上查看
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lzjy |
| mycat |
| mysql |
| performance_schema |
| sys |
| test_repl |
+--------------------+
7 rows in set (0.13 sec)mysql> use lzjy;
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_lzjy |
+----------------+
| t201 |
+----------------+
1 row in set (0.00 sec)
--server_3 从库上查看
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lzjy |
| mycat |
| mysql |
| performance_schema |
| sys |
| test_repl |
+--------------------+
7 rows in set (0.13 sec)mysql> use lzjy;
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_lzjy |
+----------------+
| t201 |
+----------------+
1 row in set (0.00 sec
总结
好了 以上就是这篇文章所有的内容 其实上面写的不是那么完全 因为时间关系 我没有去深入了解他的配置文件和其他作用 如果大家有好的想法 可以在评论区发出来 互相学习
OK 今天是8.7 但说真的 今天有点倒霉 那又怎么样呢 按照自己的节奏慢慢来就好了 希望大家开开心心~