搜尋
首頁資料庫mysql教程MongoDB之整库备份还原单表collection备份还原

MongoDB之整库备份还原单表collection备份还原 cd D:\MongoDB\bin 1整库备份: mongodump -h dbhost -d dbname -o dbdirectory -h:MongDB所在服务器地址,例如:127.0.0.1:27017 -d:需要备份的数据库实例,例如:wlwdb -o:备份的数据存放位置,例如:D:\M

MongoDB之整库备份还原单表collection备份还原

cd D:\MongoDB\bin

1整库备份:

mongodump -h dbhost -d dbname -o dbdirectory

-h:MongDB所在服务器地址,例如:127.0.0.1:27017

-d:需要备份的数据库实例,例如:wlwdb

-o:备份的数据存放位置,例如:D:\MongoDB\data\dump,当然该目录需要提前建立,在备份完成后,系统自动在dump目录下建立一个wlwdb目录,这个目录里面存放该数据库实例的备份数据。

eg:

mongodump -h 127.0.0.1 -d wlwdb -o D:\MongoDB\data\dump

\

 

可以查看该备份目录:

\

 

mongodump --help

\
mongodump的官方说明:
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 ( /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
 --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     if dbpath specified, each db is in a separate
              directory
 --journal        enable journaling
 -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)

2整库恢复:

mongorestore -h dbhost -d dbname –directoryperdb dbdirectory

-h:MongoDB所在服务器地址

-d:需要恢复的数据库实例,例如:wlwdb(可以和备份时候的不一样)

–directoryperdb:备份数据所在位置,例如:D:\MongoDB\data\dump\wlwdb

–drop:恢复的时候,先删除当前数据,然后恢复备份的数据。

eg:

mongorestore -h 127.0.0.1 -d wlwdb –directoryperdb D:\MongoDB\data\dump\wlwdb

mongorestore的官方说明(可通过mongorestore --help查看):
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 ( /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
 --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    if dbpath specified, each db is in a separate
             directory
 --journal        enable journaling
 -d [ --db ] arg     database to use
 -c [ --collection ] arg collection to use (some commands)
 --objcheck       validate object before inserting
 --filter arg      filter to apply before inserting
 --drop         drop each collection before import
 --oplogReplay      replay oplog for point-in-time restore
 --oplogLimit arg    exclude oplog entries newer than provided timestamp
             (epoch[:ordinal])
 --keepIndexVersion   don't upgrade indexes to newest version
 --noOptionsRestore   don't restore collection options
 --noIndexRestore    don't restore indexes
 --w arg (=1)      minimum number of replicas per write
3单个collection备份:

mongoexport -h dbhost -d dbname -c collectionname -f collectionKey -o dbdirectory

-h: MongoDB所在服务器地址

-d: 需要恢复的数据库实例

-c: 需要恢复的集合

-f: 需要导出的字段(省略为所有字段)

-o: 表示导出的文件名

mongoexport的官方说明(可通过mongoexport --help查看):
 --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 ( /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
 --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     if dbpath specified, each db is in a separate
              directory
 --journal         enable journaling
 -d [ --db ] arg      database to use
 -c [ --collection ] arg  collection to use (some commands)
 -f [ --fields ] arg    comma separated list of field names e.g. -f
              name,age
 --fieldFile arg      file with fields names - 1 per line
 -q [ --query ] arg    query filter, as a JSON string
 --csv           export to csv instead of json
 -o [ --out ] arg     output file; if not specified, stdout is used
 --jsonArray        output to a json array rather than one object per
              line
 -k [ --slaveOk ] arg (=1) use secondaries for export if available, default
              true
 --forceTableScan     force a table scan (do not use $snapshot)
4单个collection恢复:

mongoimport -d dbhost -c collectionname –type csv –headerline –file

-type: 指明要导入的文件格式

-headerline: 批明不导入第一行,因为第一行是列名

-file: 指明要导入的文件路径

mongoimport的官方说明(可通过mongoimport --help查看):
 --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 ( /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
 --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    if dbpath specified, each db is in a separate
             directory
 --journal        enable journaling
 -d [ --db ] arg     database to use
 -c [ --collection ] arg collection to use (some commands)
 -f [ --fields ] arg   comma separated list of field names e.g. -f name,age
 --fieldFile arg     file with fields names - 1 per line
 --ignoreBlanks     if given, empty fields in csv and tsv will be ignored
 --type arg       type of file to import. default: json (json,csv,tsv)
 --file arg       file to import from; if not specified stdin is used
 --drop         drop collection first
 --headerline      CSV,TSV only - use first line as headers
 --upsert        insert or update objects that already exist
 --upsertFields arg   comma-separated fields for the query part of the
             upsert. You should make sure this is indexed
 --stopOnError      stop importing at first error rather than continuing
 --jsonArray       load a json array, not one item per line. Currently
             limited to 16MB.
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
MySQL的角色:Web應用程序中的數據庫MySQL的角色:Web應用程序中的數據庫Apr 17, 2025 am 12:23 AM

MySQL在Web應用中的主要作用是存儲和管理數據。 1.MySQL高效處理用戶信息、產品目錄和交易記錄等數據。 2.通過SQL查詢,開發者能從數據庫提取信息生成動態內容。 3.MySQL基於客戶端-服務器模型工作,確保查詢速度可接受。

mysql:構建您的第一個數據庫mysql:構建您的第一個數據庫Apr 17, 2025 am 12:22 AM

構建MySQL數據庫的步驟包括:1.創建數據庫和表,2.插入數據,3.進行查詢。首先,使用CREATEDATABASE和CREATETABLE語句創建數據庫和表,然後用INSERTINTO語句插入數據,最後用SELECT語句查詢數據。

MySQL:一種對數據存儲的初學者友好方法MySQL:一種對數據存儲的初學者友好方法Apr 17, 2025 am 12:21 AM

MySQL適合初學者,因為它易用且功能強大。 1.MySQL是關係型數據庫,使用SQL進行CRUD操作。 2.安裝簡單,需配置root用戶密碼。 3.使用INSERT、UPDATE、DELETE、SELECT進行數據操作。 4.複雜查詢可使用ORDERBY、WHERE和JOIN。 5.調試需檢查語法,使用EXPLAIN分析查詢。 6.優化建議包括使用索引、選擇合適數據類型和良好編程習慣。

MySQL初學者友好嗎?評估學習曲線MySQL初學者友好嗎?評估學習曲線Apr 17, 2025 am 12:19 AM

MySQL適合初學者,因為:1)易於安裝和配置,2)有豐富的學習資源,3)SQL語法直觀,4)工具支持強大。儘管如此,初學者需克服數據庫設計、查詢優化、安全管理和數據備份等挑戰。

SQL是一種編程語言嗎?澄清術語SQL是一種編程語言嗎?澄清術語Apr 17, 2025 am 12:17 AM

是的,sqlisaprogramminglanguges pecialized fordatamanage.1)它具有焦點,focusingonwhattoachieveratherthanhow.2)sqlisessential forquerying forquerying,插入,更新,更新,和detletingdatainrelationalDatabases.3)

解釋酸的特性(原子,一致性,隔離,耐用性)。解釋酸的特性(原子,一致性,隔離,耐用性)。Apr 16, 2025 am 12:20 AM

ACID屬性包括原子性、一致性、隔離性和持久性,是數據庫設計的基石。 1.原子性確保事務要么完全成功,要么完全失敗。 2.一致性保證數據庫在事務前後保持一致狀態。 3.隔離性確保事務之間互不干擾。 4.持久性確保事務提交後數據永久保存。

MySQL:數據庫管理系統與編程語言MySQL:數據庫管理系統與編程語言Apr 16, 2025 am 12:19 AM

MySQL既是數據庫管理系統(DBMS),也與編程語言緊密相關。 1)作為DBMS,MySQL用於存儲、組織和檢索數據,優化索引可提高查詢性能。 2)通過SQL與編程語言結合,嵌入在如Python中,使用ORM工具如SQLAlchemy可簡化操作。 3)性能優化包括索引、查詢、緩存、分庫分錶和事務管理。

mySQL:使用SQL命令管理數據mySQL:使用SQL命令管理數據Apr 16, 2025 am 12:19 AM

MySQL使用SQL命令管理數據。 1.基本命令包括SELECT、INSERT、UPDATE和DELETE。 2.高級用法涉及JOIN、子查詢和聚合函數。 3.常見錯誤有語法、邏輯和性能問題。 4.優化技巧包括使用索引、避免SELECT*和使用LIMIT。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器