MongoDB-instance connection instructions
Connect to MongoDB through shell
You can connect to the MongoDB service by executing the following command.
Note: localhost is the host name. This option is required:
Mongodb: // localhost
When you execute the preceding command, you can see the following output:
$ ./mongoMongoDB shell version: 3.0.6connecting to: test> mongodb://localhostmongodb://localhost...
At this time, you will return to the window for running the./mongod command. You can see where to connect to the MongoDB server. You can see the following information:
...... Omitting information ...... 2015-09-25T17: 22: 27.336 + 0800 I control [initandlisten] allocator: tcmalloc2015-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: 36.012 + 0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1: 37310 #1 (1 connection now open) # This row indicates a local Connection ...... Omitting information ......
MongoDB connection Command Format
To connect to the MongoDB server using the username and password, you must use the 'username: password @ hostname/dbname' format. 'username' is the username and 'Password' is the password.
Use the username and password to connect to the default database:
$ ./mongoMongoDB shell version: 3.0.6connecting to: testmongodb://admin:123456@localhost/
In the preceding command, the user admin uses the password 123456 to connect to the local MongoDB service. The output result is as follows: <, p>
> mongodb://admin:123456@localhost/...
Use the username and password to connect to the specified database:
The format for connecting to the specified database is as follows:
Mongodb: // admin: 123456 @ localhost/test
More connected instances
Connect to the local database server. The default port is used.
Mongodb: // localhost
Use the username fred and password foobar to log on to the admin database of localhost.
Mongodb: // fred: foobar @ localhost
Use the username fred and password foobar to log on to the baz database of localhost.
Mongodb: // fred: foobar @ localhost/baz
Connect to replica pair. Server 1 is example1.com Server 2 is example2.
Mongodb: // example1.com: 27017, example2.com: 27017
Connect to the three servers of the replica set (ports 27017,270 18, and 27019 ):
Mongodb: // localhost, localhost: 27018, localhost: 27019
Connect to the replica set three servers, write operation applications on the master server, and query the distribution to the slave server.
Mongodb: // host1, host2, host3 /? SlaveOk = true
Directly connect to the first server, whether it is a part of the replica set or the master server or slave server.
Mongodb: // host1, host2, host3 /? Connect = direct; slaveOk = true
When your connection server has a priority and you need to list all servers, you can use the above connection method.
Connect to localhost in safe mode:
Mongodb: // localhost /? Safe = true
Connect to the replica set in safe mode and wait for at least two replication servers to write data. The timeout value is set to 2 seconds.
Mongodb: // host1, host2, host3 /? Safe = true; w = 2; wtimeoutMS = 2000
Parameter options
Standard Format:
Mongodb: // [username: password @] host1 [: port1] [, host2 [: port2],... [, hostN [: portN] [/[database] [? Options]
The standard connection format contains multiple options, as shown below:
Option |
Description |
ReplicaSet = name |
Verify the name of the replica 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 master. In connect = replicaSet mode, the driver sends all write requests to the master node and distributes read operations on other slave servers.
- False: In connect = direct mode, the driver automatically searches for the master server. In connect = replicaSet mode, the driver only connects to the master server, and all the read/write commands are connected to the master server.
|
Safe = true | false |
- True: After the update operation is executed, the driver sends the getLastError command to ensure the update is successful. (Also refer to wtimeoutMS ).
False: after each update, the driver does not send getLastError to ensure the update is successful. |
W = n |
Add the {w: n} driver to the getLastError command. It is applied to safe = true. |
WtimeoutMS = MS |
Driver add {wtimeout: MS} To getlasterror command. Applied to safe = true. |
Fsync = true | false |
- True: add the {fsync: true} driver to the getlasterror command. It is applied to safe = true.
- False: the driver is not added to the getLastError command.
|
Journal = true | false |
If this parameter is set to true, It is synchronized to journal (written to the entity before being committed to the database). It is applied to safe = true. |
ConnectTimeoutMS = MS |
The time when the connection can be opened. |
SocketTimeoutMS = MS |
The time when sockets is sent and accepted. |
For more MongoDB tutorials, see the following:
CentOS compilation and installation of php extensions for MongoDB and mongoDB
CentOS 6 install MongoDB and server configuration using yum
Install MongoDB2.4.3 in Ubuntu 13.04
MongoDB beginners must read (both concepts and practices)
MongoDB Installation Guide for Ubunu 14.04
MongoDB authoritative Guide (The Definitive Guide) in English [PDF]
Nagios monitoring MongoDB sharded cluster service practice
Build MongoDB Service Based on CentOS 6.5 Operating System
MongoDB details: click here
MongoDB: click here
This article permanently updates the link address: