安装
sudo apt update
sudo apt install -y mongodb
检查
sudo systemctl status mongodb
应输出:
可进一步验证:
mongo --eval 'db.runCommand({ connectionStatus: 1 })'
会得到版本号等信息。
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
{
“authInfo” : {
“authenticatedUsers” : [ ],
“authenticatedUserRoles” : [ ]
},
“ok” : 1
}
到此,已经安装成功。
管理
启动:
sudo systemctl start mongodb
停止:
sudo systemctl stop mongodb
重启:
sudo systemctl restart mongodb
查看状态:
sudo systemctl status mongodb
自启动:
sudo systemctl enable mongodb
禁止自启:
sudo systemctl disable mongodb
配置远程访问
编辑 /etc/mongodb.conf
把
扫描二维码关注公众号,回复:
11610884 查看本文章
bind_ip = 127.0.0.1
#port = 27017
改为
bind_ip = 0.0.0.0
port = 27017
如果想要允许特定ip,可在127.0.0.1
后逗号追加ip地址,如
bind_ip = 127.0.0.1,192.168.1.21
重启服务,sudo systemctl restart mongodb
,应该能从远程访问了。(如使用Robo 3T等GUI)。
如果不能访问,可能需要修改防火墙规则。
备份与还原
备份:
mongodump -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -o 文件存在路径
如果没有用户,可去掉-u和-p
如果导出本机的数据库,可去掉-h。
如果是默认端口,可以去掉–port。
如果想导出所有数据库,可以去掉-d。
执行成功会得到一个文件夹,包含bson和json文件。
还原
mongorestore -d [数据库] [dump文件夹]