MongoDB tutoria...login
MongoDB tutorial
author:php.cn  update time:2022-04-21 17:49:03

MongoDB connection


In this tutorial we will discuss the different connection methods of MongoDB.


Start the MongoDB service

In the previous tutorial, we have discussedhow to start the MongoDB service. You only need to go to the bin directory of the MongoDB installation directory Just execute 'mongod'.

After executing the startup operation, mongodb will not output any information after outputting some necessary information, and then waits for the connection to be established. When the connection is established, it will start printing log information.

You can use the MongoDB shell to connect to the MongoDB server. You can also use PHP to connect to MongoDB. In this tutorial we will use the MongoDB shell to connect to the Mongodb service. In subsequent chapters we will introduce how to connect to the MongoDB service through php.



Connecting to MongoDB service through shell

You can connect to MongoDB service by executing the following command.

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

mongodb://localhost

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

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

At this time, if you return to the window where you run the ./mongod command, you can see where you are connecting to the 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 command format

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

Use username and password to connect and log in to the default database:

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

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

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

Use user name and password to connect and log in to the specified database:

The format for connecting to the specified database is as follows:

mongodb://admin:123456@localhost/test


More connection examples

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

mongodb://localhost

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

mongodb://fred:foobar@localhost

Use username fred and password foobar to log in to the baz database of localhost.

mongodb://fred:foobar@localhost/baz

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

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

Connect replica set to three servers (ports 27017, 27018, and 27019):

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

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

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

Connect directly to the first server, whether it is 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 priority, you also need to list all Server, you can use the above connection method.

Connect to localhost in safe mode:

mongodb://localhost/?safe=true

Connect to replica set in safe mode and wait At least two replication servers write successfully and the timeout is set 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]]

The standard connection format contains multiple options (options), as shown below:

##slaveOk=true|falsesafe=true|falsew=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|falsejournal=true|falseIf set to true, synchronize to journal (write to entity before submitting to database Medium). Applies to safe=trueconnectTimeoutMS=msThe time when the connection can be opened. socketTimeoutMS=msThe time to send and receive sockets.
OptionsDescription
replicaSet=nameVerify the name of the replica set. Impliesconnect=replicaSet.
  • #true: In connect=direct mode, the driver will connect 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 search for the main server. In connect=replicaSet mode, the driver only connects to the main server, and all read and write commands are connected to Main server.

    • true: During update operation Afterwards, the driver will send the 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 that the update was successful.
  • true: The driver adds { fsync : true } to the getlasterror command. Applies to safe=true.

  • false: The driver will not be added to the getLastError command.