標籤:user   tput   類型   server   cannot   led   理解   erb   支援   
mongodump備份資料庫
常用的備份命令格式
mongodump -h IP --port 連接埠 -u 使用者名稱 -p 密碼 -d 資料庫 -o 檔案存在路徑 
如果想匯出所有資料庫,可以去掉-d
 
mongodump 文法:
[[email protected] ~]# mongodump --help  
Export MongoDB data to BSON files.
options:
  --help                                produce help message
  -v [ --verbose ]                      be more verbose (include multiple times
                                        for more verbosity e.g. -vvvvv)
  --version                             print the program‘s version and exit
  -h [ --host ] arg                     mongo host to connect to ( <set 
                                        name>/s1,s2 for sets)
  --port arg                            server port. Can also use --host 
                                        hostname:port
  --ipv6                                enable IPv6 support (disabled by 
                                        default)
  -u [ --username ] arg                 username
  -p [ --password ] arg                 password
  --authenticationDatabase arg          user source (defaults to dbname)
  --authenticationMechanism arg (=MONGODB-CR)
                                        authentication mechanism
  --dbpath arg                          directly access mongod database files 
                                        in the given path, instead of 
                                        connecting to a mongod  server - needs 
                                        to lock the data directory, so cannot 
                                        be used if a mongod is currently 
                                        accessing the same path
  --directoryperdb                      each db is in a separate directly 
                                        (relevant only if dbpath specified)
  --journal                             enable journaling (relevant only if 
                                        dbpath specified)
  -d [ --db ] arg                       database to use
  -c [ --collection ] arg               collection to use (some commands)
  -o [ --out ] arg (=dump)              output directory or "-" for stdout
  -q [ --query ] arg                    json query
  --oplog                               Use oplog for point-in-time 
                                        snapshotting
  --repair                              try to recover a crashed database
  --forceTableScan                      force a table scan (do not use 
                                        $snapshot)
匯出資料庫
[[email protected] ~]# mongodump -h 127.0.0.1 --port 30216 -d test -uxxxx -pxxxxx -o home/mongodb/
connected to: 10.10.3.245:30216
Thu Aug 11 02:15:04.529 DATABASE: test to /home/mongodb/test
mongorestore還原資料庫
常用命令格式
mongorestore -h IP --port 連接埠 -u 使用者名稱 -p 密碼 -d 資料庫 --drop 檔案存在路徑
[[email protected] mongodb]# mongorestore -d test /home/mongodb/test #test這個資料庫的備份路徑
這二個命令,可以實現資料庫的備份與還原,檔案格式是json和bson的
mongoexport匯出表,或者表中部分欄位
常用命令格式
mongoexport -h IP --port 連接埠 -u 使用者名稱 -p 密碼 -d 資料庫 -c 表名 -f 欄位
    -q 條件匯出 --csv -o 檔案名稱 上面的參數好理解,重點說一下:
    -f 匯出指欄位,以字型大小分割,-f name,email,age匯出name,email,age這三個欄位
    -q 可以根查詢條件匯出,-q ‘{ "_id" : "10001" }‘ 匯出uid為100的資料
    --csv 表示匯出的檔案格式為csv的,這個比較有用,因為大部分的關係型資料庫都是支援csv,在這裡有共同點
匯出整張表
[[email protected] mongodb]# mongoexport -d test -c users -o /home/mongodb/test/users.dat 
connected to: 127.0.0.1 
exported 24 records 
匯出表中部分欄位
[[email protected] mongodb]# mongoexport -d test -c users --csv -f uid,name,sex -o test/users.csv 
connected to: 127.0.0.1 
exported 24 records 
根據條件敢出資料
[[email protected] mongodb]# mongoexport -d test -c users -q ‘{uid:{$gt:1}}‘ -o test/users.json 
connected to: 127.0.0.1 
exported 12 records 
mongoimport匯入表,或者表中部分欄位
還原整表匯出的非csv檔案
mongoimport -h IP --port 連接埠 -u 使用者名稱 -p 密碼 -d 資料庫 -c 表名 --upsert --drop 檔案名稱
重點說一下--upsert,其他參數上面的命令已有提到,--upsert 插入或者更新現有資料
 
還原部分欄位的匯出檔案
mongoimport -h IP --port 連接埠 -u 使用者名稱 -p 密碼 -d 資料庫 -c 表名 --upsertFields 欄位 --drop 檔案名稱  
--upsertFields根--upsert一樣
還原匯出的csv檔案
mongoimport -h IP --port 連接埠 -u 使用者名稱 -p 密碼 -d 資料庫 -c 表名 --type 類型 --headerline --upsert --drop 檔案名稱
上面三種情況,還可以有其他排列組合的。
還原匯出的表資料
[[email protected] mongodb]# mongoimport -d test -c users --upsert test/users.dat 
connected to: 127.0.0.1 
............
部分欄位的表資料匯入
[[email protected] mongodb]# mongoimport -d test -c users  --upsertFields uid,name,sex  test/users.dat  
connected to: 127.0.0.1  
............................................... 
還原csv檔案
[[email protected] mongodb]# mongoimport -d test -c users --type csv --headerline --file test/users.csv 
connected to: 127.0.0.1 
...........................................
mongodb 備份、還原、匯入、匯出