搜索看看
数据库相关说明
/ 使用ubuntu apt安装 MongoDB 7.0社区版
如果是Windows想要安装MongoDB,点击此处下载Window安装包
如果是CentOS系统安装MongoDB,点击此处查看说明
本文中使用的ubuntu版本是22或者20,当你不确定当前Ubuntu版本时,使用命令查看:
cat /etc/lsb-release
1、终端安装,检查是否安装gnupg和curl,如果显示指令不可用,安装即可
sudo apt-get install gnupg curl
2、导入MongoDB公共GPG密钥,运行以下命令即可
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | \ sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \ --dearmor
3、为MongoDB创建列表文件
ubuntu 22使用如下命令
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
ubuntu 20使用如下命令
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
4、重新加载本地包数据库
sudo apt-get update
5、安装MongoDB包
sudo apt-get install -y mongodb-org
至此MongoDB就安装完成了
启动mongodb服务
sudo systemctl start mongod
验证MongoDB是否成功,查看MongoDB的运行状态
root@vultr:~# sudo systemctl status mongod ● mongod.service - MongoDB Database Server Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled) Active: active (running) since Sun 2025-12-28 01:43:19 UTC; 41s ago Docs: https://docs.mongodb.org/manual Main PID: 3091 (mongod) Memory: 133.0M CPU: 574ms CGroup: /system.slice/mongod.service └─3091 /usr/bin/mongod --config /etc/mongod.conf
6、开始使用MongoDB
root@vultr:~# mongosh Current Mongosh Log ID: 69508c6814495c25da8de665 Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.5.10 Using MongoDB: 7.0.28 Using Mongosh: 2.5.10 For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/ To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy). You can opt-out by running the disableTelemetry() command. ------ The server generated these startup warnings when booting 2025-12-28T01:43:19.885+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem 2025-12-28T01:43:20.125+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted 2025-12-28T01:43:20.125+00:00: vm.max_map_count is too low ------ test>
7、可选配置,修改默认端口和希望外网访问,
vim /etc/mongodb.conf
,或者
vim /etc/mongod.conf
找到以下配置项,vim下按字母 i 输入进行文件修改
修改端口,修改port:27017,如修改成port: 37017
外网访问,修改为bind_ip:0.0.0.0
修改完成按Esc,再按Shift+:,然后输入“wq”,保存文件并退出,然后重启mongodb服务
bind_ip = 0.0.0.0 port = 27017
开启mongodb服务 sudo systemctl start mongod 停止mongodb服务 sudo systemctl stop mongod 重启mongodb服务 sudo systemctl restart mongod 设置开机自启 sudo systemctl enable mongod 使用“mongo --port 28017”进入mongo shell
好了,至此MongoDB就安装好了,默认端口是
27017
MongoDB客户端工具使用