Common mongodb commands-automated O & M
Common mongodb commands are scripted-automated O & M writes some mongodb commands commonly used in O & M into shell scripts, greatly facilitating maintenance
1. Set replica set
#! /Bin/bash
# Mongodb enters the client and uses admin
Cd/usr/mongodb/bin
MongoDB = './mongo 192.168.1.7: 37017'
$ MongoDB <EOF
Use admin
Rsconf = {
"_ Id": "Job001 ",
"Members ":[
{
"_ Id": 0,
"Host": "192.168.1.seven: 37017"
}
]
}
Rs. initiate (rsconf)
Rs. add ("192.168.1.8: 37017 ")
Rs. add ("192.168.1.9: 37017 ")
Exit;
EOF
Note: Write the above content to the chmod 777 ***. sh file to grant the permission
In linux> sh ***. sh, you can complete the commands.
Same below
2. Modify replica set settings
#! /Bin/bash
# Mongodb enters the client and enters the primary
Cd/usr/mongodb/bin
MongoDB = './mongo 192.168.1.7: 37017'
$ MongoDB <EOF
Use Role modeljobresume
# Rs. status ()
Cfg = rs. conf ()
Cfg. members [0]. priority = 99
Cfg. members [1]. priority = 50
Cfg. members [2]. priority = 30
Rs. reconfig (cfg)
Rs. conf ()
Exit;
EOF
3. convert a table to a temporary table and copy it back. In this way, the complete log is generated again in oplog (the size of oplog must be large enough)
#! /Bin/bash
# Mongodb enters the client and enters the primary
Cd/usr/mongodb/bin
MongoDB = './mongo 192.168.1.7: 37017'
$ MongoDB <EOF
Use Role modeljobresume
Rs. remove ("192.168.1.8: 37017 ")
Rs. remove ("192.168.1.9: 37017 ")
Db. tbJobResume. renameCollection ("tbJobResumeOld ")
Db. tbJobResumeOld. copyTo ("tbJobResume ")
Rs. add ("192.168.1.8: 37017 ")
Rs. add ("192.168.1.9: 37017 ")
# Db. tbJobResumeOld. drop ()
Exit;
EOF