Latest web development tutorials

MongoDB connection

In this tutorial we will discuss the different connectors for MongoDB.


Start MongoDB service

In the previous tutorial, we have discussed how to start the MongoDB server service, you only need to perform 'mongod' can be in the bin directory MongoDB installation directory.

After the start operation, mongodb output after some necessary information will not output any information, then wait for the connection is established, when the connection is established, it will start printing log information.

You can use MongoDB MongoDB shell to connect to the server. You can also use PHP to connect to MongoDB. This tutorial we will use to connect to MongoDB shell Mongodb services later chapters we will explain how to connect to the MongoDB service via php.



MongoDB connection services through shell

You can connect MongoDB service by executing the following command.

Note: localhost is the host name, this option is required:

mongodb: // localhost

When you execute the above command, you can see the following output:

$ ./mongo
MongoDB shell version: 3.0.6
connecting to: test
> mongodb://localhostmongodb://localhost
... 

This time you check back run ./mongod command window, from where you can see a connection to MongoDB server, you can see the following information:

……省略信息……
2015-09-25T17:22:27.336+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2015-09-25T17:22:27.336+0800 I CONTROL  [initandlisten] options: { storage: { dbPath: "/data/db" } }
2015-09-25T17:22:27.350+0800 I NETWORK  [initandlisten] waiting for connections on port 27017
2015-09-25T17:22:36.012+0800 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:37310 #1 (1 connection now open)  # 该行表明一个来自本机的连接

……省略信息……


MongoDB connection Syntax

Using a user name and password to connect to the MongoDB server, you must use 'username: password @ hostname / dbname ' format, 'username' username, 'password' as a password.

User login name and password to connect to the default database:

$ ./mongo
MongoDB shell version: 3.0.6
connecting to: test
mongodb://admin:123456@localhost/

The above command, the user admin password 123456 to connect to the local MongoDB service. The output is as follows: <, p>

> mongodb://admin:123456@localhost/
... 

User login name and password to connect to the specified database:

Connect to the specified database in the following format:

mongodb: // admin: 123456 @ localhost / test


More examples of connections

Connect to the local database server, port is the default.

mongodb: // localhost

User name fred, login password foobar localhost the admin database.

mongodb: // fred: foobar @ localhost

User name fred, login password foobar localhost baz's database.

mongodb: // fred: foobar @ localhost / baz

Connection replica pair, server 1 example1.com server 2 example2.

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

Connecting replica set three servers (port 27017, 27018, and 27019):

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

Connecting replica set three servers, writes in the main application server and distributed query to the server.

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

Direct connection to the first server, whether it is part of the replica set or the main server or from the server.

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

When you connect to a server has priority, but also need to list all the servers, you can use the connection mode.

Safe Mode to connect to localhost:

mongodb:? // localhost / safe = true

Safe Mode is connected to the replica set, and wait at least two written successfully replicated servers, set the timeout to 2 seconds.

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


Parameter Option Description

standard format:

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

Standard connection format contains several options (options), as follows:

Options description
replicaSet = name Verify that the name of the replica set. Impliesconnect = replicaSet.
slaveOk = true | false
  • true: Under connect = direct mode, the drive will be connected to the first machine, even if it is not the primary server. Under connect = replicaSet mode, the driver will send all write requests to the primary and the other located in a read operation from the server.
  • false: Under connect = direct mode, the drive will automatically find the primary server at connect = replicaSet mode, the drive is only connected to the main server, and all read and write commands are connected to the main server.
safe = true | false
    • true: After the update operation, the drive will send getLastError command to ensure that the update is successful. (Also refer to wtimeoutMS).
false: After each update, the driver will not send getLastError to ensure a successful update.
w = n Drive add {w: n} command applies to getLastError safe = true..
wtimeoutMS = ms Drive Add {wtimeout: ms} command applies to getlasterror safe = true..
fsync = true | false
  • true: driving add {fsync: true}. getlasterror command applies to safe = true.
  • false: drive is not added to getLastError command.
journal = true | false If set to true, synchronized to the journal (in written to the database before submitting to the entity). Applies to safe = true
connectTimeoutMS = ms You can open the connection time.
socketTimeoutMS = ms Send and receive sockets of time.