目 录CONTENT

文章目录

mongodb安装

青云
2024-01-03 / 0 评论 / 0 点赞 / 25 阅读 / 5458 字
温馨提示:
本文最后更新于 2024-01-07,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

安装

Mac

准备

在macOS上安装MongoDB社区版

mac安装mongodb-过程记录

  1. 首先执行命令确认指向仓库:

    $ brew tap mongodb/brew
    

  2. 接着安装MongoDB 5.0社区版

    $ brew install mongodb-community@5.0
    
  3. 根据文档所示,本次安装包函以下内容:

    The installation includes the following binaries:
    
      - The mongod server
      - The mongos sharded cluster query router
      - The MongoDB Shell, mongosh
    
  4. 安装后的默认目录

    标题intel 芯片M1 芯片
    配置文件/usr/local/etc/mongod.conf/opt/homebrew/etc/mongod.conf
    日志目录/usr/local/var/log/mongodb/opt/homebrew/var/log/mongodb
    数据目录/usr/local/var/mongodb/opt/homebrew/var/mongodb

The installation includes the following binaries:

The mongod server
The mongos sharded cluster query router
The MongoDB Shell, mongosh

启动与停止

  1. 启动
    $ brew services start mongodb-community@5.0
    
  2. 停止
    $ brew services stop mongodb-community@5.0
    

测试连接

安装完成后, 我们需要测试一下是否可用, 直接执行命进入mongodb(记住先启动)

$ mongosh

短暂的等待后, 会进入到mongo环境中, 简单测试下命令

show dbs

一切正常, 至此安装就结束了

配置用户密码

MongoDB默认安装完是没有密码的, 只需要使用ip地址、端口号、数据库等就可以连接, 为了安全考虑, 我们设置一下密码

新建用户

  1. 首先我们新建root账号(以下操作默认都在mongo环境下)

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

    出现提示:

    说明已经创建成功, 我们使用 show users命令则可以看到。

    同理, 也可以新建一些专门访问权限的用户, 比如某个库的只读账号等. 关于上面命令中出现的角色以及权限可以在 文档中查看

    MongoDB文档createUser

开启权限校验

现在重启MongoDB,默认启动MongoDB服务是没有开启访问控制的, 我们可以使用参数--auth来启用

$ mongod --auth --port 27017 --dbpath /usr/local/var/mongodb

稍等片刻即可

或者我们可以修改配置文件/usr/local/etc/mongod.conf添加如下参数:

security:
    authorization: enabled

接下来输入密码登录对应的数据库即可:

$ mongo --port 27017 --authenticationDatabase "admin" -u "root" -p
0

评论区