# docker-mycat
**Repository Path**: yeon/docker-mycat
## Basic Information
- **Project Name**: docker-mycat
- **Description**: docker创建mycat
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2019-09-25
- **Last Updated**: 2024-12-11
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# 使用docker创建mycat mysql主从服务器
### 拉取 [github项目](https://github.com/liuwel/docker-mycat "github")
配置文件已经全部写好 基本找下面流程走一遍就能直接用
注意:mycat 和 mysql使用的字符集编码全部是 utf8mb4
```shell
% cd ~ #切换到主目录
% git clone https://github.com/liuwel/docker-mycat.git
% tree docker-mycat
├ compose
│ ├ docker-compose.yml
│ ├ master
│ │ └ Dockerfile
│ ├ mycat
│ │ ├ Dockerfile
│ │ └ Mycat-server-1.6.5-release-20180122220033-linux.tar.gz
│ ├ s1
│ │ └ Dockerfile
│ └ s2
│ └ Dockerfile
├ config
│ ├ hosts
│ ├ mycat
│ │ ├ autopartition-long.txt
│ │ ├ auto-sharding-long.txt
│ │ ├ auto-sharding-rang-mod.txt
│ │ ├ cacheservice.properties
│ │ ├ ehcache.xml
│ │ ├ index_to_charset.properties
│ │ ├ log4j2.xml
│ │ ├ migrateTables.properties
│ │ ├ myid.properties
│ │ ├ partition-hash-int.txt
│ │ ├ partition-range-mod.txt
│ │ ├ rule.xml
│ │ ├ schema.xml
│ │ ├ sequence_conf.properties
│ │ ├ sequence_db_conf.properties
│ │ ├ sequence_distributed_conf.properties
│ │ ├ sequence_time_conf.properties
│ │ ├ server.xml
│ │ ├ sharding-by-enum.txt
│ │ ├ wrapper.conf
│ │ ├ zkconf
│ │ │ ├ autopartition-long.txt
│ │ │ ├ auto-sharding-long.txt
│ │ │ ├ auto-sharding-rang-mod.txt
│ │ │ ├ cacheservice.properties
│ │ │ ├ ehcache.xml
│ │ │ ├ index_to_charset.properties
│ │ │ ├ partition-hash-int.txt
│ │ │ ├ partition-range-mod.txt
│ │ │ ├ rule.xml
│ │ │ ├ schema.xml
│ │ │ ├ sequence_conf.properties
│ │ │ ├ sequence_db_conf.properties
│ │ │ ├ sequence_distributed_conf-mycat_fz_01.properties
│ │ │ ├ sequence_distributed_conf.properties
│ │ │ ├ sequence_time_conf-mycat_fz_01.properties
│ │ │ ├ sequence_time_conf.properties
│ │ │ ├ server-mycat_fz_01.xml
│ │ │ ├ server.xml
│ │ │ └ sharding-by-enum.txt
│ │ └ zkdownload
│ │ └ auto-sharding-long.txt
│ ├ mycat-logs
│ │ ├ mycat.log
│ │ ├ mycat.pid
│ │ └ wrapper.log
│ ├ mysql-master
│ │ ├ conf.d
│ │ │ ├ client.cnf
│ │ │ ├ docker.cnf
│ │ │ └ mysql.cnf
│ │ ├ my.cnf
│ │ ├ mysql.cnf
│ │ └ mysql.conf.d
│ │ └ mysqld.cnf
│ ├ mysql-s1
│ │ ├ conf.d
│ │ │ ├ client.cnf
│ │ │ ├ docker.cnf
│ │ │ └ mysql.cnf
│ │ ├ my.cnf
│ │ ├ mysql.cnf
│ │ └ mysql.conf.d
│ │ └ mysqld.cnf
│ └ mysql-s2
│ ├ conf.d
│ │ ├ client.cnf
│ │ ├ docker.cnf
│ │ └ mysql.cnf
│ ├ my.cnf
│ ├ mysql.cnf
│ └ mysql.conf.d
│ └ mysqld.cnf
└ README.md
19 directories, 69 files
```
#### mysql 主从服务器的配置已经写在config对应的目录中
mysql-m1 : 主服务器 IP:172.18.0.2
mysql-s1 : 从服务器slave1 IP:172.18.0.3
mysql-s2 : 从服务器slave2 IP:172.18.0.4
mycat : Mycat服务器 IP:172.18.0.5
### 修改hosts文件 添加解析
```shell
% sudo vi /etc/hosts
# docker-mycat m1:mysql-master主服务器 s1,s2:mysql-slave 从服务器
# mycat mycat中间件服务器
172.18.0.2 m1
172.18.0.3 s1
172.18.0.4 s2
172.18.0.5 mycat
127.0.0.1 local
```
### docker-compose.yml配置文件
```shell
% cd ~/docker-mycat/compose
% cat docker-compose.yml
```
```yml
version: '2'
services:
m1:
build: ./mysql_m1
container_name: m1
volumes:
- ../config/mysql-master/:/etc/mysql/:ro
- /etc/localtime:/etc/localtime:ro
- ../config/hosts:/etc/hosts:ro
ports:
- "3309:3306"
networks:
mysql:
ipv4_address: 172.18.0.2
ulimits:
nproc: 65535
hostname: m1
mem_limit: 512m
restart: always
environment:
MYSQL_ROOT_PASSWORD: m1test
s1:
build: ./mysql_s1
container_name: s1
volumes:
- ../config/mysql-s1/:/etc/mysql/:ro
- /etc/localtime:/etc/localtime:ro
- ../config/hosts:/etc/hosts:ro
ports:
- "3307:3306"
networks:
mysql:
ipv4_address: 172.18.0.3
links:
- m1
ulimits:
nproc: 65535
hostname: s1
mem_limit: 512m
restart: always
environment:
MYSQL_ROOT_PASSWORD: s1test
s2:
build: ./mysql_s2
container_name: s2
volumes:
- ../config/mysql-s2/:/etc/mysql/:ro
- /etc/localtime:/etc/localtime:ro
- ../config/hosts:/etc/hosts:ro
ports:
- "3308:3306"
links:
- m1
networks:
mysql:
ipv4_address: 172.18.0.4
ulimits:
nproc: 65535
hostname: s2
mem_limit: 512m
restart: always
environment:
MYSQL_ROOT_PASSWORD: s2test
mycat:
build: ./mycat
container_name: mycat
volumes:
- ../config/mycat/:/mycat/conf/:ro
- ../config/mycat-logs/:/mycat/logs/:rw
- /etc/localtime:/etc/localtime:ro
- ../config/hosts:/etc/hosts:ro
ports:
- "8066:8066"
- "9066:9066"
links:
- m1
- s1
- s2
networks:
mysql:
ipv4_address: 172.18.0.5
ulimits:
nproc: 65535
hostname: mycat
mem_limit: 512m
restart: always
networks:
mysql:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.18.0.0/24
gateway: 172.18.0.1
```
### Build 镜像
```shell
% sudo docker-compose build m1 s1 s2
Building m1
Step 1/4 : FROM mysql:5.7.17
---> 9546ca122d3a
...
Successfully built cffffead5570
Successfully tagged compose_s2:latest
```
### 运行 docker mysql主从数据库 (mysql数据库密码在yml文件里面)
```shell
% sudo docker-compose up -d m1 s1 s2
Creating m1
Creating s2
Creating s1
```
### mysql主从配置
#### 配置m1主服务器
```shell
sudo docker exec -it m1 /bin/bash
root@m1:/# mysql -uroot -pm1test
mysql: [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 3
Server version: 5.7.17-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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.
mysql>
```
已经进入m1主服务器mysql 命令行
创建用于主从复制的用户repl
```shell
mysql> create user repl;
```
给repl用户授予slave的权限
```shell
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'172.18.0.%' IDENTIFIED BY 'repl';
Query OK, 0 rows affected, 1 warning (0.00 sec)
```
锁表
```shell
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
```
查看binlog状态 记录File 和 Position 状态稍后从库配置的时候会用
```shell
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000003 | 644 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
```
#### 配置从库s1 s2
进入s1 shell
```shell
% sudo docker exec -it s1 /bin/bash
root@s1:/# mysql -uroot -ps1test
mysql> change master to master_host='m1',master_port=3306,master_user='repl',master_password='repl',master_log_file='master-bin.000003',master_log_pos=644;
Query OK, 0 rows affected, 2 warnings (0.05 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
```
进入s2 shell
```shell
sudo docker exec -it s2 /bin/bash
root@s2:/# mysql -uroot -ps2test
mysql> change master to master_host='m1',master_port=3306,master_user='repl',master_password='repl',master_log_file='master-bin.000003',master_log_pos=644;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
```
### mysql主从配置完成 现在测试一下
登陆主数据库 创建masterdb数据库 (这个数据库名在稍后的mycat里面会用到)
```shell
% mysql -uroot -pm1test -hm1
MySQL [(none)]> create database masterdb;
Query OK, 1 row affected (0.01 sec)
```
进入从库看看数据库是否创建
```shell
% mysql -uroot -ps1test -hs1
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| masterdb |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
```
可以看到从库也已经创建成功了 到这里msyql的主从已经配置完成了
接下来是mycat的配置其实在 ~/config/mycat 里面已经配置好了直接就可以用了
看下schama.xml配置文件
```shell
% cat ~/config/mycat/schema.xml
```
```xml
select user()
```
server.xml 配置文件
```xml
utf8mb4
0
0
2
0
0
1
1m
1k
0
384m
true
youpassword
masterdb
```
### 启动mycat
```shell
% cd ~/docker-mycat/compose
% sudo docker-compose up -d mycat
```
### 整体测试
```shell
% mysql -uroot -p -P8066 -hlocal
```
```shell
MySQL \[(none)\]> show databases;
+----------+
| DATABASE |
+----------+
| masterdb |
+----------+
1 row in set (0.00 sec)
```
### 测试数据
```shell
MySQL [(none)]> use masterdb
Database changed
MySQL [masterdb]> CREATE TABLE `test_table` (
-> `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键',
-> `title` varchar(255) DEFAULT NULL COMMENT '标题',
-> `content` text COMMENT '内容',
-> PRIMARY KEY (`id`)
-> ) ENGINE=InnoDB COMMENT='测试表'
-> ;
Query OK, 0 rows affected (0.03 sec)
```
```shell
MySQL [masterdb]> show tables;
+--------------------+
| Tables_in_masterdb |
+--------------------+
| test_table |
+--------------------+
1 row in set (0.00 sec)
```
```shell
MySQL [masterdb]> INSERT INTO `test_table` VALUES ('1', '测试标题1', '测试内容1');
Query OK, 1 row affected (0.01 sec)
MySQL [masterdb]> INSERT INTO `test_table` VALUES ('2', '测试标题2', '测试内容2');
Query OK, 1 row affected (0.01 sec)
MySQL [masterdb]> INSERT INTO `test_table` VALUES ('3', '测试标题3', '测试内容3');
Query OK, 1 row affected (0.01 sec)
MySQL [masterdb]> INSERT INTO `test_table` VALUES ('4', '测试标题4', '测试内容4');
Query OK, 1 row affected (0.01 sec)
MySQL [masterdb]> INSERT INTO `test_table` VALUES ('5', '测试标题5', '测试内容5');
Query OK, 1 row affected (0.01 sec)
MySQL [masterdb]> INSERT INTO `test_table` VALUES ('6', '测试标题6', '测试内容6');
Query OK, 1 row affected (0.01 sec)
```
```shell
MySQL [masterdb]> select * from test_table;
+----+---------------+---------------+
| id | title | content |
+----+---------------+---------------+
| 1 | 测试标题1 | 测试内容1 |
| 2 | 测试标题2 | 测试内容2 |
| 3 | 测试标题3 | 测试内容3 |
| 4 | 测试标题4 | 测试内容4 |
| 5 | 测试标题5 | 测试内容5 |
| 6 | 测试标题6 | 测试内容6 |
+----+---------------+---------------+
6 rows in set (0.01 sec)
```