MongoDB 備份(mongodump)
mongodump -h dbhost -d dbname -o dbdirectory
-h: MongDB所在服務器地址瞒窒,例如:127.0.0.1匿值,當然也可以指定端口號:127.0.0.1:27017
-d: 需要備份的數(shù)據(jù)庫實例海洼,例如:test
-o: 備份的數(shù)據(jù)存放位置掖看,例如:c:\data\dump,當然該目錄需要提前建立槐瑞,在備份完成后据过,系統(tǒng)自動在dump目錄下建立一個test目錄栖博,這個目錄里面存放該數(shù)據(jù)庫實例的備份數(shù)據(jù)屑宠。
mongodump -h 127.0.0.1:27017 -d 數(shù)據(jù)庫名稱 -o ~/Desktop/數(shù)據(jù)庫備份的路徑
mongodump -h 127.0.0.1:27017 -o ~/Desktop/數(shù)據(jù)庫備份的路徑
恢復備份(mongorestore)
mongorestore -h hostname:port -d dbname --dir path
-h (host): MongoDB所在服務器地址,默認為: localhost:27017
-d (db): 需要恢復的數(shù)據(jù)庫實例仇让,例如:test典奉,當然這個名稱也可以和備份時候的不一樣,比如test2
--dir: 指定備份的目錄
MongoDB數(shù)據(jù)導入與導出(mongoimport)
導出
mongoexport -d dbname -c collectionname -o file --type json/csv -f field
d :數(shù)據(jù)庫名
c :collection名
o :輸出的文件名
-type : 輸出的格式丧叽,默認為json
f :輸出的字段卫玖,如果-type為csv,則需要加上-f "字段名"
導出json
mongoexport -d class1804 -c books -o ~/桌面/dump/books.json --type json
導出csv
mongoexport -d class1804 -c books -o ~/桌面/dump/books.csv --type csv -f 'by_user,likes'
導入
mongoimport -d dbname -c collectionname --file filename --headerline --type json/csv
d:數(shù)據(jù)庫名
c:collection名
-type:導入的格式默認json
f:導入的字段名
-headerline:如果導入的格式是csv踊淳,則可以使用第一行的標題作為導入的字段
-file:要導入的文件
- 導入json
例:mongoimport -d class1712B -c books --file ~/桌面/dump/books --type json
- 導入csv
例:mongoimport -d class1712B -c info --file ~/桌面/dump/books --headerline --type csv
權限
修改用戶密碼(切換到有權限操作的數(shù)據(jù)庫下)
db.updateUser(‘username',{pwd:'456'})
添加用戶權限(切換到有權限操作的數(shù)據(jù)庫下)
db.grantRolesToUser('username',[{role:'',db:''}])
移除用戶權限(切換到有權限操作的數(shù)據(jù)庫下)
db.revokeRolesFromUser('username',[{role:'',db:''}])
刪除用戶(方式一)(切換到有權限操作的數(shù)據(jù)庫下)
db.dropUser('username')
刪除用戶 (方式二)
use admin db.system.users.remove({user:'username'})