Checking Connection to MongoDB – 检查与MongoDB的连接

最后修改: 2022年 4月 19日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll learn to check the connection with MongoDB.

在本教程中,我们将学习如何检查与MongoDB的连接。

Importantly to connect to a single MongoDB instance, we need to specify the URI of the MongoDB instance.

重要的是要连接到一个MongoDB实例,我们需要指定MongoDB实例的URI。

2. Checking Connection Using Mongo Shell

2.使用Mongo Shell检查连接

In this section, we’ll connect to the MongoDB server using the mongo shell command. We’ll explore different cases of connecting to MongoDB.

在本节中,我们将使用mongo shell命令连接到MongoDB服务器。我们将探讨连接到MongoDB的不同情况。

2.1. Checking Connection on the Default Port

2.1.检查默认端口的连接情况

By default, MongoDB runs on port 27017, but we can also run it on some other port. We can connect to the MongoDB server using the simple mongo command:

默认情况下,MongoDB在端口27017上运行,但我们也可以在其他端口上运行。我们可以使用简单的mongo命令连接到MongoDB服务器。

$ mongo
MongoDB shell version v4.4.2
connecting to: mongodb://localhost:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b7f80a0c-c7b9-4aea-b34c-605b85e601dd") }
MongoDB server version: 4.0.1-rc0-2-g54f1582fc6

In the command above, by default, MongoDB assumed the port as 27017. If the MongoDB server is down, we get the following error:

在上述命令中,默认情况下,MongoDB假定端口为27017。如果MongoDB服务器停机,我们会得到以下错误。

$ mongo --host localhost --port 27017 admin
MongoDB shell version v4.4.2
connecting to: mongodb://localhost:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server localhost:27017, connection attempt failed:
  SocketException: Error connecting to localhost:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1

In this case, we got the error because we couldn’t connect to the server.

在这种情况下,我们得到的错误是因为我们无法连接到服务器。

2.2. Checking Connection on Secured MongoDB Database

2.2.检查安全的MongoDB数据库的连接情况

MongoDB can be protected with authentication. In that case, we need to pass the username and password in the command:

MongoDB可以用认证来保护。在这种情况下,我们需要在命令中传递用户名和密码。

$ mongo mongodb://baeldung:baeldung@localhost:27017

Here we used the username “baeldung” and the password “baeldung” to connect to the MongoDB running on localhost.

这里我们使用用户名 “baeldung “和密码 “baeldung “来连接到运行在localhost上的MongoDB。

2.3. Checking Connection on Custom Port

2.3.检查自定义端口的连接情况

We can also run MongoDB on a custom port. All we need to do is make changes in the mongod.conf file. If the MongoDB is running on some other port, we need to provide that port in the command:

我们也可以在一个自定义端口上运行MongoDB。我们所要做的就是在mongod.conf文件中进行修改。如果MongoDB运行在其他的端口上,我们需要在命令中提供该端口。

$ mongo mongodb://localhost:27001

Here, in the mongo shell, we can also check the currently active connections of the database server.

在这里,在mongo shell中,我们也可以检查当前数据库服务器的活动连接。

var status = db.serverStatus();
status.connections
{
    "current" : 21,
    "available" : 15979
}

The serverStatus returns a document that gives an overview of the current status of the database process. Regularly running the serverStatus command will collect statistics about the MongoDB instance.

serverStatus返回一个文件,给出了数据库进程的当前状态概览。定期运行serverStatus命令将收集关于MongoDB实例的统计数据。

3. Checking Connection Using Java Driver Code

3.使用Java驱动代码检查连接

So far, we have learned to check the connection with MongoDB using the shell. Now let’s look into the same using Java driver code:

到目前为止,我们已经学会了使用shell来检查与MongoDB的连接。现在,让我们用Java驱动代码来研究一下这个问题。

MongoClientOptions.Builder builder = MongoClientOptions.builder();
// builder settings
ServerAddress ServerAddress = new ServerAddress("localhost", 27017);
MongoClient mongoClient = new MongoClient(ServerAddress, builder.build());

try {
    System.out.println("MongoDB Server is Up:- "+mongoClient.getAddress());
    System.out.println(mongoClient.getConnectPoint());
    System.out.println(db.getStats());
} catch (Exception e) {
    System.out.println("MongoDB Server is Down");
} finally{
    mongoClient.close();
}

In the above code, we first created the MongoClientOption builder to customize the configurations of the MongoClient connectivity, then created the MongoClient connection using the server address. Suppose the MongoDB server is running on the 27017 port of the localhost. Otherwise, the MongoClient will throw an error.

在上述代码中,我们首先创建了MongoClientOption构建器来定制MongoClient连接的配置,然后使用服务器地址创建了MongoClient连接。假设 MongoDB 服务器在 localhost 的 27017 端口上运行。否则,MongoClient将抛出一个错误。

4. Conclusion

4.总结

In this tutorial, we learned to check the connection of the MongoDB server with different real-time cases.

在本教程中,我们学习了用不同的实时情况检查MongoDB服务器的连接。

First, we checked the connection with the mongo default command, then used the authenticated command and also connected to the MongoDB server running on a customized port. Next, we checked the connection for both the MongoDB shell and Java driver code.

首先,我们用mongo默认命令检查了连接情况,然后使用认证命令,也连接到了运行在自定义端口的MongoDB服务器。接下来,我们检查了MongoDB shell和Java驱动代码的连接情况。

The example of all the cases can be found over on GitHub.

所有案例的例子都可以在GitHub上找到over