MongoDB's Connection string

Source: Internet
Author: User
Tags mongodb connection string safe mode

This article is guided by: MongoDB database compared with the traditional relational database, it has simple operation, completely free, open source and other characteristics, which makes MongoDB products widely used in a variety of large-scale portals and professional web sites. Since MongoDB connection does not support the HTTP protocol, all you cannot access MongoDB directly through the browser, the following details the writing of the connection string in MongoDB

One, MongoDB connection string Common format

mongodb:// [Username:[email protected]]host1[:p ort1][,host2[:p Ort2],... [, hostn[:p Ortn]] [/[database][?options]]

Note : String connections are not case-sensitive, not all MongoDB drivers support the full connection string, drivers that do not support this format will have alternative connection schemes, refer to the documentation for the driver itself to see how to define the URI standard connection.

1. Parameter description

mongodb://this is a fixed format that must be specified.

? Username:[email protected] optional, if set, after connecting to the database server, the driver will attempt to log into this database? Host1 must specify at least one host

host1 is the only one to fill in this URI. It specifies the address to connect to the server. If you are connecting to a replica set, specify multiple host addresses.


? :p ortx Optional Port, default is 27017 if not filled

? /database If you specify Username:[email protected], connect and verify the login to the specified database. If not specified, the admin database is opened by default.


?? options is the connection option. If you do not use/database, you need to add/. All connection options are key-value pairs Name=value, and key-value pairs are separated by & or; (semicolon)

2. Options is the connection parameter

    • Connect=direct|replicaset
      • Direct: Connect as a single server. If more than one host address is provided, the connection is established and accessed sequentially. If only one host is specified, direct is the default value.
      • Replicaset: As described, connect to replica set. This host address list is to discover replica set. If you connect multiple hosts Replicaset is the default value.

    • Replicaset=name
      • Verify the name of the replica set. impliesConnect=replicaset.

    • Slaveok=true|false
      • true: In Connect=direct mode, the driver will connect to the first machine, even if the server is not the primary one. In Connect=replicaset mode, the driver sends all write requests to the master and distributes the read operations to the other slave servers.
      • false: In connect=direct mode, the driver will automatically find the primary server . In connect=replicaset mode, the driver only connects to the primary server, and all read and write commands are connected to the primary server.

    • Safe=true|false
      • true: After the update operation, the driver sends the GetLastError command to ensure that the update succeeds. (also refer to Wtimeoutms).
      • false: After each update, the driver does not send GetLastError to ensure that the update succeeds.

    • W=n
      • The driver adds {w:n} to the GetLastError command. Apply to Safe=true.

    • Wtimeoutms=ms
      • The driver adds {wtimeout:ms} to the GetLastError command. Apply to safe=true.

    • Fsync=true|false
      • true: The driver adds {fsync:true} to the GetLastError command. Applies to safe=true.
      • false: The driver is not added to the GetLastError command.

    • Maxpoolsize=n
    • Minpoolsize=n
      • Some drivers will turn off the useless connection. However, if the number of connections is below the minpoolsize value, they do not close idle connections. Note that the connection is created as needed, so the minpoolsize does not take effect when the connection pool is pre-populated by many connections.

    • Waitqueuetimeoutms=ms
      • The total time that the thread waits for the connection to take effect before timing out. This parameter takes effect if the connection pool reaches the maximum and all connections are in use.

    • Waitqueuemultiple=n
      • The driver forcibly restricts the number of threads waiting for a connection at the same time. This limits the multiple of the connection pool.

    • Connecttimeoutms=ms
      • The time at which the connection can be opened.

    • Sockettimeoutms=ms
      • Time to send and receive sockets

Ii. The connection string instance of MongoDB

1, connect the local database server, the port is the default.

Mongodb://localhost

2. Use user name Fred, password Foobar login to the admin database of localhost.

Mongodb://fred:[email protected]

3. Use user name Fred, password Foobar login to localhost's Baz database

Mongodb://fred:[email Protected]/baz

4, connect replica pair, server 1 is example1.com server 2 is example2

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

5. Connect replica set three servers (ports 27017, 27018, and 27019)

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

6. Connect replica set three servers, write operation applied on primary server and distribute query to slave server

Mongodb://host1,host2,host3/?slaveok=true

7, directly connected to the first server, either the replica set part or the primary server or from the server

Mongodb://host1,host2,host3/?connect=direct;slaveok=true

8, when your connection server has priority, also need to list all servers, you can use the above connection method

Safe Mode connection to localhost:

Mongodb://localhost/?safe=true

9. Connect to replica set in Safe mode, and wait for at least two replication servers to write successfully with a time-out setting of 2 seconds

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

Note: Original address http://www.studyofnet.com/news/337.html

MongoDB's Connection string

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.