---
title: "Ubuntu20.04 安装 MongoDB 和 MongoShell"
description: "之前用 apt 安装的 Mongo3.x, 先卸载!"
canonical_url: "https://yangqiuyi.com/blog/linux/ub-20.04-an-zhuang-mongodb"
---

> Index of every page on this site: [llms.txt](https://yangqiuyi.com/llms.txt)

# Ubuntu20.04 安装 MongoDB 和 MongoShell

之前用 apt 安装的 Mongo3.x, 先卸载!

## 卸载<sup>

[1](#user-content-fn-zhihu-mongo)

</sup>



1. 停止 mongodb 服务
sudo service mongod stop
2. 卸载 mongodb
sudo apt-get remove mongodb
3. 移除相关包
sudo apt-get purge mongodb-org
sudo apt-get purge mongodb
sudo apt-get autoremove
sudo apt-get autorclean
4. 移除相关目录
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
5. 查看系统还有哪些残留的文件或目录
whereis mongo
whereis mongodb
whereis mongod
which mongo
which mongodb
which mongod

## 安装

<warning>

我是 Ubuntu20.04, 其他版本可能不能这样安装!
可以通过`lsb_release -dc`指令查看自己的发行版

</warning>

分别下载这些 deb 包然后用`sudo dpkg -i xxxx.deb`来安装

- [MongoDB Community Server](https://www.mongodb.com/try/download/community)
MongoDB 的服务端
- [MongoDB Shell Download](https://www.mongodb.com/try/download/compass)
MongoShell 在 6.x 版本需要单独下载安装(和以前的`mongo`指令类似), 通过`mongosh`进入 mongo shell
- [MongoDB Command Line Database Tools Download](https://www.mongodb.com/try/download/database-tools)
这里有`mongodump`和`mongorestore`<sup>

[2](#user-content-fn-mongodb-tools)

</sup>

<section className="footnotes" dataFootnotes="">

## Footnotes

1. [https://zhuanlan.zhihu.com/p/347614929](https://zhuanlan.zhihu.com/p/347614929) [↩](#user-content-fnref-zhihu-mongo)
2. [https://www.mongodb.com/docs/database-tools/](https://www.mongodb.com/docs/database-tools/) [↩](#user-content-fnref-mongodb-tools)

</section>

## Sitemap

See the full [sitemap](https://yangqiuyi.com/sitemap.md) for all pages.
