mongodb 備份、還原、匯入、匯出

來源:互聯網
上載者:User

標籤: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 備份、還原、匯入、匯出

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.