MongoDB is a database server that can run in the previous or background mode while waiting for client connection. After you start MongoDB, you can see the following output:
~/$ ./mongod # # some logging output # Tue Mar 9 11:15:43 waiting for connections on port 27017 Tue Mar 9 11:15:43 web admin interface listening on port 28017
The output will stop here and wait for the client to connect to port 27017. Once you connect to the database and start sending commands, it will continue to output the ongoing operations. You can use any MongoDB driver or Mongo
Shell to connect to the database.
But you cannot access port 27017 http: // localhost: 27017 through a browser. The database does not accept HTTP access to this port.
Standard connection string format
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
- MongoDB ://Is a required prefix that identifies the current string as a standard connection format.
- {Username: password @} is optional. After the user name and password are given, the driver will try to log on after connecting to the database.
- Host1The unique required parameter in Uri, the connection address of the database.
- : PortxOptional. By default, the system connects to port 27017.
- /DatabaseIs the name of the database to be connected, only whenUsername: password @This value is valid. If this parameter is not specified, the system will connect to the "admin" database by default.
Multiple databases and ports can be specified at will, which is used to connect to replica pairs/sets.
Options include:
- Connect = direct | replicaset
- Direct: Directly establish a connection to the server. If multiple hosts are specified, the connection will be established one by one until the connection is established successfully. If only one host is specifiedDirectIs the default value.
- Replicaset: Use creplica set semantics to establish a connection (even if only one host is provided ). The specified host is used as the seed list to find the complete replica set. When multiple hosts are specifiedReplicasetIs the default value.
- Replicaset = Name
- The driver verifies the name of the replica set that establishes the connection. ImplicitConnect = replicaset.
- Slaveok = true | false
- True:Connect = directMode, the driver establishes a connection to the first server in the list, even if it is not the master server. PairConnect = replicasetMode, the driver sends all write operations to the master node, and all read operations are
Round Robin is sequentially distributed to slave nodes.
- False: PairConnect = directMode. The driver tries all hosts in order until the master node is found. PairConnect = replicasetMode. The driver only connects to the master node and sends all read and write operations to the master node.
- Safe = true | false
- True: The driver sends the getlasterror command after each update operation to ensure that the update is successful (referWAndWtimeout).
- False: The driver does not send the getlasterror command after each update operation.
- W = N
- The driver sends the {W: n} command to getlasterror. ImplicitSafe = true.
- Wtimeout = MS
- The driver adds the {wtimeout: MS} parameter to getlasterror. ImplicitSafe = true.
- Fsync = true | false
- True: The driver adds the {fsync: true} parameter to getlasterror. ImplicitSafe = true.
- False: The fsync parameter is not added to the driver.
Example
Connect to a MongoDB running on the local default port (27017)
mongodb://localhost
Connect to a MongoDB running on the local default port (27017), and log on with the username "Fred" and password "foobar". After logon, the admin database is used by default.
mongodb://fred:foobar@localhost
Connect to a MongoDB running on the local default port (27017), and log on with the username "Fred" and password "foobar". After logon, you will use the Baz Database
mongodb://fred:foobar@localhost/baz
Connect to a replica pair, one server in example1.com, and the other in example2.com
mongodb://example1.com:27017,example2.com:27017
A replica set connected to the local machine (ports are 27019, respectively)
mongodb://localhost,localhost:27018,localhost:27019
Connect to the replica set of the three servers, send all write operations to the master node, and distribute all read operations to the slave node.
mongodb://host1,host2,host3/?slaveok=true
Connect to the first server and respond, whether it is one of the replica set, whether it is the master node or slave Node
mongodb://host1,host2,host3/?connect=direct;slaveok=true
Note: This type of connection string can be used when you prefer to use a server but there is a server that can be replaced.
Connect to the local machine in Safe Mode
mongodb://localhost/?safe=true
Connect to the replica set in safe mode and wait for the backup to be completed on at least two machines. The timeout time is 2 seconds.
mongodb://host1,host2,host3/?safe=true;w=2;wtimeout=2000
Connection Pool
Each TCP connection corresponds to a database thread. Therefore, we strongly recommend that you use some form of connection pool technology. Fortunately, most official database drivers have built in to implement this function. However, if you use CGI or PHP, each new request may start a new application process. In this case, pay special attention to connection processing.