Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Automatically install MongoDB Master, Slave, Arbiter scripts

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Found that someone plagiarized my article, unexpectedly did not write the source, think about it is also very happy, will also be plagiarized, used to be plagiarized in junior high school is to copy other people's homework. I have changed this article many times, and that guy copied the code that was a little bit bug before. I hope he will copy it again after he sees it, so as not to mislead the children.

Recently, a new project needs to use MongoDB's Master,Slave,Arbiter architecture. I went to the official website to look through the documents and wrote a rudimentary script. The script can find https://github.com/sangrealest on my github.

Make sure that dns is set up in advance, or that the hosts file is set up before installation. The firewall should make the corresponding rules, or turn off the firewall.

First generate the certificate file for mongodb, then copy it to the Master, Slave, Arbiter directory, and add it to the configuration file.

Openss rand-base64 741 > / etc/mongodb.keychmod 600 / etcUnip mongodb.keyword nounsetsource. / mongo_configfunction createConfig () {role=$1 mongoConfig= ". / mongod-$role" if [!-f "$mongoConfig"] then echo "dbpath=/var/lib/mongodb-$role" > > $mongoConfig echo "logpath=/var/log/mongodb-$role/mongodb.log" > $mongoConfig echo "logappend=true" > > $mongoConfig Echo "keyFile=$MongoKeyFile" > $mongoConfig if ["$role" = = "master"] then echo "port=$MasterMongoPort" > > $mongoConfig elif ["$role" = = "slave"] then echo "port=$SlaveMongoPort" > > $mongoConfig elif ["$role" = = "arbiter"] then echo "port=$ArbiterMongoPort" > > $mongoConfig fi echo "nohttpinterface=true" > > $mongoConfig echo "nojournal=true" > > $mongoConfig Echo "replSet=rs0" > > $mongoConfig cp $mongoConfig / etc/ chmod 644 / etc/$mongoConfig fi} function installMongoService () {echo "installing mongoserver" if [!-f "/ usr/bin/mongo"] then tar zxvf. / mongodb-linux-x86_64-2.4.9.tgz cd. / mongodb-linux-x86_64-2.4.9/bin/ cp * / usr/bin cd.. /.. Else echo "mongo server file already exist" fi cp mongod-$1.sh / etc/init.d/ chmod 755 / etc/init.d/mongod-$1.sh / etc/init.d/mongod-$1.sh start iTunes 1 until ((iTunes = "0") do / bin/cat / var/log/mongodb-$1/mongodb.log | grep "waiting for connection" iTunes? Sleep 3 echo "waiting for mongodb ready" done echo "mongodb is ready" sleep 4} function setupReplSet () {echo "setup mongodb replicationset" member= "$SlaveName:$SlaveMongoPort" echo "rs.initiate ()" | / usr/bin/mongo $MasterName:$MasterMongoPort sleep 3 echo "rs.add (\" $count\ ")" | / usr/bin/mongo $MasterName:$MasterMongoPort sleep 3 echo "rs.addArb (\" $ArbiterName:$ArbiterMongoPort\ ")" | / usr/bin/ Mongo $MasterName:$MasterMongoPort echo "rs.status ()" | / usr/bin/mongo $MasterName:$MasterMongoPort} case $1 in "master" | "Master") createConfig "master" installMongoService "master" setupReplSet ; "slave" | "Slave") createConfig "slave" installMongoService "slave";; "arbiter" | "Arbiter") createConfig "arbiter" installMongoService "arbiter";; "uninstall" | "Uninstall") echo "this will remove all mongo filescrafts!" If [- f "/ usr/bin/pkill"] then pkill-9 mongod else kill-9 `ps aux | grep mongo | grep-v grep | awk-F ""'{print $2} '``fi rm-rf / etc/mongo* rm-rf / etc/init.d/mongo* rm-rf / var/lib/mongo* rm-rf / Var/log/mongo* rm-rf / usr/bin/mongo* rm-rf / var/run/mongo* ; *) echo "Please inpute $0 Master, slave or ARBITER" exit 0; esac

$cat mongo_config SlaveIP='10.128.129.45'SlaveName='Databse-Slave'SlaveMongoPort='27017'ArbiterIP='10.128.129.46'ArbiterName='Database-Arbiter'ArbiterMongoPort='27017'MasterIP='10.128.129.44'MasterName='Database-Master'MasterMongoPort='27017'#Mongos configMongosPort='27015'#Mongo COnfig Server ConfigMongoConfigIP='127.0.0.1'MongoConfigName='Database-Config'MongoConfigDBPath='/var/lib/mongodc'MongoConfiglogpath='/var/log/mongodc'MongoConfigPort='27014'MongoKeyFile='/etc/mongodb.key'

$cat mongod-master.sh #! / bin/bash#source / etc/mongod-start.confDBLocation='/var/lib/mongodb-master/'LogFolder='/var/log/mongodb-master/'StartProcess='/usr/bin/mongod-f / etc/mongod-master'PidFile='/var/lib/mongodb-master/mongod-master.lock'uid= `id | cut-d\ (- F1 | cut-d =-f2`if [!-d "$DBLocation"]; thenmkdir $DBLocationfi if [!-d "$LogFolder"] Thenmkdir $LogFolderfi if [!-f "$PidFile"]; thentouch $PidFilefi PidFileNum= `/ bin/cat $PidFile`CurrentPid = `ps aux | grep "$StartProcess" | grep-v "grep" | awk-F "'{print $2} '`ProcessStart () {if [" x$CurrentPid "! =" x "]; then echo" mongod process already runnings! PID is $CurrentPid. " Else exec $StartProcess & sleep 2 NewPidNum= `ps aux | grep "$StartProcess" | grep-v "grep" | awk-F "'{print $2} '`echo" mongod process is running, pid is $NewPidNum "# PidFileNum= `/ bin/cat $PidFile` # if [$NewPidNum! = $PidFileNum] Then # echo "Process is runing, but pid number is not match the pid file." # echo "pid number is $NewPidNum" # fi fi} ProcessStop () {if ["x$CurrentPid" = = "x"]; then echo "mongod process already stops!" Else kill-15$ CurrentPid sleep 3 NewPidNum= `ps aux | grep "\" $StartProcess\ "| grep-v" grep "| awk-F"'{print $2} '`if ["x$NewPidNum" = = "x"]; then echo "mongod process is stop." Else kill-9$ NewPidNum if ["x$NewPidNum"! = "x"]; then echo "Can't kill mongod process, please call system administrator." Else echo "mongod is stop." Fi} ProcessStatus () {if ["x$CurrentPid"! = "x"]; then echo "mongod process is runing! PID is $CurrentPid." Else echo "mongod not running." Fi} case "$1" in start) ProcessStart;; stop) ProcessStop;; restart) ProcessStop CurrentPid='' ProcessStart;; status) ProcessStatus; *) echo $"Usage: mongod {start | stop | status | restart}" RETVAL=2;; esac

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 251

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report