數據備份 mongodump
./mongodump -h localhost:27022 -d lison -o /usr/local/mongodb/mongodb-linux-x86_64-3.4.18/backup
-h :指定ip和端口; -d :備份的數據庫名稱 ; -o:指定備份的路徑
其本質為:執行查詢,然后寫入文件;
數據恢復 mongorestore
./mongorestore -h localhost:27022 -d lison /usr/local/mongodb/mongodb-linux-x86_64-3.4.18/backup/lison --drop
--drop 已存在lison庫則刪除原數據庫,去掉--drop則是合並
數據導出 mongoexport(針對集合)
./mongoexport -h localhost:27022 -d lison -c users -f id,username,age,salary --type=csv -o /usr/local/mongodb/mongodb-linux-x86_64-3.4.18/backup/users.csv
-c :指定導出的集合; -f :要導出的字段; --type:導出的文件格式類型[csv,json]
數據導入 mongoimport(針對集合)
./mongoimport -h localhost:27022 -d lison -c users /usr/local/mongodb/mongodb-linux-x86_64-3.4.18/backup/users.csv --upsert
--upsert 表示更新現有數據,如果不適用—upsert,則導入時已經存在的文檔會報id重復,數據不再插入,也可以使用—drop刪除原有數據
end.