117.info
人生若只如初见

如何在Debian上配置MongoDB的认证

在Debian上配置MongoDB的认证可以通过以下步骤完成。这些步骤包括安装MongoDB、启用认证以及创建管理员用户。

1. 安装MongoDB

首先,确保你的Debian系统是最新的:

sudo apt update
sudo apt upgrade

然后,添加MongoDB官方APT仓库:

wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

更新APT包列表并安装MongoDB:

sudo apt update
sudo apt install -y mongodb-org

2. 启用认证

编辑MongoDB配置文件 /etc/mongod.conf,找到 security 部分并启用认证:

security:
  authorization: enabled

保存并关闭文件,然后重启MongoDB服务:

sudo systemctl restart mongod

3. 创建管理员用户

连接到MongoDB实例:

mongo

在MongoDB shell中,切换到 admin 数据库并创建一个管理员用户:

use admin
db.createUser({
  user: "admin",
  pwd: "your_password",
  roles: ["root"]
})

your_password 替换为你选择的强密码。

4. 使用管理员用户认证

退出MongoDB shell:

exit

重新连接到MongoDB实例并使用管理员用户认证:

mongo -u admin -p your_password --authenticationDatabase admin

5. 创建其他数据库用户

你可以为其他数据库创建用户。例如,为 mydatabase 数据库创建一个用户:

use mydatabase
db.createUser({
  user: "myuser",
  pwd: "mypassword",
  roles: ["readWrite", "dbOwner"]
})

myusermypassword 替换为你选择的用户名和密码。

6. 验证用户权限

确保新创建的用户具有适当的权限:

use mydatabase
db.auth("myuser", "mypassword")
db.runCommand({ connectionStatus: 1 })

通过这些步骤,你已经在Debian上成功配置了MongoDB的认证。

未经允许不得转载 » 本文链接:https://www.117.info/ask/fee3eAzsNAwJeDFE.html

推荐文章

  • 如何使用FetchDebian加速下载

    使用FetchDebian来加速下载,可以参考以下步骤: 选择合适的镜像源: 在安装Debian时,选择一个离你地理位置较近的镜像源可以显著提高下载速度。例如,选择国内的...

  • mysql在debian上怎么配置

    在 Debian 上配置 MySQL 主要包括以下几个步骤:
    安装 MySQL 更新系统: sudo apt update
    sudo apt upgrade -y 安装 MySQL: sudo apt install mysql-...

  • Debian怎样提升Flutter编译速度

    在Debian系统上提升Flutter编译速度可以通过以下几种方法实现: 使用–release模式编译:
    使用--release模式编译应用,这会启用树摇优化和AOT(Ahead-of-Ti...

  • Debian如何进行软件包的回收管理

    在Debian系统中,进行软件包的回收管理主要涉及到使用apt和dpkg这两个命令行工具。以下是一些常用的命令和步骤,用于卸载不再需要的软件包及其配置文件,以及清理...

  • CentOS时间戳与文件关联

    在 CentOS 系统中,时间戳通常与文件的创建、修改和访问时间相关联。这些时间戳可以通过 ls -l 命令查看。如果你想将时间戳与文件关联,可以使用以下方法: 使用...

  • 如何分析Debian JS日志错误

    分析Debian系统中的JavaScript(JS)日志错误通常涉及以下几个步骤: 定位日志文件: Debian系统中的日志文件通常位于/var/log目录下。
    对于JavaScript应用...

  • CentOS Apache如何恢复

    在CentOS系统中恢复Apache服务通常涉及几个步骤,包括检查服务状态、修复配置文件、重新启动服务等。以下是一些常见的恢复步骤:
    检查Apache服务状态
    ...

  • debian gcc如何使用预处理器指令

    在Debian系统上使用GCC编译器时,预处理器指令是编译过程的一个重要部分。预处理器指令以#开头,用于在编译之前对源代码进行文本替换、条件编译等操作。以下是一...