安装JDK1.8

使用MyCAT 2要安装JDK,因为MyCAT 是基于JDK1.8开发的。

 # CentOS7
 yum -y install java-1.8.0-openjdk.x86_64
 ​
 # Ubuntu22.04
 apt install openjdk-8-jdk-headless
 ​
 # 测试安装,显示java版本则为安装成功
 java -version
 ​
 openjdk version "1.8.0_392"
 OpenJDK Runtime Environment Corretto-8.392.08.1 (build 1.8.0_392-b08)
 OpenJDK 64-Bit Server VM Corretto-8.392.08.1 (build 25.392-b08, mixed mode)

安装MyCAT2

 # 创建/usr/local/src
 mkdir -p  /usr/local/src
 ​
 # 进入/usr/local/src目录
 cd /usr/local/src
 ​
 # 第三方下载
 wget https://download.topunix.com/MySQL/Software-Cluster/Software-Mycat/Mycat2/mycat2-install-template-1.20.zip
 ​
 ## 忽略证书
 wget --no-check-certificate https://download.topunix.com/MySQL/Software-Cluster/Software-Mycat/Mycat2/mycat2-install-template-1.20.zip
 ​
 # 第三方下载
 wget https://download.topunix.com/MySQL/Software-Cluster/Software-Mycat/Mycat2/mycat2-1.21-release-jar-with-dependencies.jar
 ​
 ## 忽略证书
 wget --no-check-certificate https://download.topunix.com/MySQL/Software-Cluster/Software-Mycat/Mycat2/mycat2-1.21-release-jar-with-dependencies.jar
 ​
 # 如果没有wget安装方式
 # CentOS7安装wget
 yum -y install wget
 ​
 # Ubuntu22.04安装wget
 apt install wget
 ​
 # 解压并移动到/usr/local目录下
 ## 安装解压软件
 ## CentOS7
 yum -y install unzip
 ​
 # Ubuntu22.04
 apt install unzip
 ​
 # 解压Mycat2
 unzip mycat2-install-template-1.20.zip
 mv mycat ../
 ​
 # 修改权限
 cd /usr/local/mycat/
 chmod +x  bin/*
 ​
 # 把所需的jar复制到mycat/lib目录
 ## mycat2-1.21-release-jar-with-dependencies.jar是MyCAT2依赖文件,缺失会导致MyCAT启动失败
 cp /usr/local/src/mycat2-1.21-release-jar-with-dependencies.jar /usr/local/mycat/lib/
 ​
 # MyCAT的目录结构
 1)bin 执行命令的目录
 2)conf 配置文件
 3)lib 依赖包
 4)logs 日志包
 ​
 # 启动一个3306的MySQL
 systemctl start mysqld      #启动mysql服务
 mysql -uroot -p123456       # 能够连接而上mysql
 ​
 # 配置物理库地址
 ## 在启动之前我们要配置物理库的地址,要不然MyCAT启动就会报错。
 ## 配置文件位置: /usr/local/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": "root123",
     "type": "JDBC",
     "url": "jdbc:mysql://localhost:3306?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8",
     "user": "root",
     "weight": 0
 }
 ​
 # 启动MyCAT
 cd /usr/local/mycat/bin
 ./mycat start 启动
 ./mycat stop 停止
 ./mycat console 前台运行
 ./mycat restart 重启
 ./mycat pause 暂停
 ./mycat status 查看启动状态
 ​
 ## 设置开机自启创建 systemd 服务文件
 cat > /etc/systemd/system/mycat.service << 'EOF'
 [Unit]
 Description=Mycat2 Database Middleware
 After=network.target
 ​
 [Service]
 Type=forking
 ExecStart=/usr/local/mycat/bin/mycat start
 ExecStop=/usr/local/mycat/bin/mycat stop
 ExecRestart=/usr/local/mycat/bin/mycat restart
 User=root
 Group=root
 RemainAfterExit=yes
 Restart=on-failure
 RestartSec=10s
 LimitNOFILE=65535
 ​
 [Install]
 WantedBy=multi-user.target
 EOF
 ​
 ## 重载配置并启用开机自启
 systemctl daemon-reload      # 重新加载 systemd 配置,
 systemctl enable mycat       # 设置 mycat 服务开机自启(创建软链接)
 systemctl start mycat        # 立即启动 mycat 服务
 systemctl status mycat       # 查看 mycat 服务的当前运行状态和详细信息
 ​
 ## 验证是否成功
 # 查看是否已启用 输出 enabled 表示成功 ✅
 systemctl is-enabled mycat
 ​
 # 查看运行状态看到 active (running) 表示正在运行 ✅
 systemctl status mycat
 ​
 # 可以配置MyCAT和MySQL的环境变量,方便之后执行(在任何目录下都能直接执行)
 vim /etc/profile
 ​
 ## 在文件末行添加:
 export MYCAT_PATH=/usr/local/mycat
 export MYSQL_PATH=/usr/local/mysql
 export PATH="$MYSQL_PATH/bin:$MYCAT_PATH/bin:$PATH"
 ​
 ### ESC:wq退出文件:重新加载配置文件
 source  /etc/profile
 ​
 # 查看logs/wrapper.log文档看错
 cat /usr/local/mycat/logs/wrapper.log 如果出现下面的信息就说明启动成功了。
 ​
 [root@qilin-zhu-1 bin]# cat /usr/local/mycat/logs/wrapper.log
 STATUS | wrapper  | 2026/07/06 11:19:34 | --> Wrapper Started as Daemon
 STATUS | wrapper  | 2026/07/06 11:19:34 | Launching a JVM...
 INFO   | jvm 1    | 2026/07/06 11:19:36 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
 INFO   | jvm 1    | 2026/07/06 11:19:36 |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
 INFO   | jvm 1    | 2026/07/06 11:19:36 | 
 INFO   | jvm 1    | 2026/07/06 11:19:37 | path:/usr/local/mycat/./conf
 INFO   | jvm 1    | 2026/07/06 11:19:38 | 2026-07-06 11:19:38,668[INFO]io.mycat.MycatCore.newMycatServer:213start VertxMycatServer
 INFO   | jvm 1    | 2026/07/06 11:19:41 | 2026-07-06 11:19:41,257[INFO]com.alibaba.druid.pool.DruidDataSource.init:990{dataSource-1} inited
 INFO   | jvm 1    | 2026/07/06 11:19:43 | 2026-07-06 11:19:43,472[INFO]io.mycat.replica.heartbeat.HeartbeatFlow.sendDataSourceStatus:71prototypeDs heartStatus DatasourceStatus(status=OK_STATUS, isSlaveBehindMaster=false, dbSynStatus=DB_SYN_NORMAL, master=true)
 INFO   | jvm 1    | 2026/07/06 11:19:49 | 2026-07-06 11:19:49,700[INFO]io.mycat.config.MycatRouterConfigOps.recoveryXA:728readXARecoveryLog start
 INFO   | jvm 1    | 2026/07/06 11:19:50 | 2026-07-06 11:19:50,340[INFO]io.mycat.vertx.VertxMycatServer.lambda$start$1:120Mycat Vertx server 2e5d963d-6aa7-4ec0-a0cd-8236cc9d5a9c started up.
 INFO   | jvm 1    | 2026/07/06 11:19:50 | 2026-07-06 11:19:50,337[INFO]io.mycat.vertx.VertxMycatServer.lambda$start$1:120Mycat Vertx server f4b735aa-8142-4e6d-8417-789c0a634933 started up.
 INFO   | jvm 1    | 2026/07/06 11:19:50 | 2026-07-06 11:19:50,344[INFO]io.mycat.vertx.VertxMycatServer.lambda$start$1:120Mycat Vertx server bd6d8738-18cd-44c7-9b0e-fc9522ddc6db started up.
 INFO   | jvm 1    | 2026/07/06 11:19:50 | 2026-07-06 11:19:50,358[INFO]io.mycat.vertx.VertxMycatServer.lambda$start$1:120Mycat Vertx server 377fa3ca-bfa2-4ef3-b134-6c5157f414b0 started up.
 INFO   | jvm 1    | 2026/07/06 11:19:50 | 2026-07-06 11:19:50,361[INFO]io.mycat.vertx.VertxMycatServer.lambda$start$1:120Mycat Vertx server 9ad86371-edbd-4c74-981b-ca320f1fc723 started up.
 INFO   | jvm 1    | 2026/07/06 11:19:50 | 2026-07-06 11:19:50,361[INFO]io.mycat.vertx.VertxMycatServer.lambda$start$1:120Mycat Vertx server 5b8f2992-ed58-4d80-a023-503ec2546c6e started up.
 INFO   | jvm 1    | 2026/07/06 11:19:50 | 2026-07-06 11:19:50,363[INFO]io.mycat.vertx.VertxMycatServer.lambda$start$1:120Mycat Vertx server dddf9d06-8a65-4161-a23b-9e90c67499bd started up.
 INFO   | jvm 1    | 2026/07/06 11:19:50 | 2026-07-06 11:19:50,364[INFO]io.mycat.vertx.VertxMycatServer.lambda$start$1:120Mycat Vertx server 66301832-afe9-40c6-a944-8c3a9076bce4 started up.
 ​
 # 连接MyCAT
 我们在外面看MyCAT,认为MyCAT 就是一个MySQL,怎么连接MySQL?
 ​
 MyCAT的默认用户是:root
 初始密码是:123456
 端口号是:8066
 ​
 1) 使用MySQL默认客户端或者mycli命令行登陆MyCAT
 ​
 mysql -uroot -p123456 -P8066 -h127.0.0.1
 # 或者
 mycli -uroot -P8066 -h"127.0.0.1"
 ​
 2) Navicat for MySQL 对MyCAT支持查询
 3) SQLyog 操作MyCAT更友好