Compacting a replica set

I need to compact a collection in a db that uses replica set. All the documentation I’ve found says to compact the secondaries first. How do I connect to a secondary in order to run the compact command?

Hi @Mark_De_May and welcome to the MongoDB Community forums. :wave:

To connect to a secondary member you would use mongo (installed 5.0.x and earlier) or mongosh (installed with 6.0.x and later). Something like the following:

mongosh mongodb://<URI or IP of secondary>:<port> -u <user> -p <other parameters as needed>

Note that we are not passing in the password so it will prompt you for that. This is the safe way as your password is not exposed on the command line or in the command history.

Once you connect, and authenticate to the secondary node you can do your compaction.

2 Likes

This is a very good advice.

1 Like

Thanks for the help. I tried this and got a MongoServerSelectionError error. Any thoughts on what’s wrong?

Can you post a screenshot showing the error as I’m not familiar with that one. Please blur out host name and user name (if visible) as that’s not needed and will help protect your instance.

Thanks for the screenshot Mark. Add --tls on to the command as Atlas forces secure connections.

That worked!! Thanks again for the help.

1 Like