使用YCSB基准测试MongoDB Atlas遇连接问题,求正确配置
问题描述
我想用YCSB基准测试工具测试MongoDB Atlas的性能。在Ubuntu本地MongoDB实例上测试一切正常,但测试云端Atlas时遇到连接问题:
使用Atlas提供的Java连接字符串(
mongodb+srv://<user>:<password>@cluster0.swk7mjl.mongodb.net/?retryWrites=true&w=majority)执行加载命令:./bin/ycsb load mongodb -s -P workloads/workloada -p mongodb.url=mongodb+srv://user:password@cluster0.swk7mjl.mongodb.net/?retryWrites=true&w=majority -p mongodb.auth="true" -p recordcount=1000 > outputLoad.txt得到错误:
ERROR: Invalid URL: 'mongodb+srv://cluster0.swk7mjl.mongodb.net/?retryWrites=true'. Must be of the form 'mongodb://<host1>:<port1>,<host2>:<port2>/database?options'. http://docs.mongodb.org/manual/reference/connection-string/去掉
srv后缀后重新执行:./bin/ycsb load mongodb -s -P workloads/workloada -p mongodb.url=mongodb://user:password@cluster0.swk7mjl.mongodb.net/?retryWrites=true&w=majority -p mongodb.auth="true" -p recordcount=1000 > outputLoad.txt出现超时错误:
Exception while trying bulk insert with 0 com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches PrimaryServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=cluster0.swk7mjl.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: cluster0.swk7mjl.mongodb.net}, caused by {java.net.UnknownHostException: cluster0.swk7mjl.mongodb.net}}]尝试分开传入用户名密码参数:
./bin/ycsb load mongodb -s -P workloads/workloada -p mongodb.url=mongodb://cluster0.swk7mjl.mongodb.net/?retryWrites=true&w=majority -p mongodb.username=user -p mongodb.password=password -p mongodb.auth="true" -p recordcount=1000 > outputLoad.txt仍出现相同超时错误。
已确认Atlas集群白名单添加了0.0.0.0/0,且能通过Mongo Shell正常连接。
解决方法
1. 核心原因
YCSB的MongoDB客户端绑定目前不支持mongodb+srv://格式的连接字符串,无法自动解析SRV记录获取集群节点,因此需要手动解析并组装标准连接串。
2. 手动解析SRV记录
在Ubuntu终端执行以下命令解析Atlas集群的SRV记录:
dig _mongodb._tcp.cluster0.swk7mjl.mongodb.net SRV
输出中会包含所有集群节点的地址(target字段),例如:
_mongodb._tcp.cluster0.swk7mjl.mongodb.net. 300 IN SRV 0 0 27017 cluster0-shard-00-00.swk7mjl.mongodb.net. _mongodb._tcp.cluster0.swk7mjl.mongodb.net. 300 IN SRV 0 0 27017 cluster0-shard-00-01.swk7mjl.mongodb.net. _mongodb._tcp.cluster0.swk7mjl.mongodb.net. 300 IN SRV 0 0 27017 cluster0-shard-00-02.swk7mjl.mongodb.net.
3. 组装标准连接字符串
将解析得到的所有节点地址组装成标准mongodb://格式的连接串,注意添加ssl=true(Atlas强制要求SSL连接),格式如下:
mongodb://<user>:<password>@cluster0-shard-00-00.swk7mjl.mongodb.net:27017,cluster0-shard-00-01.swk7mjl.mongodb.net:27017,cluster0-shard-00-02.swk7mjl.mongodb.net:27017/?ssl=true&retryWrites=true&w=majority
4. 修正YCSB命令
将组装好的连接串填入命令,注意用引号包裹URL避免shell解析&符号:
./bin/ycsb load mongodb -s -P workloads/workloada -p mongodb.url="mongodb://user:password@cluster0-shard-00-00.swk7mjl.mongodb.net:27017,cluster0-shard-00-01.swk7mjl.mongodb.net:27017,cluster0-shard-00-02.swk7mjl.mongodb.net:27017/?ssl=true&retryWrites=true&w=majority" -p mongodb.auth="true" -p recordcount=1000 > outputLoad.txt
额外注意事项
- 确保YCSB使用的MongoDB驱动版本足够新(建议3.10+),否则可能无法兼容Atlas的TLS 1.2/1.3要求。
- 如果仍有问题,可添加
mongodb.ssl=true参数显式启用SSL连接。
内容的提问来源于stack exchange,提问作者ruebe99
相关产品推荐
相关产品推荐

