MongoDB安装配置

361 阅读1分钟

系统说明

本示例使用的系统为Ubuntu22.04

root@mini-desk:~# uname -a
Linux mini-desk 6.5.0-18-generic #18~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Feb  7 11:40:03 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

下载MongoDB

下载服务端

wget https://repo.mongodb.org/apt/ubuntu/dists/jammy/mongodb-org/7.0/multiverse/binary-amd64/mongodb-org-server_7.0.5_amd64.deb

下载MongoDB Shell

MongoDB Shell用来操作MongoDB

https://downloads.mongodb.com/compass/mongodb-mongosh_2.1.5_amd64.deb

安装

 sudo dpkg -i mongodb-org-server_7.0.5_amd64.deb
 sudo dpkg -i mongodb-mongosh_2.1.5_amd64.deb

启动MongoDB

sudo systemctl start mongo
sudo systemctl enable mongo

可以看到MongoDB监听在了27017端口

~# ss -tunlp | grep 27017
tcp   LISTEN 0      1024            127.0.0.1:27017      0.0.0.0:*    users:(("mongod",pid=67849,fd=14)) 

创建用户

使用mongosh连接到MongoDB

root@mini-desk:~# mongosh

切换到admin数据库,创建用户

test> use admin
switched to db admin
admin> db.createUser({user:"root", pwd:"123456", roles:["root"]})
{ ok: 1 }

配置MongoDB服务器

修改配置绑定所有ip

修改/etc/mongod.conf文件,修改内容如下

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0  # 绑定到服务器所有的ip
security:
 authorization: enabled   # 开启认证

重启服务

root@mini-desk:~# systemctl restart mongod

查看端口

可以看到监听了所有ip

~# ss -tunlp | grep 27017
tcp   LISTEN 0      1024              0.0.0.0:27017      0.0.0.0:*    users:(("mongod",pid=72302,fd=14))

连接测试

root@mini-desk:~# mongosh -u root -p 123456 --host 192.168.0.12
Current Mongosh Log ID: 65d7cee35bf9022868275628
Connecting to:          mongodb://<credentials>@192.168.0.12:27017/?directConnection=true&appName=mongosh+2.1.5
Using MongoDB:          7.0.5
Using Mongosh:          2.1.5

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

------
   The server generated these startup warnings when booting
   2024-02-23T06:44:47.564+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
------

image.png

参考链接