在 WSL 使用 MongoDB(适用于 Ubuntu 系统)

创建
阅读 447

WSL1 运行不了,需要在 WSL2 版本下运行,查看 使用 WSL1 和 WSL2

添加 apt 源

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

安装最新版 MongoDB

sudo apt-get update
sudo apt-get install -y mongodb-org
mongod --version

mongod-version.png

db version v5.0.4
Build Info: {
    "version": "5.0.4",
    "gitVersion": "62a84ede3cc9a334e8bc82160714df71e7d3a29e",
    "openSSLVersion": "OpenSSL 1.1.1f  31 Mar 2020",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu2004",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

添加 service

curl https://raw.githubusercontent.com/mongodb/mongo/master/debian/init.d | sudo tee /etc/init.d/mongodb >/dev/null
备用链接 ( 如果无法请求 githubusercontent )

可以用 jsdelivr 镜像

curl https://cdn.jsdelivr.net/gh/mongodb/mongo@master/debian/init.d | sudo tee /etc/init.d/mongodb >/dev/null

添加执行权限

sudo chmod +x /etc/init.d/mongodb

运行 mongodb

sudo service mongodb start

查看 mongodb 运行状态

sudo service mongodb status

install-mongodb-service.png

一些有用的文件

默认 MongoDB 配置文件

/etc/mongod.conf

日志保存位置

/var/log/mongodb/mongod.log

参考链接

https://docs.microsoft.com/en-us/windows/wsl/tutorials/wsl-database

本文链接 https://www.yidiankuaile.com/post/wsl-mongodb

最后更新