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

Spring Boot迁AWS DocumentDB启用Mongock遇writeConcern不支持及连接问题

Spring Boot + Mongock 迁移至AWS DocumentDB 问题排查与解决思路

问题背景

原基于MongoDB的Spring Boot应用,依赖Mongock执行数据库初始化脚本。迁移至AWS DocumentDB后:

  • 禁用Mongock时,应用可正常连接DocumentDB并完成CRUD操作
  • 启用Mongock时,启动报错提示Field 'writeConcern' is currently not supported,但日志显示已成功连接DocumentDB
  • AWS支持判定为Mongock驱动兼容性问题,且原MongoDB环境下Mongock运行正常
  • 尝试在连接字符串添加replicaSet=rs0后,出现连接拒绝超时错误

相关错误日志

1. 启用Mongock时的writeConcern错误

2024-02-16 14:18:54.267  INFO 49084 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:27018], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2024-02-16 14:18:54.791  INFO 49084 --- [           main] org.mongodb.driver.cluster               : Cluster description not yet available. Waiting for 30000 ms before timing out
2024-02-16 14:18:55.251  INFO 49084 --- [localhost:27018] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:1}] to localhost:27018
2024-02-16 14:18:55.252  INFO 49084 --- [localhost:27018] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27018, type=SHARD_ROUTER, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=13, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=572555100}
2024-02-16 14:18:56.493 ERROR 49084 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'connectionDriver' defined in class path resource [com/github/cloudyrock/mongock/driver/mongodb/springdata/v3/config/SpringDataMongoV3Context.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.github.cloudyrock.mongock.driver.api.driver.ConnectionDriver]: Factory method 'connectionDriver' threw exception; nested exception is com.mongodb.MongoCommandException: Command failed with error 303: 'Field 'writeConcern' is currently not supported' on server localhost:27018. The full response is {"code": 303, "errmsg": "Field 'writeConcern' is currently not supported", "ok": 0.0, "errorUUID": "9f98fcb2-118a-4a8b-b735-495c14b92af3"}
...

2. 添加replicaSet=rs0后的连接拒绝错误

2024-02-19 10:53:23.832  INFO 73292 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:27018], mode=MULTIPLE, requiredClusterType=REPLICA_SET, serverSelectionTimeout='30000 ms', requiredReplicaSetName='rs0'}
2024-02-19 10:53:24.245  INFO 73292 --- [localhost:27018] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server localhost:27018

com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.2.3.jar:na]
Caused by: java.net.ConnectException: Connection refused: connect
...

解决思路

1. 强制指定DocumentDB兼容的WriteConcern

DocumentDB不支持MongoDB部分高级WriteConcern参数,需显式配置兼容的参数(AWS默认支持w=1):

  • 配置文件方式:
    mongock.mongo-driver.write-concern.w=1
    
  • Java代码配置方式:
    @Bean
    public MongockConfiguration mongockConfiguration(MongoClient mongoClient) {
        return MongockConfiguration.builder()
                .setMongoClient(mongoClient)
                .setDatabaseName("your-db-name")
                .setChangeLogScanPackage("your.changelog.package")
                .setWriteConcern(WriteConcern.W1)
                .build();
    }
    

2. 升级/更换Mongock版本

部分旧版Mongock对DocumentDB兼容性不足,尝试升级到最新稳定版(推荐5.x+),该版本官方对DocumentDB的适配有优化。

3. 移除replicaSet配置,改用单节点模式

添加replicaSet=rs0后出现连接拒绝,说明当前DocumentDB实例并非副本集模式(或本地测试端口映射存在问题),恢复连接字符串为单节点模式即可。

4. 自定义MongoClient供Mongock使用

手动创建符合DocumentDB要求的MongoClient,避免Mongock自动生成的配置包含不兼容字段:

@Bean
public MongoClient mongoClient() {
    ConnectionString connectionString = new ConnectionString("mongodb://localhost:27018/your-db");
    MongoClientSettings settings = MongoClientSettings.builder()
            .applyConnectionString(connectionString)
            .writeConcern(WriteConcern.W1)
            .build();
    return MongoClients.create(settings);
}

让Mongock直接使用该自定义Bean,而非自动创建客户端。

5. 排查变更日志脚本

检查Mongock的变更脚本中是否使用了DocumentDB不支持的MongoDB特定操作(如自定义写关注语法、特定命令),先简化脚本执行基础初始化,逐步定位问题点。

内容的提问来源于stack exchange,提问作者sonic-cloud-arch

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 10:02:28