mongodb 備份、還原、導入、導出
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]
這二個命令,可以實現數據庫的備份與還原,文件格式是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 備份、還原、導入、導出