Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

MongoDB connection


May 17, 2021 MongoDB


Table of contents


MongoDB - Connect


Describe

In this tutorial we'll discuss the different ways MongoDB connects.


Start the MongoDB service

In the previous tutorial, we discussed how to start MongoDB services, you only need to perform 'mongod' in the bin directory of the MongoDB installation directory.

After the startup operation, mongodb outputs some necessary information without outputing any information, then waits for the connection to be established, and when the connection is established, the log information is printed.

You can use the MongoDB shell to connect to the MongoDB server. Y ou can also use PHP to connect to mongodb. In this tutorial we'll use mongoDB shell to connect to the Mongodb service, and in later chapters we'll show you how to connect to the MongoDB service via php.

By default, the start port for MongoDB is 27017. The port 1000 longer than the MongoDB boot port is The MongoDB's web user interface, which you can http://localhost:28017 into the browser to access MongoDB's web user interface.



Connect to the MongoDB service via shell

You can connect to MongoDB's services by executing the following commands.

Note: Localhost is the host name, and this option is required:

mongodb://localhost

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

MongoDB connection

If you check where to connect to MongoDB's servers, you can see the following information:

MongoDB connection The last line (marked) prints information about your successful connection to the MongoDB service.

MongoDB connection command format

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

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

mongodb://mongo_admin:AxB6_w3r@localhost/

In the above commands, mongo_admin can use AxB6_w3r to connect to the local MongoDB service. T he output results are as follows: MongoDB connection

Use the username and password connection to log in to the specified database:

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

mongodb://mongo_admin:AxB6_w3r@localhost/w3r


More connection instances

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

mongodb://localhost

Using the username fred, password foobar logs on to the admin database of localhost.

mongodb://fred:foobar@localhost

Using the username fred, password foobar logs on 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 the replica set three servers (ports 27017, 27018, and 27019):

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

Connecting the replica set three servers, the write operation is applied on the primary server and the query is distributed from the server.

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

Connect directly to the first server, whether it's part of the reprica set or the primary server or from the server.

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

When your connection servers have priority and you also need to list all the servers, you can use the connection methods above.

Safe mode is connected to localhost:

mongodb://localhost/?safe=true

Connect to repica set in safe mode and wait for at least two replication servers to write successfully, with the timeout set to 2 seconds.

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


Description of the parameter options

Standard format:

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

The standard connection format includes several options, as follows:

Options Describe
replicaSet=name Verify the name of the reprica 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 primary. In connect-replicaSet mode, the driver sends all write requests to the main and distributes the read operations across the other from the server.
  • false: The driver will automatically find the primary server in connect- and direct mode. In connect-replicaSet mode, the driver connects only to the primary server, and all read and write commands are connected to the primary server.
safe=true|false
    • TRUE: After performing an update, the driver sends a getLastError command to ensure that the update is successful.(Also refer to WTIMEOUTMS).
FALSE: After each update, the driver does not send GetLastError to ensure that the update is successful.
w=n Drive Add {W: N} to getLastError command. Apply to Safe = True.
wtimeoutMS=ms Drive Add {WTIMEOUT: MS} to getLastError command. Apply to Safe = true.
fsync=true|false
  • True: Drive Add {fsync: true} to getLastError command. Apply to Safe = true.
  • false: The driver is not added to the getLastError command.
journal=true|false If wie true is set, sync to journal (write to the entity before submitting to the database). Applied to safe-true
connectTimeoutMS=ms The time at which the connection can be opened.
socketTimeoutMS=ms Time to send and receive sockets.