MongoDB Connections Tutorial from Coding compiler. In this MongoDB tutorial, we will discuss the different ways in which MongoDB can be connected. Let’s start learning MongoDB.
MongoDB Connections – Start the MongoDB service
In the previous tutorial, we have discussed how to start the MongoDB service. You only need to execute MongoDB in the bin directory of the MongoDB installation directory.
After performing the startup operation, MongoDB will not output any information after outputting some necessary information, and then wait for the connection to be established. When the connection is established, it will start to print the 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 the following sections, we will show you how to connect to the MongoDB service via PHP.
Standard URI connection syntax:
Mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
- Mongodb:// This is a fixed format and must be specified.
- Username:password@ Optional, if set, the driver will try to log in to the database after connecting to the database server.
- Host1 must specify at least one host, and host1 is the only one to be filled in. It specifies the address to connect to the server. If you are connecting to a replication set, specify multiple host addresses.
- portX optional designated port, if not filled, the default is 27017
- /database If username:password@ is specified, connect and verify login to the specified database. If not specified, the test database is opened by default.
- ?options are connection options. If you do not use /database, you need to add / before. All connection options are key-value pairs name=value, separated by a &/; (semicolon)
The standard connection format contains several options, as shown below:
Option | description |
---|---|
replicaSet=name | Verify the name of the replica set. Impliesconnect=replicaSet. |
slaveOk=true|false |
|
Safe=true|false |
False: After each update, the driver does not send a getLastError to ensure the update is successful. |
w=n | The driver adds { w : n } to the getLastError command. Applies to safe=true. |
wtimeoutMS=ms | The driver adds { wtimeout : ms } to the getlasterror command. Applies to safe=true. |
Fsync=true|false |
|
Journal=true|false | If set to true, sync to journal (written to the entity before committing to the database). Applies to safe=true |
connectTimeoutMS=ms | The time at which the connection can be opened. |
socketTimeoutMS=ms | The time to send and receive sockets. |
Instance
Use the default port to connect to MongoDB’s services.
Mongodb://localhost: //localhost
Connect to the MongoDB service through the shell:
$ ./mongo./ mongo MongoDB shell version: 3.0.6MongoDB shell version : 3.0 . 6 Connecting to: test: test ... ...
At this point, you return to the window where the ./mongod command is run. You can see where the server is connected to MongoDB. You can see the following information:
... omit the information... 2015-09-25T17:22:27.336+0800 I CONTROL [initandlisten] allocator: tcmalloc2015 - 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.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 270172015 - 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) # This line indicates a connection from this machine2015 - 09 - 25T17 : 22 : 36.012 + 0800 I NETWORK [ initandlisten ] connection accepted from 127.0 . 0.1 : 37310 #1 (1 connection now open) # This line indicates a connection from this machine ... omit the information...... omit the information...
MongoDB connection command format
To connect to the MongoDB server with a username and password, you must use the ‘ username:password@hostname/dbname ‘ format, ‘username’ for the username and ‘password’ for the password.
Log in to the default database using a username and password:
$ ./mongo./ mongo MongoDB shell version: 3.0.6MongoDB shell version : 3.0 . 6 Connecting to: test: test
Use the user admin to connect to the local MongoDB service with password 123456. The output is as follows:
> mongodb://admin:123456@localhost/Mongodb : //admin:123456@localhost/ ... ...
Log in to the specified database using a username and password. The format is as follows:
Mongodb://admin:123456@localhost/test: //admin:123456@localhost/test
More connection examples
Connect to the local database server, the port is the default.
Mongodb://localhost: //localhost
Use the username fred, password foobar to log in to the localhost admin database.
Mongodb://fred:foobar@localhost: //fred:foobar@localhost
Use the username fred and the password foobar to log in to the localhost’s baz database.
Mongodb://fred:foobar@localhost/baz: //fred:foobar@localhost/baz
Connect to the replica pair, server 1 is example1.com server 2 is example2.
Mongodb://example1.com:27017,example2.com:27017: //example1.com:27017,example2.com:27017
Connect the replica set to three servers (ports 27017, 27018, and 27019):
Mongodb://localhost,localhost:27018,localhost:27019: //localhost,localhost:27018,localhost:27019
Connect the replica set to three servers, write the application to the primary server and distribute the query to the secondary server.
Mongodb://host1,host2,host3/?slaveOk=true: //host1,host2,host3/?slaveOk=true
Connect directly to the first server, either part of the replica set or the primary or secondary server.
Mongodb://host1,host2,host3/?connect=direct;slaveOk=true: //host1,host2,host3/?connect=direct;slaveOk=true
When your connection server has priority and you need to list all the servers, you can use the above connection method.
Connect to localhost in safe mode:
Mongodb://localhost/?safe=true: //localhost/?safe=true
Connect to the replica set in secure mode and wait for at least two replication servers to successfully write, setting the timeout to 2 seconds.
Mongodb://host1,host2,host3/?safe=true;w=2;wtimeoutMS=2000: //host1,host2,host3/?safe=true;w=2;wtimeoutMS=2000
Related MongoDB Tutorials For Beginners
What is MongoDB Tutorial For Beginners
MongoDB Data Modelling Tutorial