mongodb连接

2019-04-16  本文已影响0人  CXY_XZL

此处使用MongoDB shell 来连接 Mongodb 服务

标准 URI 连接语法:(中括号里的内容为可选,根据具体业务需要选择)

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

options详解

options-1.png options-2.png

MongoDB 连接命令格式

$ ./mongo
MongoDB shell version: 3.0.6
connecting to: test

mongodb://admin:123456@localhost/

mongodb://admin:123456@localhost/test

*连接本地数据库服务器,端口是默认的。

mongodb://localhost

mongodb://fred:foobar@localhost

mongodb://fred:foobar@localhost/baz

mongodb://example1.com:27017,example2.com:27017

mongodb://localhost,localhost:27018,localhost:27019

mongodb://host1,host2,host3/?slaveOk=true

mongodb://host1,host2,host3/?connect=direct;slaveOk=true

当你的连接服务器有优先级,还需要列出所有服务器,你可以使用上述连接方式。

mongodb://localhost/?safe=true

mongodb://host1,host2,host3/?safe=true;w=2;wtimeoutMS=2000

参考资料

上一篇 下一篇

猜你喜欢

热点阅读