I have mongodb server on my system. For testing purpose i created different database using different port. E.g.
- dbname1 - SomepathIndrive -- Port
- dbname2 - SomepathIndrive -- Port
- dbname3 - SomepathIndrive -- Port
Now how do connect/start dbname2 or dbname 3 ?
When I use mongo command , it connects to default port and default database
asked Sep 21, 2017 at 6:52
3
As you said
mongo
command will connect to the server on the default port, if you want to connect to the server on different port use
mongo --port portnumber
When you are connected to mongo shell, command
show dbs
will result in list of all available databasses and to start using one of these use command
use databasename
Here's the link to the mongodb documentation about mongo Shell: //docs.mongodb.com/manual/mongo/
answered Sep 21, 2017 at 8:37
Docs Home → MongoDB Manual
connect[url, user, password]
Creates a connection
to a MongoDB instance and returns the reference to the database. However, in most cases, use the Mongo[]
object and its getDB[]
method instead.
| string | Specifies the connection string. You can specify either:
|
| string | Optional. Specifies an existing username with access privileges for this database. If |
| string | Optional unless the |
The following example instantiates a new connection to the MongoDB instance running on the localhost interface and returns a reference to myDatabase
:
db = connect["localhost:27017/myDatabase"]
Tip
Navigation
You were redirected from a different version of the documentation. Click here to go back.
- Create or Import a MongoDB Deployment >
- Connect to a MongoDB Process
Overview¶
To connect to a MongoDB, retrieve the hostname and port information from Cloud Manager and then use a MongoDB client, such as
mongosh
or a MongoDB driver, to connect. To connect to a cluster, retrieve the hostname and port for the mongos process.
To connect to a replica set or standalone process, retrieve the hostname and port for the mongod processes.
Firewall Rules¶
Firewall rules and user authentication affect your access to MongoDB. You must have access to the server and port of the MongoDB process. For information on firewalls on servers running MongoDB, see the firewall information in the Network Security document in the MongoDB manual.
If your MongoDB instance runs on Amazon Web Services [AWS], then the security group associated with the AWS servers also affects access. AWS security groups control inbound and outbound traffic to their associated servers.
Procedures¶
You can retrieve a shell command for connecting to your MongoDB instance, or you can retrieve the host and port number of a specific MongoDB process and create your own command for connecting using either shell or MongoDB driver.
Get a Shell Command to Connect to a MongoDB Instance¶
Navigate to the Clusters view for your deployment.¶
- If it is not already displayed, select the organization that contains your desired project from the office icon Organizations menu in the navigation bar.
- If it is not already displayed, select your desired project from the Projects menu in the navigation bar.
- If it is not already displayed, click Deployment in the sidebar.
- Click the Clusters view.
On the line listing the cluster, replica set, or process, click the ellipsis icon and select Connect to this instance.¶
Cloud Manager provides a mongo
command that you can use to connect to the MongoDB process.
Get the Host and Port for a MongoDB Process¶
Navigate to the Clusters view for your deployment.¶
- If it is not already displayed, select the organization that contains your desired project from the office icon Organizations menu in the navigation bar.
- If it is not already displayed, select your desired project from the Projects menu in the navigation bar.
- If it is not already displayed, click Deployment in the sidebar.
- Click the Clusters view.
[Optional] For sharded clusters, filter which process type is listed.¶
The four buttons are listed in the following order, left to right: Shards, Configs, Mongos, and BIs.
Shards | mongod processes that host your data. |
Configs | mongod processes that run as config servers to store a sharded cluster’s metadata. |
Mongos | mongos processes that route data in a sharded cluster. |
BIs | BI processes that access data in a sharded cluster. |
On the line listing the process, click Metrics.¶
Cloud Manager displays the hostname and port of the process at the top of the charts page.
Connect to a Deployment Using mongosh
¶
Get the host and port using the
previous procedure. From a shell, run mongosh
and specify the host and port. For example:
mongosh --username --password --host --port
Connect to a Deployment Using a MongoDB Driver¶
Get the host and port using the above procedure. See your driver’s instructions for creating a connection string that specifies the hostname and port.
For sharded clusters, you specify the hostname and port of the mongos instance. For a replica set, you specify a seed list of all hosts in the replica set. Your driver will automatically connect to the primary. For example:
mongodb://[:@]hostname0[,hostname1:][,hostname2:][...][,hostnameN:]