Writing the connection string in MongoDB _mongodb

Source: Internet
Author: User
Tags mongodb mongodb connection string

Compared with traditional relational database, MongoDB database has the characteristics of simple operation, free and open source, which makes MongoDB products widely used in various large-scale portals and professional websites. Since the MongoDB connection does not support the HTTP protocol, all of you cannot access the MongoDB directly through the browser, the following is a detailed description of the writing of the connection string in MongoDB

First, MongoDB connection string Common format

Copy Code code as follows:

mongodb://[username:password@]host1[:p ort1][,host2[:p Ort2],... [, hostn[:p Ortn]]] [/[database][?options]]

Note: string concatenation is case-insensitive, not all MongoDB drivers support full connection strings, drivers that do not support this format connection string have alternate connection scenarios, refer to the documentation that drives themselves, and see how to define a URI standard connection.

1. Parameter description

mongodb://this is a fixed format that must be specified.
username:password@ option, 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 be filled in for this URI. It specifies the address to connect to the server. If you want to connect to a replica set, specify multiple host addresses.

:p ORTX Optional designated port, if not filled, the default is 27017
/database If you specify username:password@, connect and verify login to the specified database. If not specified, the admin database opens by default.

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

2. Options are connection parameters

Connect=direct|replicaset
Direct: The connection is a single server. If multiple host addresses are provided, they are accessed sequentially after the connection is established. If only one host is specified, direct is the default value.
Replicaset: Connect to replica set, as described. This host address list is to discover the replica set. If you are connecting to 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 computer. In Connect=replicaset mode, the driver sends all write requests to the master and distributes the read operations to the other from the server.
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: When the update operation is performed, 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
Driver Add {w:n} to GetLastError command. Applied to Safe=true.

Wtimeoutms=ms
Driver Add {wtimeout:ms} to GetLastError command. Applied to Safe=true.

Fsync=true|false
True: Driver add {fsync:true} to GetLastError command. Apply to Safe=true.
False: The driver is not added to the GetLastError command ...

Maxpoolsize=n
Minpoolsize=n
Some drivers will turn off the unused connections. However, if the number of connections is below the minpoolsize value, they do not turn off idle connections. Note that the connection is created as needed, so minpoolsize does not take effect when the connection pool is pre-populated with 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 drive forces the number of threads to wait for the 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

Second, MongoDB connection string instance

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

Copy Code code as follows:

Mongodb://localhost

2, the use of user name Fred, password foobar login localhost admin database.

Copy Code code as follows:

Mongodb://fred:foobar@localhost

3, the use of user name Fred, password foobar login localhost baz database

Copy Code code as follows:

Mongodb://fred:foobar@localhost/baz

4, connect replica pair, server 1 for example1.com server 2 for Example2

Copy Code code as follows:

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

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

Copy Code code as follows:

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

6, the connection replica set three servers, writes the operation to apply in the primary server and distributes the query to from the server

Copy Code code as follows:

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

7, directly connected to the first server, whether it is replica set part or the primary server or from the server

Copy Code code as follows:

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

8, when your connection server has priority, but also need to list all servers, you can use the above connection method
Safe mode connects to localhost:

Copy Code code as follows:

Mongodb://localhost/?safe=true

9. Connect to replica set in Safe mode, and wait for at least two replication servers to write successfully, timeout time set to 2 seconds

Copy Code code as follows:

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

The above mentioned is the entire content of this article, I hope you can enjoy.

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.