The writing of connection strings in MongoDB

  • 2020-05-24 06:24:40
  • OfStack

Compared with the traditional relational database, MongoDB database has the characteristics of simple operation, complete free, open source, etc., which makes MongoDB products widely used in various large portal websites and professional websites. Since the MongoDB connection does not support the HTTP protocol, you cannot access MongoDB directly through the browser

1. Common format for MongoDB connection strings


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

Note: string concatenation is not case sensitive, not all MongoDB drivers support complete concatenation string, do not support this format of concatenation string driver will have an alternative connection scheme, please refer to the driver's own documentation, see how to define uri standard connection.

1. Parameter description

mongodb:// this is a fixed format and must be specified.
The & # 61548; username:password@ optional, if set, after connecting to the database server, the driver will try to log into the database. host1 must specify at least 1 host
host1 is the only one to be filled in this URI. It specifies the address to connect to the server. If you want to connect to a replica set, specify multiple host addresses.

The & # 61548; :portX optional specified port, if not filled, default is 27017
The & # 61548; If you specify username:password@, connect and verify login to the specified database. If not specified, the admin database is opened by default.

The & # 61548; The & # 63; options is the connection option. If you don't use /database, you need to add/before it. All connection options are key-value pairs name=value, key-value pairs pass between & Or; (semicolon) separated

2. options is the connection parameter

connect=direct|replicaSet
direct: connect to a single server. If multiple host addresses are provided, access them sequentially after establishing the connection. If only one host is specified, direct is the default.
replicaSet: as described, connect to replica set. This host address list is to discover replica set. replicaSet is the default if multiple hosts are connected.

replicaSet=name
Verify the name of replica set. Impliesconnect = replicaSet.

slaveOk=true|false
true: in connect=direct mode, the driver connects to the first machine, even if the server is not the master. In connect=replicaSet mode, the driver sends all write requests to the master and distributes read operations to 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 the update is successful. (see also wtimeoutMS).
false: after each update, the driver does not send getLastError to ensure the update is successful.

w=n
Driver adds {w: n} to getLastError command. Applies to safe=true.

wtimeoutMS=ms
Driver adds {wtimeout: ms} to getlasterror command. Applies to safe=true.

fsync=true|false
true: drivers add {fsync: true} to the getlasterror command. Applies to safe=true.
false: drivers are not added to the getLastError command. .

maxPoolSize=n
minPoolSize=n
Some drivers will close unwanted connections. However, if the number of connections is below the minPoolSize value, they do not close idle connections. Note that connections are created as needed, so minPoolSize does not take effect when the connection pool is prefilled with many connections.

waitQueueTimeoutMS=ms
The total time the thread waited for the connection to take effect before the timeout. This parameter takes effect if the connection pool reaches a maximum and all connections are in use.

waitQueueMultiple=n
The driver forcibly limits the number of threads waiting for connections at the same time. This limits the multiple of the connection pool.

connectTimeoutMS=ms
Can open the connection time.

socketTimeoutMS=ms
Time to send and receive sockets

2. Connection string instance of MongoDB

1. Connect to the local database server on the default port.


 mongodb://localhost

2. Use the user name fred and password foobar to log in localhost's admin database.


mongodb://fred:foobar@localhost

3. Use the user name fred and password foobar to log in localhost's baz database


mongodb://fred:foobar@localhost/baz

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


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

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


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

6. Connect three servers of replica set, write operation is applied in the master server and distributed to the slave server


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

7. Directly connect to the first server, either part of replica set1 or the master server or the slave server


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

8. When your connection server has priority and needs to list all servers, you can use the above connection method
Secure mode connection to localhost:


mongodb://localhost/?safe=true

9. Connect to replica set in secure mode and wait for at least two replication servers to write successfully. The timeout is set to 2 seconds


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

That's all for this article, I hope you enjoy it.


Related articles: