Updated March 8, 2023
Introduction to MongoDB Connection String
MongoDB connection string is defined as connection format to join the MongoDB database server, we have using username, hostname, password, and port parameter to connect the database server. Without a connection string, we cannot connect to the database server, we need a connection string to connect the database server. Using “mongo” and “mongodb” commands we have to connect to our database server, with “mongodb” command we need to define database connection string details. Standard and complex are the types of connection strings in MongoDB, standard connection string uses the list of libraries and drivers to connect the MongoDB database server.
Syntax of MongoDB Connection String:
Given below is the syntax mentioned:
mongodb:// [ name_of user: password_of_database_user] host_name1 [port_no_of_host1], host_name2 [port_no_of_host2],... host_nameN [port_no_of_hostN] [/] database_name [?options]
Parameter Explanation:
- Mongodb: This is the command which was used to connect the MongoDB database server. We can also connect the database server by using the “mongo” command. If we want to connect the local database server then we have to use the command as mongo.
- Name of user: This is defined as the username which was used to connect the database server. We need to specify the correct username while login in to the database server.
- Password of database user: This is defined as the password which was used with username to connect the database server. We need to specify the correct password while login into the database server. If suppose we have given the wrong password in the connection string it will issue an authentication error.
- Hostname: This is defined as the hostname of the database server. We can also use the IP of the database server instead of using the hostname. The hostname is an important parameter while connecting to the MongoDB database server.
- Port no: This is defined as port no which was we have defined to our database server. The default port of the MongoDB database server is 27017. We can also change the default port no to other for security reason.
- Database name: At the time connecting to the MongoDB database server we need to define the database name which was we have connected. For connecting to the specified database we need to have specified privileges. The database name is also an important parameter while connecting to the MongoDB database server.
- Other options: We can also use other options with connection parameters like replica set name and connection timeout value, we need to give connection timeout in milliseconds. This is an optional parameter while using connection string in MongoDB.
How does Connection String work in MongoDB?
It will contain the below string parameters while connecting to the database server.
- mongodb, mongo, and mongosh command
- Name of user
- Password
- Hostname or IP address
- Port no of database server
- Database name
- Replica set name
- Connection timeout value
At the time of connecting to the MongoDB database server first, it will check our username and password. If the username and password authentication are successful it will give access to the specified database. If suppose username or password is incorrect it will issue the error as authentication failed.
The below example shows that we need the correct username and password to connect the MongoDB database server.
In the below example we have used username as “dbuser” and used incorrect password so it will give an error, we will not connect to the database server. Also we have used hostname as “cluster0.xttyr.mongodb.net” and used command as “mongosh” to connect the database server.
Code:
# mongosh "mongodb+srv://cluster0.xttyr.mongodb.net/myFirstDatabase" --username dbuser
Output:
We need the correct username and password to connect to the MongoDB database server.
We can also use the replica set option with our connection string in MongoDB. This option basically specifies the replica set of our master database using one of them. SSL is also the connection string option which was we have used in it. As the name specifies SSL it is used whether our database connection string will establish the SSL or not.
There is multiple connection string formats available in the MongoDB database server which are as follows.
- Standard format.
- DNS seed list format.
- Complex connection string format.
We can also connect the MongoDB database server locally by using a local address. We need to use the hostname as localhost or IP address as 127.0.0.1 to connect the local database server. We can also connect the MongoDB atlas cluster using the mongo shell using a connection string.
Examples of MongoDB Connection String
Given below are the examples mentioned:
Example #1
Connect to the local MongoDB database server by using the mongo command.
- Below example shows that connect to the local MongoDB database server by using the mongo command.
- We can see that in the first example we have used hostname as localhost to connect the database server. And in the second example we have used the IP address 127.0.0.1 to connect the database server.
Code:
# mongo --host localhost
# mongo --host 127.0.0.1
Output:
Example #2
Connect to the atlas database cluster using MongoDB compass.
- Below example shows that connect to the atlas database cluster using MongoDB compass.
- We have used username as “dbuser” and password as MongoDB. Also using this connection string we have used hostname as “cluster0.xttyr.mongodb.net”.
Code:
mongodb+srv://dbuser (username) :mongodb (password) @cluster0.xttyr.mongodb.net (hostname) /test (database name)
Output:
Example #3
Connect to the atlas database cluster using MongoDB shell.
- Below example shows that connect to the atlas database cluster using MongoDB shell.
- We have used username as “dbuser” and password as mongodb. Also in this connection string we have used hostname as “cluster0.xttyr.mongodb.net”.
- In the below example, we have connected to the MongoDB database server using mongosh command.
Code:
# mongosh "mongodb+srv://cluster0.xttyr.mongodb.net (Host name) /test (database name)" --username dbuser
Output:
Example #4
Connect to the database server without connecting any database.
- Below example shows that connect to the database server without connecting any database.
- At starting time we have not connected any database.
Code:
# mongo --nodb
Output:
Conclusion
It is a combination of different strings like username, password, hostname, port number, and database name. Standard connection string, complex connection string, and DNS seed connection string are the format of connection string available in MongoDB. Basically, it is used to connect the database server.
Recommended Articles
This is a guide to MongoDB Connection String. Here we discuss the introduction, how does connection string works in MongoDB? and examples. You may also have a look at the following articles to learn more –