如何将绑定到localhost:27017的MongoDB users库复制到其他实例?
Got it, let's walk through how to copy your users database from the local MongoDB instance (bound to localhost:27017) to another instance. There are a couple of reliable methods you can use, depending on your needs:
mongodump + mongorestore) This is the most common approach, great if you want to keep a backup file for later use or need to transfer data between machines.
Step 1: Backup the local users database
Open your terminal and run the mongodump command to create a backup of your local database:
mongodump --host localhost:27017 --db users --out ./users_backup
--host: Specifies the source MongoDB instance (here it's your locallocalhost:27017)--db: The exact database you want to copy (users)--out: The directory where the backup files will be stored (we're using./users_backupin the current working directory)
Once this finishes, you'll see a users_backup folder with all the data files for the users database.
Step 2: Restore the backup to the target instance
Assuming your target MongoDB instance is bound to target-host:27017 (replace this with your actual target address), run the mongorestore command:
mongorestore --host target-host:27017 --db users ./users_backup/users
--host: The address of your target MongoDB instance--db: The name you want the database to have on the target (can match the source or be renamed if needed)- The final argument points to the specific backup directory for the
usersdatabase
If your target instance requires authentication, add the auth parameters:
mongorestore --host target-host:27017 --username your_target_user --password your_target_pass --authenticationDatabase admin --db users ./users_backup/users
If you don't need to keep a backup file and want a faster, direct transfer, you can pipe the output of mongodump directly into mongorestore. This skips writing files to disk entirely:
mongodump --host localhost:27017 --db users --archive | mongorestore --host target-host:27017 --db users --archive
--archive: Packages the backup into a single archive stream, perfect for piping- The
|pipe operator sends the backup data straight from the source to the target instance
For authenticated target instances, adjust the command like so:
mongodump --host localhost:27017 --db users --archive | mongorestore --host target-host:27017 --username your_target_user --password your_target_pass --authenticationDatabase admin --db users --archive
- If your source instance requires authentication, add the same
--username,--password, and--authenticationDatabaseparameters to themongodumpcommand - Try to keep source and target MongoDB versions compatible. Cross-major-version migrations may need extra steps—double-check the official docs if you're jumping between versions like 4.x and 6.x
- For large databases, schedule this during low-traffic hours to avoid impacting your application
- If you're working with a sharded cluster (not a single instance), you'll need to use the
--oplogflag withmongodumpto ensure data consistency during the transfer
内容的提问来源于stack exchange,提问作者Francesco Vicidomini

