使用mongodump工具,该工具被封装到了mongodb-database-tools软件包中,如果工具不存在,则下载最新的软件包
MongoDB官方下载地址
安装&验证方式为二进制解压缩并且更新环境变量,太基础此处就不详细赘述了。
导出语句:
mongodump --authenticationDatabase=act_test --username=user_test --password="user_test@123" --host=127.0.0.1 --port=9001 --db=act_test --out=/data/backup
导出命令详解如下:
[root@mongdb ~]# mongodump --version
mongodump version: 100.10.0
git version: 6d4f001be3fcf673de04d20176e90ee02ef233a9
Go version: go1.21.12
os: linux
arch: amd64
compiler: gc
[root@mongdb ~]# mongodump --help
Usage:
mongodump <options> <connection-string>
Export the content of a running server into .bson files.
Specify a database with -d and a collection with -c to only dump that database or collection.
Connection strings must begin with mongodb:// or mongodb+srv://.
See http://docs.mongodb.com/database-tools/mongodump/ for more information.
general options:
--help print usage
--version print the tool version and exit
--config= path to a configuration file
verbosity options:
-v, --verbose=<level> more detailed log output (include multiple times for more verbosity, e.g. -vvvvv, or specify a numeric value, e.g. --verbose=N)
--quiet hide all log output
connection options:
-h, --host=<hostname> mongodb host to connect to (setname/host1,host2 for replica sets)
--port=<port> server port (can also use --host hostname:port)
ssl options:
--ssl connect to a mongod or mongos that has ssl enabled
--sslCAFile=<filename> the .pem file containing the root certificate chain from the certificate authority
--sslPEMKeyFile=<filename> the .pem file containing the certificate and key
--sslPEMKeyPassword=<password> the password to decrypt the sslPEMKeyFile, if necessary
--sslCRLFile=<filename> the .pem file containing the certificate revocation list
--sslFIPSMode use FIPS mode of the installed openssl library
--tlsInsecure bypass the validation for server's certificate chain and host name
authentication options:
-u, --username=<username> username for authentication
-p, --password=<password> password for authentication
--authenticationDatabase=<database-name> database that holds the user's credentials
--authenticationMechanism=<mechanism> authentication mechanism to use
--awsSessionToken=<aws-session-token> session token to authenticate via AWS IAM
kerberos options:
--gssapiServiceName=<service-name> service name to use when authenticating using GSSAPI/Kerberos (default: mongodb)
--gssapiHostName=<host-name> hostname to use when authenticating using GSSAPI/Kerberos (default: <remote server's address>)
namespace options:
-d, --db=<database-name> database to use
-c, --collection=<collection-name> collection to use
uri options:
--uri=mongodb-uri mongodb uri connection string
query options:
-q, --query= query filter, as a v2 Extended JSON string, e.g., '{"x":{"$gt":1}}'
--queryFile= path to a file containing a query filter (v2 Extended JSON)
--readPreference=<string>|<json> specify either a preference mode (e.g. 'nearest') or a preference json object (e.g. '{mode: "nearest", tagSets: [{a: "b"}], maxStalenessSeconds: 123}')
--forceTableScan force a table scan (do not use $snapshot or hint _id). Deprecated since this is default behavior on WiredTiger
output options:
-o, --out=<directory-path> output directory, or '-' for stdout (default: 'dump')
--gzip compress archive or collection output with Gzip
--oplog for taking a point-in-time snapshot on a replica set that is not part of a sharded cluster.
--archive=<file-path> dump as an archive to the specified path. If flag is specified without a value, archive is written to stdout
--dumpDbUsersAndRoles dump user and role definitions for the specified database
--excludeCollection=<collection-name> collection to exclude from the dump (may be specified multiple times to exclude additional collections)
--excludeCollectionsWithPrefix=<collection-prefix> exclude all collections from the dump that have the given prefix (may be specified multiple times to exclude additional prefixes)
-j, --numParallelCollections= number of collections to dump in parallel
--viewsAsCollections dump views as normal collections with their produced data, omitting standard collections
导入语句:
mongorestore --authenticationDatabase=act_test --username=user_test --password="user_test@123" --host=127.0.0.1 --port=9001 --db=act_test --dir=/data/tmp/act_test_202411/act_test
导入命令详解如下:
[root@mongdb ~]# mongodump --version
mongodump version: 100.10.0
git version: 6d4f001be3fcf673de04d20176e90ee02ef233a9
Go version: go1.21.12
os: linux
arch: amd64
compiler: gc
[root@mongdb ~]#
[root@mongdb ~]#
[root@mongdb ~]# mongorestore --version
mongorestore version: 100.10.0
git version: 6d4f001be3fcf673de04d20176e90ee02ef233a9
Go version: go1.21.12
os: linux
arch: amd64
compiler: gc
[root@mongdb ~]# mongorestore --help
Usage:
mongorestore <options> <connection-string> <directory or file to restore>
Restore backups generated with mongodump to a running server.
Specify a database with -d to restore a single database from the target directory,
or use -d and -c to restore a single collection from a single .bson file.
Connection strings must begin with mongodb:// or mongodb+srv://.
See http://docs.mongodb.com/database-tools/mongorestore/ for more information.
general options:
--help print usage
--version print the tool version and exit
--config= path to a configuration file
verbosity options:
-v, --verbose=<level> more detailed log output (include multiple times for more verbosity, e.g. -vvvvv, or specify a numeric value, e.g. --verbose=N)
--quiet hide all log output
connection options:
-h, --host=<hostname> mongodb host to connect to (setname/host1,host2 for replica sets)
--port=<port> server port (can also use --host hostname:port)
ssl options:
--ssl connect to a mongod or mongos that has ssl enabled
--sslCAFile=<filename> the .pem file containing the root certificate chain from the certificate authority
--sslPEMKeyFile=<filename> the .pem file containing the certificate and key
--sslPEMKeyPassword=<password> the password to decrypt the sslPEMKeyFile, if necessary
--sslCRLFile=<filename> the .pem file containing the certificate revocation list
--sslFIPSMode use FIPS mode of the installed openssl library
--tlsInsecure bypass the validation for server's certificate chain and host name
authentication options:
-u, --username=<username> username for authentication
-p, --password=<password> password for authentication
--authenticationDatabase=<database-name> database that holds the user's credentials
--authenticationMechanism=<mechanism> authentication mechanism to use
--awsSessionToken=<aws-session-token> session token to authenticate via AWS IAM
kerberos options:
--gssapiServiceName=<service-name> service name to use when authenticating using GSSAPI/Kerberos (default: mongodb)
--gssapiHostName=<host-name> hostname to use when authenticating using GSSAPI/Kerberos (default: <remote server's address>)
namespace options:
-d, --db=<database-name> database to use
-c, --collection=<collection-name> collection to use
uri options:
--uri=mongodb-uri mongodb uri connection string
namespace options:
--excludeCollection=<collection-name> DEPRECATED; collection to skip over during restore (may be specified multiple times to exclude additional collections)
--excludeCollectionsWithPrefix=<collection-prefix> DEPRECATED; collections to skip over during restore that have the given prefix (may be specified multiple times to exclude additional prefixes)
--nsExclude=<namespace-pattern> exclude matching namespaces
--nsInclude=<namespace-pattern> include matching namespaces
--nsFrom=<namespace-pattern> rename matching namespaces, must have matching nsTo
--nsTo=<namespace-pattern> rename matched namespaces, must have matching nsFrom
input options:
--objcheck validate all objects before inserting
--oplogReplay for recovering a point-in-time snapshot on a replica set that is not part of a sharded cluster.
--oplogLimit=<seconds>[:ordinal] only include oplog entries before the provided Timestamp
--oplogFile=<filename> oplog file to use for replay of oplog
--archive=<filename> restore dump from the specified archive file. If flag is specified without a value, archive is read from stdin
--restoreDbUsersAndRoles restore user and role definitions for the given database
--dir=<directory-name> input directory, use '-' for stdin
--gzip decompress gzipped input
restore options:
--drop drop each collection before import
--dryRun view summary without importing anything. recommended with verbosity
--writeConcern=<write-concern> write concern options e.g. --writeConcern majority, --writeConcern '{w: 3, wtimeout: 500, fsync: true, j: true}'
--noIndexRestore don't restore indexes
--convertLegacyIndexes Removes invalid index options and rewrites legacy option values (e.g. true becomes 1).
--noOptionsRestore don't restore collection options
--keepIndexVersion don't update index version
--maintainInsertionOrder restore the documents in the order of their appearance in the input source. By default the insertions will be performed in an arbitrary order. Setting this flag also enables the behavior of --stopOnError and
restricts NumInsertionWorkersPerCollection to 1.
-j, --numParallelCollections= number of collections to restore in parallel
--numInsertionWorkersPerCollection= number of insert operations to run concurrently per collection
--stopOnError halt after encountering any error during insertion. By default, mongorestore will attempt to continue through document validation and DuplicateKey errors, but with this option enabled, the tool will stop
instead. A small number of documents may be inserted after encountering an error even with this option enabled; use --maintainInsertionOrder to halt immediately after an error
--bypassDocumentValidation bypass document validation
--preserveUUID preserve original collection UUIDs (off by default, requires drop)
--fixDottedHashIndex when enabled, all the hashed indexes on dotted fields will be created as single field ascending indexes on the destination
最终,进入到导入后的库查看数据
mongosh --authenticationDatabase activity -u user_developer -p "user_developer123" --host=127.0.0.1 --port=9001
show dbs;
use act_test;
db.table_test.find()