Mongodb Guide (translation) (5)-developer zone-connection

Source: Internet
Author: User
Tags mongodb driver mongo shell

Mongodb is a database server that runs on the frontend or backend and waits for a connection from a user. When you start mongodb, you will see something similar to the following:

~/$ ./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, it stops printing output but does not freeze it. It only listens to port 27017 waiting for connection. Once you connect and start sending the command, it will continue to output the log of the task. You can use any mongodb driver or mongo shell to connect to the database.

You cannot connect to mongodb by entering http: // localhost: 27017 in a web browser. The database cannot be accessed over port 27017 using HTTP.

 

Standard connection string format

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
  • MongoDB: // a required prefix to indicate a string in the standard connection format.
  • Username: password @ optional. If yes, the driver tries to log on to a database after connecting to the database server.
  • The required part of the host1 URL. It indicates the address of the server to be connected.
  • : Portx is optional. If the default connection is not provided: 27017.
  • /Database: the name of the database to be logged on. It is only useful after the username: password @ syntax is used. If not, "admin" is used by default ".
  • ? Options is a connection option. Note: If the database is not provided? A slash "/" is still required in the middle of the symbol "/". Possible values are pairs of name = values, which are separated by "&" or.

Many hosts may need to be specified (connect to the replication peer/group ).

These options are:

Copy group:

  • Replicaset = Name

    • The driver checks whether the connected replication group matches the name. This means that if the host is a seed list, the driver will try to find all the members in the replication group.

Single Server:

  • Slaveok = true | false

Under any Configuration:

  • Safe = true | false

    • True: After the driver executes the update operation, it sends a getLastError command to ensure the update is successful (check w and wtimeoutMS at the same time ).
    • False: the driver does not send the getLastError command after each update operation.
  • W = N
    • Add {w: n} to the getLastError command }. It means safe = true.
  • Wtimeoutms = MS
    • Add {wtimeout: ms} to the getLastError command }. It means safe = true.
  • Fsync = true | false
    • True: The driver adds {fsync: true} to the getLastError command }. It means safe = true.
    • False: the driver does not add synchronization parameters in the getLastError command.
  • Journal = true | false
    • Synchronize to log files. Meaning fase = true.
  • Connecttimeoutms = MS
    • The time that a connection can be opened before timeout.
  • Sockettimeoutms = MS
    • The timeout time for sending and receiving operations on the socket.

These options are case-insensitive.

Examples

Connect to the database server that runs on the default listening port of the local machine:

mongodb://localhost

Use the username "fred" and password "foobar" to connect and log on to the management database:

mongodb://fred:foobar@localhost

Use the username "fred" and password "foobar" to connect and log on to the "baz" database:

mongodb://fred:foobar@localhost/baz

Connect to a replication pairing, where one server is in example1.com and the other server is in example2.com:

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

Connect to a replication group, and the three servers run locally (ports: 27019 ):

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

Connect to a replication group composed of 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 local machine in safe mode:

mongodb://localhost/?safe=true

Connect to the replication group in safe mode and wait for the replication operation to be completed on at least two machines. 2 seconds of Timeout:

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

 

Connection Pool
The server will start a thread for each TCP connection, and it is strongly recommended that your application use a certain type of connection pool. Fortunately, most drivers have implemented this function for you. When your application starts a process for each request, the driver throws an important exception, for example, in CGI and some PHP configurations.

      

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.