远程连接MongoDB

robo3t

远程连接MongoDB

配置远程连接 MongoDB

IP 绑定

默认情况下,MongoDB 启动时将 bindIp 设置为 127.0.0.1,绑定到 localhost 网络接口。这意味着 mongod 只能接受来自同一计算机上运行的客户端的连接。远程客户端将无法连接到 mongod,并且 mongod将无法初始化副本集,除非将此值设置为有效的网络接口。

可以一下两种方式配置绑定的 IP:

  • 在带有 bindIp 的 MongoDB 配置文件中(默认 /etc/mongod.conf)
  • 或者通过命令行参数 --bind_ip

WARNING

Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured your cluster from unauthorized access. For a complete list of security recommendations, see Security Checklist. At minimum, consider enabling authentication and hardening network infrastructure.

端口绑定

端口默认绑定到 27017,可以根据需要进行配置,配置方法与配置 IP 绑定类似。

  • 在带有 port 的 MongoDB 配置文件中(默认 /etc/mongod.conf)
  • 或者通过命令行参数 --port

不启用认证

在不启用认证的情况下,要想远程连接到 MongoDB,需要将 IP 绑定到 0.0.0.0,以修改配置文件为例,编辑 /etc/mongod.conf 文件:

bindIp: 127.0.0.1 替换为 bindIp: 0.0.0.0

修改完成后,重启 mongod:

1
systemctl restart mongod

如果你的防火墙对端口有限制,请关闭防火墙或开放相应的端口。

启用认证

不需要认证的远程连接方式具有很大的安全风险,所以一般远程连接都采用认证的方式。

启用访问控制时,请确保你在管理数据库中拥有一个具有 userAdmin 或 userAdminAnyDatabase 角色的用户。该用户可以管理用户和角色,例如:创建用户,向用户授予或撤消角色,以及创建或修改自定义角色。

启用认证的简要步骤:

  • 不启用访问控制的情况下运行 MongoDB
  • 使用 mongo shell 连接到运行的 mongod 实例
  • 创建一个具有 userAdmin 或 userAdminAnyDatabase 角色的用户
  • 启用访问控制
  • 重启 MongoDB

以上步骤假定你的管理数据库中没有一个具有 userAdmin 或 userAdminAnyDatabase 角色的用户。

创建一个认证用户

下面将创建一个具有 userAdmin 或 userAdminAnyDatabase 角色的用户 myUserAdmin。

(1) 不启用访问控制的情况下启动 MongoDB
1
mongod --port 27017 --dbpath /var/lib/mongodb

你可以使用 systemd 的方式启动管理 mongod:

1
systemctl start mongod
(2) 连接到 MongoDB 运行的实例
1
mongo --port 27017
(3) 创建管理员用户 root

在 mongo shell 中,在 admin 数据库中添加一个具有 userAdminAnyDatabase 角色的用户。包括此用户所需的其他角色。例如,以下代码在 admin 数据库中使用 userAdminAnyDatabase 角色和 readWriteAnyDatabase 角色创建用户 myUserAdmin。

TIP

Starting in version 4.2 of the mongo shell, you can use the passwordPrompt() method in conjunction with various user authentication/management methods/commands to prompt for the password instead of specifying the password directly in the method/command call. However, you can still specify the password directly as you would with earlier versions of the mongo shell.

1
2
3
4
5
6
7
8
use admin
db.createUser(
{
user: "myUserAdmin",
pwd: passwordPrompt(), // or cleartext password
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
}
)

NOTE

The database where you create the user (in this example, admin) is the user’s authentication database. Although the user would authenticate to this database, the user can have roles in other databases; i.e. the user’s authentication database does not limit the user’s privileges.

(4) 启用访问控制重启 MongoDB
  • 关闭 mongod 实例。例如,在 mongo shell 中,执行以下命令:

    1
    db.adminCommand( { shutdown: 1 } )
  • 退出 mongo shell

  • 在终端上,使用 --auth 命令行选项重新启动 mongod 实例:

    1
    mongod --auth --port 27017 --dbpath /var/lib/mongodb

    现在,连接到该实例的客户端必须将自己认证为 MongoDB 用户。客户只能执行由其分配的角色确定的操作。

如果你是通过 systemd 的方式利用配置文件启动的:

  • 创建完认证用户后,退出 mongo shell

  • 编辑配置文件 /etc/mongod.conf

    在 security 下添加 authorization,删除 security 前面的注释符号 #,如:

    1
    2
    security:
    authorization: enabled
  • 重启 mongod

    1
    systemctl restart mongod
(5) 以用户管理员身份连接并进行身份验证

使用 mongo shell,你可以:

  • 通过连接时传递用户凭据以验证身份
  • 或首先连接而不进行身份验证,然后通过 db.auth() 方法进行身份验证

连接时认证:

使用 -u, -p, 和 --authenticationDatabase 命令行选项启动 mongo shell:

1
mongo --port 27017  --authenticationDatabase "admin" -u "myUserAdmin" -p

出现提示时输入密码。

连接后认证:

将 mongo shell 连接到 mongod:

1
mongo --port 27017

在 mongo shell 中,切换到身份验证数据库(在本例中为 admin),然后使用 db.auth(<username>, <pwd>)方法进行身份验证:

1
2
use admin
db.auth("myUserAdmin", passwordPrompt()) // or cleartext password
(6) 根据部署需要创建其他用户

通过身份验证为用户管理员后,可以使用 db.createUser() 创建其他用户。你可以将任何 built-in rolesuser-defined roles 分配给用户。

以下操作将用户 myTester 添加到 test 数据库,该用户在 test 数据库中具有 readWrite 角色,在 reporting 数据库中具有 read 角色。

1
2
3
4
5
6
7
8
9
use test
db.createUser(
{
user: "myTester",
pwd: passwordPrompt(), // or cleartext password
roles: [ { role: "readWrite", db: "test" },
{ role: "read", db: "reporting" } ]
}
)

创建其他用户后,断开 mongo shell 的连接。

(7) 连接到实例并通过 myTester 进行身份验证

以 myUserAdmin 断开 mongo shell 的连接后,以 myTester 的身份重新连接。你可以:

连接时验证身份:

1
mongo --port 27017 -u "myTester" --authenticationDatabase "test" -p

或者连接后验证身份:

1
mongo --port 27017
1
2
use test
db.auth("myTester", passwordPrompt()) // or cleartext password

出现提示时输入密码。

(8) 作为 myTester 插入文档

作为 myTester,你有权在 test 数据库中执行读取和写入操作(以及在 reporting 数据库中执行读取操作)。 通过 myTester 身份验证后,将文档插入 test 数据库的集合中。例如,你可以在测试数据库中执行以下插入操作:

1
db.foo.insert( { x: 1, y: 1 } )

robo 3t 远程连接 MongoDB

robo3t-connection

robo3t-authentication

robo3t-test

References

https://docs.mongodb.com/manual/tutorial/enable-authentication/