在这里讲述怎样部署mysql-innodb-cluster 怎样在springboot中配置读写分离请参考本人的另一个仓库back-monolithic的cluster分支,使用动态数据源进行配置 所有的mysql相关镜像版本均为8.0.32 8.0以上(8.4,9.0)暂不支持innodb-cluster 首先创建项目对应目录,可以直接用我的 3个mysql.cnf配置文件和docker-compose.yml文件
注意!!!所有的命令需要在包含docker-compose.yml文件的目录下运行
执行前删除data/node${1..3}目录下的所有文件
首先运行命令
docker-compose up -d mysql1 mysql2 mysql3
首次运行可能会尝试拉取镜像,即使镜像已经存在,也会尝试拉取,这是正常的,再次运行即可

这里需要等待容器启动完毕,大概最多等个60s
然后运行命令连接到mysql容器 密码和用户名可以自定义
docker-compose exec mysql1 mysql -uroot -p123456

运行下一条命令创建用户 密码和用户名可以自定义
CREATE USER IF NOT EXISTS 'clusteradmin'@'%' IDENTIFIED WITH mysql_native_password BY 'clusterpass123';
授权
GRANT ALL PRIVILEGES ON . TO 'clusteradmin'@'%' WITH GRANT OPTION;
保存
FLUSH PRIVILEGES;
退出当前容器
exit

mysql2和mysql3操作同上类似
完成用户创建和授权后,配置和启动 InnoDB Cluster,可以使用 MySQL Shell (mysqlsh) 来创建集群。临时启动一个 MySQL Shell 容器。
docker run --rm -it --network=mysql-cluster-net mysql/mysql-server:8.0.32 mysqlsh

// 连接到第一个节点(将成为初始主节点)
\connect clusteradmin@mysql1:3306
这里需要输入密码并保存 clusterpass123
// 检查实例配置是否符合集群要求
dba.checkInstanceConfiguration('clusteradmin@mysql1:3306');
MySQL mysql1:3306 ssl JS > dba.checkInstanceConfiguration('clusteradmin@mysql1:3306');
Validating MySQL instance at mysql1:3306 for use in an InnoDB cluster...
This instance reports its own address as mysql1:3306 Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.
Checking whether existing tables comply with Group Replication requirements... No incompatible tables detected
Checking instance configuration...
NOTE: Some configuration options need to be fixed: +----------------------------------------+---------------+----------------+--------------------------------------------------+ | Variable | Current Value | Required Value | Note | +----------------------------------------+---------------+----------------+--------------------------------------------------+ | binlog_transaction_dependency_tracking | COMMIT_ORDER | WRITESET | Update the server variable | | enforce_gtid_consistency | OFF | ON | Update read-only variable and restart the server | | gtid_mode | OFF | ON | Update read-only variable and restart the server | | server_id | 1 | | Update read-only variable and restart the server | +----------------------------------------+---------------+----------------+--------------------------------------------------+
Some variables need to be changed, but cannot be done dynamically on the server. NOTE: Please use the dba.configureInstance() command to repair these issues.
{ "config_errors": [ { "action": "server_update", "current": "COMMIT_ORDER", "option": "binlog_transaction_dependency_tracking", "required": "WRITESET" }, { "action": "server_update+restart", "current": "OFF", "option": "enforce_gtid_consistency", "required": "ON" }, { "action": "server_update+restart", "current": "OFF", "option": "gtid_mode", "required": "ON" }, { "action": "server_update+restart", "current": "1", "option": "server_id", "required": "" } ], "status": "error" // 这里会报错,不能配置集群 }
// 自动修复
dba.configureInstance('clusteradmin@mysql1:3306')
这里会有两个确认,第一个是保存配置确认,第二个是重启容器确认,重启容器一般会失败,等会儿我们手动重启
剩下两个节点重复以上操作
重启容器,需要新开一个终端窗口,不能咋mysql shell里面运行
docker-compose restart mysql1 mysql2 mysql3

重新连接到第一个节点并测试是否可以配置集群 docker run --rm -it --network=mysql-cluster-net mysql/mysql-server:8.0.32 mysqlsh
\connect clusteradmin@mysql1:3306
dba.checkInstanceConfiguration('clusteradmin@mysql1:3306');

创建集群
var cluster = dba.createCluster('myCluster');

通过复制将其它两个节点加入集群 需要注意替换为你自己的用户名和密码
cluster.addInstance('clusteradmin@mysql2:3306', {password: 'clusterpass123', recoveryMethod: 'clone'});
cluster.addInstance('clusteradmin@mysql3:3306', {password: 'clusterpass123', recoveryMethod: 'clone'});

检查集群状态
cluster.status();
输出应显示三个实例都是 ONLINE,其中一个角色是 PRIMARY,另外两个是 SECONDARY。
启动并配置 MySQL Router 新开命令行
docker-compose up -d mysql-router

测试连接
"6446" # 读写端口 (RW)
"6447" # 只读端口 (RO)

只读

复制  
注意!!!关闭容器后重启时不会默认启动集群 首先运行命令,启动容器 docker-compose up -d mysql1 mysql2 mysql3
等待60s让容器完全启动
以sql模式进入mysql1容器,
docker exec -it mysql1 mysqlsh --sql

查询集群状态
SELECT * FROM performance_schema.replication_group_members;
OFFLINE表示未启动
切换到js模式
\js

开启集群
dba.rebootClusterFromCompleteOutage();

切换回sql模式,并验证集群状态
\sql
SELECT * FROM performance_schema.replication_group_members;

启动mysql-router docker-compose up -d mysql-router
测试连接
mysql -h 127.0.0.1 -P 6446 -u clusteradmin -p
