0%

在M1芯片 MacBook pro 安装 MongoDB

摘要:安装适合 M1 芯片的 MongoDB 社区版本。

前提条件:

1、M1 芯片 MacBook

2、已经安装 Xcode 命令行工具

3、已经安装 Homebrew

安装步骤

(建议全局科学上网,不然大概率出现因文件不能正常下载而安装失败)

1
2
3
4
5
# 下载官方配置
brew tap mongodb/brew

# 安装 MongoDB
brew install mongodb-community@5.0

安装位置:

1
2
3
4
5
6
7
8
# 配置文件
/opt/homebrew/etc/mongod.conf

#日志文件
/opt/homebrew/var/log/mongodb

# 数据目录
/opt/homebrew/var/mongodb

安装完成日志:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
==> Summary
🍺 /opt/homebrew/Cellar/mongodb-community/5.0.6: 11 files, 181.5MB, built in 2 seconds
==> Running `brew cleanup mongodb-community`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> mongodb-community

To start mongodb/brew/mongodb-community now and restart at login:
brew services start mongodb/brew/mongodb-community

Or, if you do not want/need a background service you can just run:
mongod --config /opt/homebrew/etc/mongod.conf

检查是否安装完成:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mongod -version

----
db version v5.0.6
Build Info: {
"version": "5.0.6",
"gitVersion": "212a8dbb47f07427dae194a9c75baec1d81d9259",
"modules": [],
"allocator": "system",
"environment": {
"distarch": "x86_64",
"target_arch": "x86_64"
}
}

启动服务:

1
2
3
4
5
6
7
8
9
10
11
#作为 macOS 服务运行
brew services start mongodb-community@5.0

#停止服务
brew services stop mongodb-community@5.0

#重启服务
brew services restart mongodb-community@5.0

#查看服务运行列表
brew services list

基础使用

连接数据库:

1
2
3
4
5
6
7
8
9
mongosh

----
Current Mongosh Log ID: 6232a53ad95c821061c3c2ac
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.2.3
Using MongoDB: 5.0.6
Using Mongosh: 1.2.3

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

新增一条数据:

1
2
3
4
5
db.testdb.insertOne({name:"legalgeek",slogen:"hello world"})
{
acknowledged: true,
insertedId: ObjectId("6232a5ced95c821061c3c2ad")
}

查询指定数据:

1
2
3
4
5
6
7
8
db.testdb.find({name:"legalgeek"})
[
{
_id: ObjectId("6232a5ced95c821061c3c2ad"),
name: 'legalgeek',
slogen: 'hello world'
}
]

自带数据库工具

1
mongotop

参考官方文档:
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/