1. 效果展示
2. 安裝MongoDB 8
根據官方文檔https://www.mongodb.com/zh-cn/docs/manual/tutorial/install-mongodb-on-ubuntu/一頓操作即可
2.1 配置微調支持遠程訪問
修改配置文件,默認/etc/mongod.conf
# network interfaces
net:port: 27017bindIp: 0.0.0.0
2.2 新增admin用戶鑒權訪問
在配置文件中,我們啟動權限控制
security:authorization: enabled
通過命令mongosh
,連接到MongoDB,切換數據庫use admin
,然后創建用戶
# 創建用戶
db.createUser({user: "admin",pwd: "yourPassword", roles: [{ role: "root", db: "admin" }]
})
# 查看創建的用戶
db.getUsers()
2.3 重啟服務遠程連接
systemctl restart mongod
mongosh -u admin -p ****** --authenticationDatabase admin --port 27017
遠程通過Navicat等工具即可連接
3. 數據dump與restore
# 數據dump出來
mongodump --host 127.0.0.1 --port 27017 --username admin --password ****** --authenticationDatabase admin --db stock --out /home/shenjian/stock --gzip# 數據restore到遠程服務器
mongorestore --host 192.163.0.9 --port 27017 --username admin --password ****** --authenticationDatabase admin --db stock /home/shenjian/stock/stock --gzip