DescriptionIn this tutorial, we will discuss how to connect to a MongoDB Server using different options. Show
Start MongoDB ServerIn a previous tutorial, we have already discussed how to start a MongoDB Server, you have to execute 'mongod' from bin folder of your MongoDB installation folder. After you execute this, it stops printing any output after printing some necessary information and waits for a connection. As soon a connection is created, it starts printing log information. You can use MongoDB Shell to connect to a MongoDB Server. You can use any supported programming language like PHP to connect to also. But for now we will see how to create a connection using shell and discuss how to do that using PHP in an upcoming tutorial. By default, MongoDB starts at port 27017. But you can access it in a web browser not at that port, rather, at a port number 1000 more than the port at which MongoDB is started. So if you point your browser to http://localhost:28017, you can see MongoDB web interface. connection to MongoDB Server from shellYou have to execute the following command (in it's most simple form, without any other options used) to connect to a MongoDB Server. Remember that localhost is the hostname and this option is required.
You see the output as shown below, as soon as you execute the command above. If you check the command prompt window from where you started MongoDB Server, you can see as follows. The last line(marked), is printed when you have successfully connected to a MongoDB Server. Format of MongoDB connect commandConnect and login with username and password to default databaseTo connect to a MongoDB Server using username and password, you have to use '[email protected]/dbname'. Where username is the username, password is the password for that user.
With the above command, 'mongo_admin' user with a password of 'AxB6_w3r' is connected to default database at localhost. Output of the above command is as follows : Connect and login with username and password to a particular databaseTo connect to a MongoDB Server using username and password, you have to use '[email protected]/dbname'. Where username is the username, password is the password for that user and dbname is the database to which you want to connect to.
With the above command, 'mongo_admin' user with a password of 'AxB6_w3r' is connected to 'w3r' database at localhost. Output of the above command is as follows : Note : You can use multiple hostname to connect to with a single command. Connect and login with username and password to a particular database at a specific portTo connect to a MongoDB Server using username and password, you have to use '[email protected]/dbname'. Where username is the username, password is the password for that user, dbname is the database to which you want to connect to and optionally you can specify a port number at which you want to connect to.
With the above command, 'mongo_admin' user with a password of 'AxB6_w3r' is connected to 'w3r' database at localhost at port 29000. If you don't mention the port number, it is connect at port 27107. Output of the above command is as follows : To Connect to multiple MongoDB servers running on different hostnamesTo Connect to multiple MongoDB servers running on different hostnames, use the following commands. This kind of situation can arise when 'replica sets' are used. A replica set comprises of two or more nodes that are copies of each other. A Replica set can automatically detect one master node. This mechanism is good to ensure that when a server fails, recovery is possible.
To Connect to multiple MongoDB servers running on same hostnames but on different portsTo Connect to multiple MongoDB servers running on same hostnames but on different ports, use the following command. This is also used for replica sets.
OptionsThere are several options which can be used along with standards format discussed above. The following table describes those :
Previous: Databases, Documents, Collections Can we access MongoDB on browser?Overview. The data browser allows access to your MongoDB databases, collections, and documents from inside the Compose UI. It also provides data migration, user management, and other administrative tools.
How can I access MongoDB from my website?By default, MongoDB starts at port 27017. But you can access it in a web browser not at that port, rather, at a port number 1000 more than the port at which MongoDB is started. So if you point your browser to http://localhost:28017, you can see MongoDB web interface.
How do I access MongoDB database?To open up the MongoDB shell, run the mongo command from your server prompt. By default, the mongo command opens a shell connected to a locally-installed MongoDB instance running on port 27017 . Try running the mongo command with no additional parameters: mongo.
How do I access MongoDB on localhost?To connect to your local MongoDB, you set Hostname to localhost and Port to 27017 . These values are the default for all local MongoDB connections (unless you changed them). Press connect, and you should see the databases in your local MongoDB.
|