Difference between revisions of "DB/MongoDB"
< DB
		
		
		
		Jump to navigation
		Jump to search
		| Line 26: | Line 26: | ||
| <source lang=bash> | <source lang=bash> | ||
| # Print the RS (replication set) configuration | # Print the RS (replication set) configuration | ||
| kubectl -n mongo exec -it mongodb-0 -- mongo -u root -p $PASSWORD --eval "rs.conf()" | |||
| </source> | </source> | ||
| <syntaxhighlight lang="json"> | <syntaxhighlight lang="json"> | ||
| rs0: | rs0:PRIMARY> rs.conf() | ||
| { | { | ||
| 	"_id" : "rs0", | 	"_id" : "rs0", | ||
| Line 111: | Line 111: | ||
| Update RS configuration by changing votes or priories. The below commands update <code>mongodb-1</code> priority from 0.5 to 1. The newly triggered election process does not impact the service. | Update RS configuration by changing votes or priories. The below commands update <code>mongodb-1</code> priority from 0.5 to 1. The newly triggered election process does not impact the service. | ||
| <source lang=bash> | <source lang=bash> | ||
| cfg = rs.conf() | # Execute into any of cluster nodes | ||
| cfg.members[2].priority = 1 # mongodb-1.mongodb-headless.mongo:27017 | kubectl -n mongo exec -it mongodb-0 -- mongo -u root -p $PASSWORD --eval "rs.conf()" | ||
| cfg.members[2].votes    = 1 | rs0:PRIMARY> cfg = rs.conf() | ||
| rs.reconfig(cfg)            # save the configuration | rs0:PRIMARY> cfg.members[2].priority = 1 # mongodb-1.mongodb-headless.mongo:27017 | ||
| rs0:PRIMARY> cfg.members[2].votes    = 1 | |||
| rs0:PRIMARY> rs.reconfig(cfg)            # save the configuration | |||
| </source> | </source> | ||
Revision as of 17:15, 11 March 2022
Mongo client
mongocli
mongocli is a client for connecting to MongoDB versions 5 and above.
kubectl -n mongo run --image=ubuntu:20.04 ubuntu-1 --rm -it -- bash # Install generic tools DEBIAN_FRONTEND=noninteractive apt install -yq dnsutils iproute2 iputils-ping iputils-tracepath net-tools netcat procps curl wget # Install mongo cli apt-get install gnupg -yy wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | apt-key add - touch /etc/apt/sources.list.d/mongodb-org-5.0.list echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" > /etc/apt/sources.list.d/mongodb-org-5.0.list apt-get update apt-get install -y mongocli
Operational mongo queries
rs.conf() # show current replica set config rs.status() # show current replica set status printjson(db.serverStatus()) # server status
Failover PRIMARY
We want to failover the PRIMARY mongodb-0.mongodb-headless.mongo:27017 node to mongodb-1.mongodb-headless.mongo:27017
# Print the RS (replication set) configuration kubectl -n mongo exec -it mongodb-0 -- mongo -u root -p $PASSWORD --eval "rs.conf()"
rs0:PRIMARY> rs.conf()
{
	"_id" : "rs0",
	"version" : 201111,
	"protocolVersion" : NumberLong(1),
	"writeConcernMajorityJournalDefault" : true,
	"members" : [
		{
			"_id" : 0,
			"host" : "mongodb-0.mongodb-headless.mongo:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 0.5,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 1,
			"host" : "mongodb-arbiter-0.mongodb-arbiter-headless.mongo:27017",
			"arbiterOnly" : true,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 0,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 2,
			"host" : "mongodb-1.mongodb-headless.mongo:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 0.5,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 3,
			"host" : "mongodb-2.mongodb-headless.mongo:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 0.5,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		}
	],
	"settings" : {
		"chainingAllowed" : true,
		"heartbeatIntervalMillis" : 2000,
		"heartbeatTimeoutSecs" : 10,
		"electionTimeoutMillis" : 10000,
		"catchUpTimeoutMillis" : -1,
		"catchUpTakeoverDelayMillis" : 30000,
		"getLastErrorModes" : {
			
		},
		"getLastErrorDefaults" : {
			"w" : 1,
			"wtimeout" : 0
		},
		"replicaSetId" : ObjectId("111111111122222222223333")
	}
}
Update RS configuration by changing votes or priories. The below commands update mongodb-1 priority from 0.5 to 1. The newly triggered election process does not impact the service.
# Execute into any of cluster nodes kubectl -n mongo exec -it mongodb-0 -- mongo -u root -p $PASSWORD --eval "rs.conf()" rs0:PRIMARY> cfg = rs.conf() rs0:PRIMARY> cfg.members[2].priority = 1 # mongodb-1.mongodb-headless.mongo:27017 rs0:PRIMARY> cfg.members[2].votes = 1 rs0:PRIMARY> rs.reconfig(cfg) # save the configuration