Connection



You cannot connect to MongoDB by going to http://localhost:27017 in your web browser. The database cannot be accessed via HTTP on port 27017.

Standard Connection String Format

The uri scheme described on this page is not yet supported by all of the drivers. Refer to a specific driver's documentation to see how much (if any) of the standard connection uri is supported. All drivers support an alternative method of specifying connections if this format is not supported.
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
  • mongodb:// is a required prefix to identify that this is a string in the standard connection format.
  • username:password@ are optional. If given, the driver will attempt to login to a database after connecting to a database server.//通过uri一次登录到某个数据库上时才用需要此项,若不需要上来就连到到非test数据库,则可不写。
  • host1 is the only required part of the URI. It identifies a server address to connect to.
  • :portX is optional and defaults to :27017 if not provided.
  • /database is the name of the database to login to and thus is only relevant if the username:password@ syntax is used. If not specified the "admin" database will be used by default.
  • ?options are connection options. Note that if database is absent (指不显示指定时)there is still a / required between the last host and the ?introducing the options. Options are name=value pairs and the pairs are separated either by "&"" or ";".
  • A database consists of one or more collections, the documents (objects) in those collections, and an optional set of security credentials(证书) for controlling access.



As many hosts as necessary may be specified (for connecting to replica pairs/sets,2个副本(其实没有真本)叫pair,多余2个副本 叫 set).//数据库或option共用一个,因为多台机子应相同

The options are:

Replica set:

  • replicaSet=name
    • The driver verifies that the name of the replica set it connects to matches this name. Implies that the hosts given are a seed list, and the driver will attempt to find all members of the set.

Single server:

  • slaveOk=true|false

Any configuration:

  • safe=true|false
    • true: the driver sends a getLastError command after every update to ensure that the update succeeded (see also w andwtimeoutMS).
    • false: the driver does not send a getLastError command after every update.
  • w=n
    • The driver adds { w : n } to the getLastError command. Implies safe=true.
  • wtimeoutMS=ms
    • The driver adds { wtimeout : ms } to the getlasterror command. Implies safe=true.
  • fsync=true|false
    • true: the driver adds { fsync : true } to the getlasterror command. Implies safe=true.
    • false: the driver does not not add fsync to the getlasterror command.
  • journal=true|false
    • true: Sync to journal. Implies safe=true.
  • connectTimeoutMS=ms
    • How long a connection can take to be opened before timing out.
  • socketTimeoutMS=ms
    • How long a send or receive on a socket can take before timing out.

These options are not case sensitive.

Examples

Connect to a database server running locally on the default port:

mongodb://localhost

Connect and login to the admin database as user "fred" with password "foobar":

mongodb://fred:foobar@localhost

Connect and login to the "baz" database as user "fred" with password "foobar":

mongodb://fred:foobar@localhost/baz

Connect to a replica pair, with one server on example1.com and another server on example2.com:

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

Connect to a replica set with three servers running on localhost (on ports 27017, 27018, and 27019):

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

Connect to a replica set with three servers, sending all writes to the primary and distributing reads to the secondaries:

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

Connect to localhost with safe mode on:

mongodb://localhost/?safe=true

Connect to a replica set with safe mode on, waiting for replication to succeed on at least two machines, with a two second timeout:

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

Connection Pooling

 
The server will use one thread per TCP connection, therefore it is highly recomended that your application use some sort of connection pooling ????. Luckily, most drivers handle this for you behind the scenes. One notable exception is setups where your app spawns a new process for each request, such as CGI and some configurations of PHP.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值