May 17, 2021 MongoDB
In this tutorial we'll discuss the different ways MongoDB connects.
In the previous tutorial, we discussed how to start MongoDB services, you only need to perform 'mongod' in the bin directory of the MongoDB installation directory.
After the startup operation, mongodb outputs some necessary information without outputing any information, then waits for the connection to be established, and when the connection is established, the log information is printed.
You can use the MongoDB shell to connect to the MongoDB server. Y ou can also use PHP to connect to mongodb. In this tutorial we'll use mongoDB shell to connect to the Mongodb service, and in later chapters we'll show you how to connect to the MongoDB service via php.
By default, the start port for MongoDB is 27017. The port 1000 longer than the MongoDB boot port is The MongoDB's web user interface, which you can http://localhost:28017 into the browser to access MongoDB's web user interface.
You can connect to MongoDB's services by executing the following commands.
Note: Localhost is the host name, and this option is required:
When you execute the above command, you can see the following output:
If you check where to connect to MongoDB's servers, you can see the following information:
The last line (marked) prints information about your successful connection to the MongoDB service.To connect to the MongoDB server using a username and password, you must use the 'username:password@hostname/dbname' format, 'username' as the user name, and 'password' as the password.
Use the username and password connection to log in to the default database:
In the above commands, mongo_admin can use AxB6_w3r to connect to the local MongoDB service. T he output results are as follows:
Use the username and password connection to log in to the specified database:
The format for connecting to the specified database is as follows:
Connect to the local database server, and the port is the default.
Using the username fred, password foobar logs on to the admin database of localhost.
Using the username fred, password foobar logs on to the baz database of localhost.
Connect replica pair, server 1 is example1.com server 2 is example2.
Connect the replica set three servers (ports 27017, 27018, and 27019):
Connecting the replica set three servers, the write operation is applied on the primary server and the query is distributed from the server.
Connect directly to the first server, whether it's part of the reprica set or the primary server or from the server.
When your connection servers have priority and you also need to list all the servers, you can use the connection methods above.
Safe mode is connected to localhost:
Connect to repica set in safe mode and wait for at least two replication servers to write successfully, with the timeout set to 2 seconds.
Standard format:
The standard connection format includes several options, as follows:
Options | Describe |
---|---|
replicaSet=name | Verify the name of the reprica set. Impliesconnect=replicaSet. |
slaveOk=true|false |
|
safe=true|false |
|
w=n | Drive Add {W: N} to getLastError command. Apply to Safe = True. |
wtimeoutMS=ms | Drive Add {WTIMEOUT: MS} to getLastError command. Apply to Safe = true. |
fsync=true|false |
|
journal=true|false | If wie true is set, sync to journal (write to the entity before submitting to the database). Applied to safe-true |
connectTimeoutMS=ms | The time at which the connection can be opened. |
socketTimeoutMS=ms | Time to send and receive sockets. |