您好,登錄后才能下訂單哦!
發現有人抄襲我的文章,竟然不寫出處,想想也挺開心的,還會被抄襲,以前上初中的時候都是抄襲別人的作業。這篇文章我都改動過好多次了,那哥們抄襲的還是之前有點bug的代碼。望他看到后,重新抄一遍,免得誤人子弟了。
最近有一個新項目需要用到MongoDB的Master,Slave,Arbiter的架構,去官網翻了一下文檔,寫了一個簡陋的腳本。腳本可以在我的github上找到https://github.com/sangrealest。
安裝之前確保已經提前設置好dns,或者hosts文件已經設置好。防火墻做好相應的規則,或者關閉防火墻。
首先生成mongodb的證書文件,然后copy到Master, Slave, Arbiter目錄下,并添加到配置文件里面。
openss rand -base64 741 >/etc/mongodb.key chmod 600 /etc/mongodb.key
#!/bin/bash #Author:Shanker #set -x #set -o nounset source ./mongo_config function 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 i=1 until ((i=="0")) do /bin/cat /var/log/mongodb-$1/mongodb.log | grep "waiting for connection" i=$? 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 files!!!" 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 config MongosPort='27015' #Mongo COnfig Server Config MongoConfigIP='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.conf DBLocation='/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" ];then mkdir $DBLocation fi if [ ! -d "$LogFolder" ];then mkdir $LogFolder fi if [ ! -f "$PidFile" ];then touch $PidFile fi 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 runing!! 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 Stop!! " 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 fi 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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。