You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Testcontainers-Go创建单节点MySQL集群用于端到端测试

在Testcontainers-Go中创建单节点MySQL Group Replication集群

我平时通过mysqlsh登录服务器,执行以下命令创建集群:

dba.checkInstanceConfiguration()
dba.createCluster("mycluster")

现在需要在Testcontainers中创建单节点MySQL集群用于端到端测试,但用Testcontainers-Go尝试的方案未生效,具体情况如下:

当前尝试的容器构建代码

testExec := testcontainers.NewRawCommand([]string{`echo "dba.configureInstance();dba.createCluster(\"mycluster\") | mysqlsh -ppassword -uroot`})
mysqlContainer, err := mysql.RunContainer(ctx,
    testcontainers.WithImage(mysqlImageVersion),
    mysql.WithUsername("root"),
    mysql.WithPassword("password"),
    mysql.WithDatabase("mysql"),
    testcontainers.WithAfterReadyCommand(testExec),
    mysql.WithConfigFile(filepath.Join("test", "test.cnf")),
)

配置文件test.cnf内容

[mysqld]
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql

server_id                               = 12345678
binlog_transaction_dependency_tracking  = WRITESET
gtid_mode                           = ON
enforce_gtid_consistency            = ON

pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/run/mysqld/mysqld.sock

遇到的问题

执行查询主节点的语句:

SELECT member_host FROM performance_schema.global_status JOIN performance_schema.replication_group_members WHERE variable_name = 'group_replication_primary_member' AND member_id=variable_value

返回空结果,预期应得到主实例名称。


解决方案

1. 修正命令语法错误

原AfterReadyCommand存在两处关键问题:

  • 管道符位置错误,需将echo输出的脚本传递给mysqlsh,而非嵌套在mysqlsh参数中
  • 未通过shell解析命令,RawCommand直接传单字符串无法处理管道、echo等shell语法

修正后的命令通过sh -c执行:

testExec := testcontainers.NewRawCommand([]string{
    "sh", "-c",
    `echo "dba.configureInstance({password: 'password', interactive: false}); dba.createCluster('mycluster', {singlePrimaryMode: true, force: true})" | mysqlsh -uroot -ppassword --no-wizard`,
})
  • interactive: false:禁用mysqlsh交互提示,确保非自动化执行
  • force: true:允许单节点启动集群(默认要求至少3节点)
  • --no-wizard:禁用向导模式,避免交互流程

2. 补充Group Replication必要配置

在test.cnf的[mysqld]段添加以下参数:

# Group Replication 核心配置
group_replication_group_name = "e2e7d2a0-1f2b-4c3d-8e9f-0a1b2c3d4e5f" # 任意合法UUID
group_replication_start_on_boot = OFF
group_replication_local_address = "0.0.0.0:33061"
group_replication_group_seeds = "0.0.0.0:33061"
group_replication_single_primary_mode = ON
group_replication_enforce_update_everywhere_checks = OFF
log_slave_updates = ON
plugin_load_add = 'group_replication.so'

这些参数是Group Replication初始化的基础,缺少会导致集群无法正常启动。

3. 完整容器构建代码

mysqlContainer, err := mysql.RunContainer(ctx,
    testcontainers.WithImage(mysqlImageVersion), // 推荐使用MySQL 8.0+版本,如mysql:8.0.36
    mysql.WithUsername("root"),
    mysql.WithPassword("password"),
    mysql.WithDatabase("mysql"),
    testcontainers.WithAfterReadyCommand(testExec),
    mysql.WithConfigFile(filepath.Join("test", "test.cnf")),
)

验证

容器启动完成后,执行原查询语句即可返回主节点的member_host(容器内部主机名或IP)。


内容的提问来源于stack exchange,提问作者AlexTheOpsGuy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 15:35:02