Documentations and Articles
Setup
- Medium - High-availability MongoDB Cluster Configuration Solutions
- Medium - Configure 3 nodes replica set of MongoDB on AWS EC2
- MongoDB - Deploy a Self-Managed Replica Set
Tips & Configuration
Install MongoDB Tools
Mongoshell
You can download and install mongosh. If you already use linux
, you can follow my guideline
wget https://downloads.mongodb.com/compass/mongodb-mongosh_2.4.2_amd64.deb
chmod +x mongodb-mongosh_2.4.2_amd64.deb
sudo dpkg -i mongodb-mongosh_2.4.2_amd64.deb
After download, you can validate the mongosh by
mongosh --version
Backup and Restore
To backup and restore, we need to use MongoDB Database Tools, such as mongodump
and mongorestore
. Following the guideline to install
wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-debian10-x86_64-100.12.0.deb
chmod +x mongodb-database-tools-debian10-x86_64-100.12.0.deb
sudo dpkg -i mongodb-database-tools-debian10-x86_64-100.12.0.deb
Knowledge
Use MongoDB Connect with DirectConnection
This configuration relate in configuration of MongoDB, when you set replicaset
mode for your MongoDB, when you double-check the configuration member, you can see this at
For the configuration, you can use command with mongosh
rs.conf()
It will show your replicaset configuration in MongoDB, like this
{
_id: <string>,
version: <int>,
term: <int>,
protocolVersion: <number>,
writeConcernMajorityJournalDefault: <boolean>,
configsvr: <boolean>,
members: [
{
_id: <int>,
host: <string>,
arbiterOnly: <boolean>,
buildIndexes: <boolean>,
hidden: <boolean>,
priority: <number>,
tags: <document>,
secondaryDelaySecs: <int>,
votes: <number>
},
...
],
settings: {
chainingAllowed : <boolean>,
heartbeatIntervalMillis : <int>,
heartbeatTimeoutSecs: <int>,
electionTimeoutMillis : <int>,
catchUpTimeoutMillis : <int>,
getLastErrorModes : <document>,
getLastErrorDefaults : <document>,
replicaSetId: <ObjectId>
}
}
If It returns your host
configuration as private ip address, you can encounter some problems about connection when this IP range same as IP range used for your network, that why sometime it will show connection timeout. So for fix this problem, you should connect directly into replicaset via directConnection=true
parameter
mongosh 'mongodb://username:password@server-uri/db?directConnection=true'
Info
In some situations, you can bypass this configuration because it will add default into your connection string, like
- TheΒ
replicaSet
Β query parameter is present in the connection string.- The connection string uses theΒ
mongodb+srv://
Β connection string format.- The connection string contains a seed list with multiple hosts.
- The connection string already contains aΒ
directConnection
Β parameter.
MongoDB CLI Usage
Login into cluster
# Connect via parameter
mongosh --host <host> --port <port> -u <username> -p <password>
# Connect via connection string
mongosh 'mongodb://<user>:<pass>@<host>:<port>/<collection>'