MongoDB driver connection to MongoDB database details

Source: Internet
Author: User
Tags failover mongodb driver

Reprinted: View Original

Learn about MongoDB
All the children's shoes in replica set know that they haveFailoverThe intelligent failover feature, how can this feature be implemented in programming languages (C/PHP/Python/Java, etc.) and connected to the database with a driver?How do I know which server is available?The answer is in the following content.

The URI styles listed below are not supported by all database drivers. For more information, see the document of each driver. If the following style is not supported, the driver will have its own replacement method for the specified connection.
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
•   mongodb:// Is a required prefix that identifies the current string as a standard connection format.
• {Username: password @} is optional. After the user name and password are given, the driver will try to log on after connecting to the database.
• The unique required parameter in the host1 Uri, the connection address of the database.
•: Portx is optional. By default, the system connects to port 27017.
•/Database is the name of the database to be connected. This value is valid only when Username: password @ is provided. If this parameter is not specified, the connection is established by default."admin"Database.
Multiple databases and ports can be specified at will, which is used to connect to replica pairs/sets.
Optional options:
•   connect=direct|replicaset

O direct: directly establish a connection to the server. If multiple hosts are specified, the connection will be established one by one until the connection is established successfully. If only one host is specified, direct is the default value.
O replicaset: Use creplicaset Semantics establishes a connection (even if only one host is provided ). The specified host is used as the seed list to find the complete replica.set. Replicaset is the default value when multiple hosts are specified.
•   replicaset=name

O Driver verification for establishing the connection replicaset. Implicit connect = replicaset.
•   slaveok=true|falseo   true: For connect = direct mode, the driver establishes a connection to the first server in the list, even if it is not the master server. In connect = replicaset mode, the driver sends all write operations to the master node, and forwards all read operations to
Round Robin is sequentially distributed to slave nodes.

o   false: In connect = direct mode, the driver tries all hosts in order until the master node is found. In connect = replicaset mode, the driver connects only to the master node and sends all read and write operations to the master node.
•   safe=true|false

o   true: The driver sends the getlasterror command after each update operation to ensure the update is successful (see w and wtimeout ).
o   false: The driver does not send the getlasterror command after each update operation.
•   w=nThe O Driver sends the {W: n} command to getlasterror. Implicit safe =true
•   wtimeout=ms

The O driver adds the {wtimeout: MS} parameter to getlasterror. Implicit safe =true
•   fsync=true|false

o   true: The driver adds {fsync to getlasterror:true } Parameter. Implicit safe =true
o   false: The fsync parameter is not added to the driver.
Connection example
Connect mongodbmongodb: // localhost running on the local default port (27017) to a MongoDB running on the local default port (27017, log On with the username "Fred" and password "foobar". After logon, connect the default admin database MongoDB: // FRED: foobar @ localhost to a default port (27017) running on the local machine) and use the username "Fred" and password "foobar" to log on to apsaradb for MongoDB: // FRED: foobar @ localhost/Baz, one server is located at example1.com, the other is located at example2.commongodb: // example1.com: 27017, and example2.com: 27017 is connected to a local replica set (ports are 27019, and respectively) mongod. B: // localhost, localhost: 27018, localhost: 27019 connect to the replica set of the three servers, send all write operations to the master node, and distribute all read operations to the slave node MongoDB: // host1, host2, host3 /? Slaveok = true connect to the first server and respond, whether it is a replica set, whether it is the master node or slave node MongoDB: // host1, host2, host3 /? Connect = direct; slaveok = true note: This type of connection string can be used when you prefer to use a server but there is a server that can be replaced. Connect to the local MongoDB in safe mode: // localhost /? Safe = true connect to the replica set in safe mode and wait for the backup to be completed on at least two machines. The timeout time is 2 seconds for MongoDB: // host1, host2, host3 /? Safe = true; W = 2; wtimeout = 2000


Note: Connection Pool

Each TCP connection corresponds to a database thread. Therefore, we strongly recommend that you use some form of connection pool technology. Fortunately, most official database drivers have built in to implement this function. However, if you use CGI or PHP, each new request may start a new application process. In this case, pay special attention to connection processing, the latest PHP driver submitted on GitHub says it has been optimized in the connection pool. You can download and compile it by yourself.

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.