MongoDB Dump Export
You can use a MongoDB URI connection string with mongodump
to connect to a remote MongoDB server. The URI connection string includes all the necessary connection details such as hostname, port, database name, and authentication information.
Basic Syntax with URI
mongodump --uri="mongodb://<username>:<password>@<host>:<port>/<database>?authSource=<authDatabase>" --out=<output_directory>
Examples
- Dump a Specific Database Using URI:
mongodump --uri="mongodb://user:pass@remote.mongodb.server:27017/yourDatabase?authSource=admin" --out=/local/path/to/backup
This command connects to the MongoDB server using the specified URI, dumps the yourDatabase
database, and stores it in the local backup directory.
Dump All Databases Using URI:
mongodump --uri="mongodb://user:pass@remote.mongodb.server:27017/?authSource=admin" --out=/local/path/to/backup
This command connects to the MongoDB server using the specified URI and dumps all databases.
Using SSL/TLS with URI:
If your MongoDB server requires SSL/TLS, you can include the ssl=true
option in the URI:
mongodump --uri="mongodb://user:pass@remote.mongodb.server:27017/yourDatabase?authSource=admin&ssl=true" --out=/local/path/to/backup
MongoDB Dump Import
To restore a MongoDB dump using a URI connection string, you use the mongorestore
tool. The process is similar to mongodump
, but instead of creating a backup, you’re restoring from a previously created backup.
Basic Syntax with URI
mongorestore --uri="mongodb://<username>:<password>@<host>:<port>/<database>?authSource=<authDatabase>" --dir=<path_to_backup_directory>
Components of the URI
<username>
: Your MongoDB username.<password>
: Your MongoDB password.<host>
: The hostname or IP address of the MongoDB server.<port>
: The port MongoDB is listening on (default is27017
).<database>
: The name of the database you want to restore to (if omitted, the database names from the dump are used).<authDatabase>
: The database to authenticate against (oftenadmin
).
Examples
Restore a Database Using URI:
mongorestore --uri="mongodb://user:pass@remote.mongodb.server:27017/yourDatabase?authSource=admin" --dir=/local/path/to/backup
This command restores the dump from /local/path/to/backup
into the yourDatabase
database on the MongoDB server specified by the URI.
Restore All Databases Using URI:
If you have a dump of multiple databases and want to restore them all:
mongorestore --uri="mongodb://user:pass@remote.mongodb.server:27017/?authSource=admin" --dir=/local/path/to/backup
This restores all databases from the backup directory into the MongoDB server specified by the URI.
Using SSL/TLS with URI:
If your MongoDB server requires SSL/TLS, include the ssl=true
option in the URI:
mongorestore --uri="mongodb://user:pass@remote.mongodb.server:27017/yourDatabase?authSource=admin&ssl=true" --dir=/local/path/to/backup