2023-04-06 安装mongodb和清空mongodb数据库

1、清空mongodb数据库

brew services list

~/Library/LaunchAgents/homebrew.mxcl.mongodb-community@4.4.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>homebrew.mxcl.mongodb-community@4.4</string>
  <key>ProgramArguments</key>
  <array>
    <string>/opt/homebrew/opt/mongodb-community@4.4/bin/mongod</string>
    <string>--config</string>
    <string>/opt/homebrew/etc/mongod.conf</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>KeepAlive</key>
  <false/>
  <key>WorkingDirectory</key>
  <string>/opt/homebrew</string>
  <key>StandardErrorPath</key>
  <string>/opt/homebrew/var/log/mongodb/output.log</string>
  <key>StandardOutPath</key>
  <string>/opt/homebrew/var/log/mongodb/output.log</string>
  <key>HardResourceLimits</key>
  <dict>
    <key>NumberOfFiles</key>
    <integer>64000</integer>
  </dict>
  <key>SoftResourceLimits</key>
  <dict>
    <key>NumberOfFiles</key>
    <integer>64000</integer>
  </dict>
</dict>
</plist>

/opt/homebrew/etc/mongod.conf

systemLog:
  destination: file
  path: /opt/homebrew/var/log/mongodb/mongo.log
  logAppend: true
storage:
  dbPath: /opt/homebrew/var/mongodb
net:
  bindIp: 127.0.0.1, ::1
  ipv6: true

停止mongodb服务:brew services stop mongodb-community@4.4

删除/opt/homebrew/var/mongodb/*
删除/opt/homebrew/var/log/mongodb/*

重启mongodb服务:brew services start mongodb-community@4.4
这样就清空了mongodb数据库所有数据。

2、安装mongodb数据库

  • 在安装社区版前要先执行: brew tap mongodb/brew 这个过程有点久,我装过可能需要30分钟左右。
  • 安装最新社区版:brew reinstall mongodb-community
  • 启动 mongodb-community 服务:brew services start mongodb-community
    提示 “Service mongodb-community already started”,说明服务启动成功

mongodb-community 命令区别 mongodb

  • 启动服务:brew services start mongodb-community
  • 重启服务: brew services restart mongodb-community
  • 停止服务:brew services stop mongodb-community
  • 安装某个版本:brew install mongodb-community@x.x.x
  • 手动启动服务:mongod --config /usr/local/etc/mongod.conf

文件路径:

  • 配置文件:/usr/local/etc/mongod.conf
  • 日志目录路径:/usr/local/var/log/mongodb
  • 数据目录路径:/usr/local/var/mongodb

这样 MongoDB 服务就安装和启动好了。

将 MongoDB 下载到本地,mongodb-osx-ssl-x86_64-4.0.13.tgz 我下载的是这个版本,解压后,新建目录,将解压后的文件存放在里面。

我的目录为:/Volumes/code/localhost/node/mongodb/bin

  • 创建存储数据库文件 data
       在任意盘符根目录下创建一个 data 目录,用来存放数据库文件。 mongoDB 会自动把自己安装位置的盘符根目录下的 data 文件夹作为自己的数据存储目录,这里也可以直接在安装位置所在盘符创建,我是在 bin 目录下创建的 data 目录。

新开一个 shell,指定 db 路径:

进入 /Volumes/code/localhost/node/mongodb/bin 目录 ,输入 mongod,会看到 :

MongoDB starting : pid=942 port=27017 dbpath=/data/db 64-bit

db version v4.2.1

git version: edf6d45851c0b9ee15548f0f847df141764a317e

。。。

说明 db 启动成功了!!

如果 data 目录在其它位置,需要指定 data 目录路径,输入 mongod --dbpath 路径

默认情况下,db 启动成功后,本地会有三个表 admin / config / local

转:https://www.cnblogs.com/baiyygynui/p/11865647.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容