Connection parameters of the client to MongoDB

Source: Internet
Author: User
Tags mongodb driver mongo shell

MongoDB is a database server: it runs on the frontend or backend and waits for the user's connection. When you start MongoDB, the following results are displayed:

~/$ ./mongod
## some logging output
#
Tue Mar  9 11:15:43 waiting for connections on port 27017
Tue Mar  9 11:15:43 web admin interface listening on port 28017

At this time, no logs are output, but it does not mean that the log output is stopped. MongoDB waits for the client to connect to port 27017. Logs are output as long as they are connected. You can use the MongoDB driver and Mongo Shell to connect to the database.

MongoDB connection does not support HTTP, and you cannot directly access MongoDB through a browser. Standard connected string format

Some drivers do not support the URI mode. Refer to the specific driver to see how to define the URI standard connection. If the driver does not support the connection, they will have an optional method to specify the connection.
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
  • Mongodb ://This is a fixed format and must be specified.
  • Username: password @This is optional. If it is set, the driver will attempt to log on to the database after connecting to the database server.
  • Host1This URI is unique. It specifies the address of the server to be connected.
  • : PortXOptional. If this parameter is not specified, the default value is 27017.
  • /DatabaseIs the name of the database to be connected. this parameter is associated with username: password .. If not specified, the default database is admin.
  • ? OptionsIs the connection parameter. Note that if the database is not filled in, the symbols "/" and "? ". The parameter format is name = value. If multiple separators are set to "&" or ";".

To connect to a replica set, specify multiple host addresses.

The parameters are as follows:

  • Connect = direct | replicaSet

    • Direct: The connection mode is single server. If multiple host addresses are provided, access them in sequence after the connection is established. If only one host is specified, direct is the default value.
    • ReplicaSet: As described, connect to the replica set. Host address list to find the replica set. If you connect 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 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 node and distributes read operations on other slave servers.
    • False: InIn connect = direct mode, the driver automatically searches for the master server.InConnect = replicaSetIn this mode, the driver only connects to the master server, and all the read/write commands are connected to the master server.
  • Safe = true | false
    • True: After the update operation is executed, the driver sends the getlasterror command to ensure the update is successful. (For more information, seeWtimeoutMS).
    • False: After each update, the driver does not send getlasterror to ensure the update is successful.
  • W = n
    • Add the {W: n} driver to the getlasterror command. It is applied to safe = true.
  • WtimeoutMS = MS
    • Driver add {wtimeout: MS} To getlasterror command. AppliedSafe = true.
  • Fsync = true | false
    • True: Add the {fsync: true} driver to the getlasterror command. applySafe = true.
    • False: The driver will not be added to the getlasterror command ..
  • MaxPoolSize = n
    • The connection pool is not allowed to grow beyond this size. when a thread requests a connection it either: Es one immediately from the connection pool if one is available, es a newly created connection if the pool has not reached the maximum size, or waits for a connection to be released by another thread (see waitQueueTimeoutMS and waitQueueMultiple ).
  • MinPoolSize = n
    • Some drivers will close useless connections. However, if the number of connections is lower than the minPoolSize value, they do not close idle connections. Note that the connection will be created as needed, so the minPoolSize will not take effect when the connection pool is pre-filled by many connections.
  • WaitQueueTimeoutMS = MS
    • The total time the thread waits for the connection to take effect before the timeout. If the connection pool reaches the maximum and all connections are in use, this parameter takes effect.
  • WaitQueueMultiple = n
    • The driver forcibly limits the number of threads waiting for connection at the same time. This limits the multiples of the connection pool.
  • Connecttimeoutms = MS
    • The time when the connection can be opened.
  • Sockettimeoutms = MS
    • The time when sockets is sent and accepted.

String connection is case insensitive.

Example

Connect to the local database server. The default port is used.

mongodb://localhost

Use the username fred and password foobar to log on to the admin database of localhost.

mongodb://fred:foobar@localhost

 

Use the username fred and password foobar to log on to the baz database of localhost.
mongodb://fred:foobar@localhost/baz

Connect to replica pair. Server 1 is example1.com Server 2 is example2.

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

Connect to the three servers of the replica set (ports 27017,270 18, and 27019 ):

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

Connect to the replica set three servers, write operation applications on the master server, and query the distribution to the slave server.

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

Directly connect to the first server, whether it is a part of the replica set or the master server or slave server.

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

When your connection server has a priority and you need to list all servers, you can use the above connection method.

Connect to localhost in safe mode:

mongodb://localhost/?safe=true

Connect to the replica set in safe mode and wait for at least two replication servers to write data. The timeout value is set to 2 seconds.

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


Each TCP connection server uses a thread. Therefore, it is strongly recommended that your application use the connection pool. Fortunately, most drivers are implemented. Note that the configuration of a new process must be created for each request, for example, CGI and PHP settings.

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.