mongodb安装及副本集搭建

mongodb下载地址:https://www.mongodb.com/dr/fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.7.tgz/download

实验环境:172.16.10.194 centos6.5

一、mongodb搭建与管理 

[root@mongodb ~]# cd soft/

[root@mongodb soft]# ls

mongodb-linux-x86_64-rhel62-3.2.7.tgz

[root@mongodb soft]# tar xf mongodb-linux-x86_64-rhel62-3.2.7.tgz -C /usr/src/

[root@mongodb soft]# mv /usr/src/mongodb-linux-x86_64-rhel62-3.2.7/ /usr/local/mongodb

[root@mongodb ~]# mkdir -p /data/mongodb1

[root@mongodb ~]# mkdir -p /data/logs/mongodb

[root@mongodb ~]# touch /data/logs/mongodb/mongodb1.log

[root@mongodb ~]# cd /usr/local/mongodb/

[root@mongodb mongodb]# ls

bin GNU-AGPL-3.0 MPL-2 README THIRD-PARTY-NOTICES

[root@mongodb mongodb]# mkdir conf

[root@mongodb mongodb]# vim conf/mongodb1.conf

port=27017

dbpath=/data/mongodb1          #数据库文件

logpath=/data/logs/mongodb/mongodb1.log  #日志文件

logappend=true     #以追写方式写入日志

fork=true        #以守护进程方式运行

maxConns=5000   #系统文件打开数要大于这个数值

storageEngine=mmapv1  #存储引擎

[root@mongodb mongodb]# pwd

/usr/local/mongodb

[root@mongodb mongodb]# ls bin/

bsondump mongodump mongoimport mongorestore mongotop

mongo mongoexport mongooplog mongos

mongod mongofiles mongoperf mongostat

[root@mongodb mongodb]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf        //启动mongodb数据库 -f 指定配置文件

about to fork child process, waiting until server is ready for connections.

forked process: 1312

child process started successfully, parent exiting

[root@mongodb mongodb]# netstat -anpt | grep mongod

tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 1312/mongod

[root@mongodb mongodb]# ps aux | grep mongodb | grep -v "grep"

root 1268 0.0 0.1 100940 636 pts/1 S+ 15:55 0:00 tail -f mongodb1.log

root 1312 0.8 10.5 532996 51344 ? Sl 16:12 0:04 /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf

 

设置开机自启动

[root@mongodb mongodb]# vim /etc/rc.local

 

#!/bin/sh

#

# This script will be executed *after* all the other init scripts.

# You can put your own initialization stuff in here if you don't

# want to do the full Sys V style init stuff.

 

touch /var/lock/subsys/local

rm -rf /data/mongodb1/mongod.lock

/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf

 

连接数据库

[root@mongodb mongodb]# /usr/local/mongodb/bin/mongo

MongoDB shell version: 3.2.7

connecting to: test

Welcome to the MongoDB shell.

For interactive help, type "help".

For more comprehensive documentation, see

http://docs.mongodb.org/

Questions? Try the support group

http://groups.google.com/group/mongodb-user

Server has startup warnings:

2016-12-19T16:12:57.577+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

2016-12-19T16:12:57.577+0800 I CONTROL [initandlisten]

show dbs

local 0.078GB

exit

bye

 

去除报错

[root@mongodb mongodb]# echo never > /sys/kernel/mm/transparent_hugepage/enabled

[root@mongodb mongodb]# echo never > /sys/kernel/mm/transparent_hugepage/defrag

[root@mongodb mongodb]# cd

[root@mongodb ~]# vim .bash_profile

 

# .bash_profile

 

# Get the aliases and functions

if [ -f ~/.bashrc ]; then

. ~/.bashrc

fi

 

# User specific environment and startup programs

 

PATH=$PATH:$HOME/bin

 

export PATH

alias mongo=/usr/local/mongodb/bin/mongo

[root@mongodb ~]# . .bash_profile

 

 

关闭服务的三种方法

方法一:

[root@mongodb ~]# mongo

MongoDB shell version: 3.2.7

connecting to: test

Server has startup warnings:

2016-12-19T16:12:57.577+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

2016-12-19T16:12:57.577+0800 I CONTROL [initandlisten]

use admin

switched to db admin

db.shutdownServer();

server should be down...

2016-12-19T16:36:36.903+0800 I NETWORK [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed

2016-12-19T16:36:36.904+0800 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused

2016-12-19T16:36:36.904+0800 I NETWORK [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) failed failed

exit

bye

[root@mongodb ~]# netstat -anpt | grep mongod

[root@mongodb ~]#

 

方法二:

[root@mongodb ~]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf

about to fork child process, waiting until server is ready for connections.

forked process: 1403

child process started successfully, parent exiting

[root@mongodb ~]# netstat -anpt | grep mongod

tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 1403/mongod

[root@mongodb ~]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf --shutdown

killing process with pid: 1403

[root@mongodb ~]# netstat -anpt | grep mongod

[root@mongodb ~]#

 

方法三:

[root@mongodb ~]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf

about to fork child process, waiting until server is ready for connections.

forked process: 1424

[root@mongodb ~]# ps aux | grep mongod | grep -v "grep"

root 1268 0.0 0.1 100940 620 pts/1 S+ 15:55 0:00 tail -f mongodb1.log

root 1424 8.4 10.4 531972 50948 ? Sl 16:43 0:08 /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf

[root@mongodb ~]# kill -15 1424

[root@mongodb ~]# netstat -anpt | grep mongod

[root@mongodb ~]#

 

 

开启两个实例:

[root@mongodb ~]# cd /usr/local/mongodb/conf/

[root@mongodb conf]# ls

mongodb1.conf

[root@mongodb conf]# cp mongodb{1,2}.conf

[root@mongodb conf]# ls

mongodb1.conf mongodb2.conf

[root@mongodb conf]# vim mongodb2.conf

 

port=27018

dbpath=/data/mongodb2

logpath=/data/logs/mongodb/mongodb2.log

logappend=true

fork=true

maxConns=5000

storageEngine=mmapv1

[root@mongodb conf]# mkdir /data/mongodb2/

[root@mongodb conf]# touch /data/logs/mongodb/mongodb2.log

[root@mongodb conf]# cd /etc/init.d/

[root@mongodb init.d]# vim mongodb

#!/bin/bash

#mongodb 启停脚本

INSTANCE=$1

ACTION=$2

case "$ACTION" in

start)

/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/$INSTANCE.conf

;;

stop)

/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/$INSTANCE.conf --shutdown

;;

restart)

/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/$INSTANCE.conf --shutdown

/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/$INSTANCE.conf

;;

esac

[root@mongodb init.d]# chmod +x mongodb

[root@mongodb init.d]# netstat -anpt | grep mongod

tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 1545/mongod

[root@mongodb init.d]# /etc/init.d/mongodb mongodb1 stop

killing process with pid: 1545

[root@mongodb init.d]# netstat -anpt | grep mongod

[root@mongodb init.d]#

[root@mongodb init.d]# /etc/init.d/mongodb mongodb1 start

about to fork child process, waiting until server is ready for connections.

forked process: 1571

child process started successfully, parent exiting

[root@mongodb init.d]# /etc/init.d/mongodb mongodb2 start

about to fork child process, waiting until server is ready for connections.

forked process: 1587

child process started successfully, parent exiting

[root@mongodb init.d]# /etc/init.d/mongodb mongodb2 stop

killing process with pid: 1587

[root@mongodb init.d]# /etc/init.d/mongodb mongodb2 restart

There doesn't seem to be a server running with dbpath: /data/mongodb2

about to fork child process, waiting until server is ready for connections.

forked process: 1607

child process started successfully, parent exiting

[root@mongodb init.d]# netstat -anpt | grep mongod | grep -v "grep"

tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 1571/mongod

tcp 0 0 0.0.0.0:27018 0.0.0.0:* LISTEN 1607/mongod 

 

二、副本集搭建

[root@localhost ~]# cd /usr/local/mongodb/conf/

[root@localhost conf]# ls

mongodb1.conf mongodb2.conf

[root@localhost conf]# vim mongodb1.conf

port=27017

dbpath=/data/mongodb1

logpath=/data/logs/mongodb/mongodb1.log

logappend=true

fork=true

maxConns=1000

storageEngine=mmapv1

replSet=repl           //名字随意取

[root@localhost conf]# vim mongodb2.conf

port=27018

dbpath=/data/mongodb2

logpath=/data/logs/mongodb/mongodb2.log

logappend=true

fork=true

maxConns=1000

storageEngine=mmapv1

replSet=repl

 

[root@localhost conf]# vim mongodb3.conf

port=27019

dbpath=/data/mongodb3

logpath=/data/logs/mongodb/mongodb3.log

logappend=true

fork=true

maxConns=1000

storageEngine=mmapv1

replSet=repl

 

[root@localhost conf]# vim mongodb4.conf

port=27020

dbpath=/data/mongodb4

logpath=/data/logs/mongodb/mongodb4.log

logappend=true

fork=true

maxConns=1000

storageEngine=mmapv1

replSet=repl

[root@localhost conf]# mkdir /data/mongodb{3,4}

[root@localhost conf]# ls /data/

logs mongodb1 mongodb2 mongodb3 mongodb4

[root@localhost conf]# touch /data/logs/mongodb/mongodb{3,4}.log

[root@localhost conf]# ls /data/logs/mongodb/

mongodb1.log mongodb2.log mongodb3.log mongodb4.log

[root@localhost conf]# /etc/init.d/mongodb mongodb1 stop

killing process with pid: 1019

[root@localhost conf]# /etc/init.d/mongodb mongodb1 start

about to fork child process, waiting until server is ready for connections.

forked process: 1182

child process started successfully, parent exiting

[root@localhost conf]# /etc/init.d/mongodb mongodb2 start

about to fork child process, waiting until server is ready for connections.

forked process: 1203

child process started successfully, parent exiting

[root@localhost conf]# /etc/init.d/mongodb mongodb3 start

about to fork child process, waiting until server is ready for connections.

forked process: 1224

child process started successfully, parent exiting

[root@localhost conf]# /etc/init.d/mongodb mongodb4 start

about to fork child process, waiting until server is ready for connections.

forked process: 1245

child process started successfully, parent exiting

[root@localhost conf]# netstat -anpt | grep mongod

tcp 0 0 0.0.0.0:27019 0.0.0.0:* LISTEN 1224/mongod

tcp 0 0 0.0.0.0:27020 0.0.0.0:* LISTEN 1245/mongod

tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 1182/mongod

tcp 0 0 0.0.0.0:27018 0.0.0.0:* LISTEN 1203/mongod

[root@localhost conf]# mongo

MongoDB shell version: 3.2.7

connecting to: test

Server has startup warnings:

2016-12-22T12:37:45.344+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

2016-12-22T12:37:45.344+0800 I CONTROL [initandlisten]

> show dbs

2016-12-22T12:39:30.701+0800 E QUERY [thread1] Error: listDatabases failed:{ "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 } :

_getErrorWithCode@src/mongo/shell/utils.js:25:13

Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1

shellHelper.show@src/mongo/shell/utils.js:760:19

shellHelper@src/mongo/shell/utils.js:650:15

@(shellhelp2):1:1

 

> rs.status()

{

"info" : "run rs.initiate(...) if not yet done for the set",

"ok" : 0,

"errmsg" : "no replset config has been received",

"code" : 94

}

> cfg={"_id":"repl","members":[{"_id":0,"host":"172.16.10.194:27017"},{"_id":1,"host":"172.16.10.194:27018"},{"_id":2,"host":"172.16.10.194:27019"}]}

{

"_id" : "repl",

"members" : [

{

"_id" : 0,

"host" : "172.16.16.194:27017"

},

{

"_id" : 1,

"host" : "172.16.10.194:27018"

},

{

"_id" : 2,

"host" : "172.16.10.194:27019"

}

]

}

> rs.initiate(cfg)

{"ok" : 1}

 

repl:PRIMARY> rs.status()

{

"set" : "repl",

"date" : ISODate("2017-01-19T04:54:52.865Z"),

"myState" : 1,

"term" : NumberLong(1),

"heartbeatIntervalMillis" : NumberLong(2000),

"members" : [

{

"_id" : 0,

"name" : "172.16.10.194:27017",

"health" : 1,                  //1为健康,0为宕机

"state" : 1,                   //1为主,2为从

"stateStr" : "PRIMARY",

"uptime" : 1027,

"optime" : {

"ts" : Timestamp(1482382341, 1),

"t" : NumberLong(1)

},

"optimeDate" : ISODate("2017-01-19T04:52:21Z"),

"electionTime" : Timestamp(1482382336, 1),

"electionDate" : ISODate("2017-01-19T04:52:16Z"),

"configVersion" : 1,

"self" : true

},

{

"_id" : 1,

"name" : "172.16.10.194:27018",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 170,

"optime" : {

"ts" : Timestamp(1482382341, 1),

"t" : NumberLong(1)

},

"optimeDate" : ISODate("2017-01-19T04:52:21Z"),

"lastHeartbeat" : ISODate("2017-01-19T04:54:52.500Z"),

"lastHeartbeatRecv" : ISODate("2017-01-19T04:54:49.084Z"),

"pingMs" : NumberLong(0),

"configVersion" : 1

},

{

"_id" : 2,

"name" : "172.16.10.194:27019",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 170,

"optime" : {

"ts" : Timestamp(1482382341, 1),

"t" : NumberLong(1)

},

"optimeDate" : ISODate("2017-01-19T04:52:21Z"),

"lastHeartbeat" : ISODate("2017-01-19T04:54:52.500Z"),

"lastHeartbeatRecv" : ISODate("2017-01-19T04:54:49.060Z"),

"pingMs" : NumberLong(0),

"configVersion" : 1

}

],

"ok" : 1

}

repl:PRIMARY> rs.add("172.16.10.194:27020") //增加一个节点

{ "ok" : 1 }

repl:PRIMARY> rs.status()

{

"set" : "repl",

"date" : ISODate("2017-01-19T04:58:08.541Z"),

"myState" : 1,

"term" : NumberLong(1),

"heartbeatIntervalMillis" : NumberLong(2000),

"members" : [

{

"_id" : 0,

"name" : "172.16.10.194:27017",

"health" : 1,

"state" : 1,

"stateStr" : "PRIMARY",

"uptime" : 1223,

"optime" : {

"ts" : Timestamp(1482382649, 1),

"t" : NumberLong(1)

},

"optimeDate" : ISODate("2017-01-19T04:57:29Z"),

"electionTime" : Timestamp(1482382336, 1),

"electionDate" : ISODate("2017-01-19T04:52:16Z"),

"configVersion" : 2,

"self" : true

},

{

"_id" : 1,

"name" : "192.168.16.11:27018",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 366,

"optime" : {

"ts" : Timestamp(1482382649, 1),

"t" : NumberLong(1)

},

"optimeDate" : ISODate("2017-01-19T04:57:29Z"),

"lastHeartbeat" : ISODate("2017-01-19T04:58:07.434Z"),

"lastHeartbeatRecv" : ISODate("2017-01-19T04:58:08.501Z"),

"pingMs" : NumberLong(0),

"syncingTo" : "172.16.10.194:27017",

"configVersion" : 2

},

{

"_id" : 2,

"name" : "172.16.10.194:27019",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 366,

"optime" : {

"ts" : Timestamp(1482382649, 1),

"t" : NumberLong(1)

},

"optimeDate" : ISODate("2017-01-19T04:57:29Z"),

"lastHeartbeat" : ISODate("2017-01-19T04:58:07.433Z"),

"lastHeartbeatRecv" : ISODate("2017-01-19T04:58:08.501Z"),

"pingMs" : NumberLong(0),

"syncingTo" : "172.16.10.194:27017",

"configVersion" : 2

},

{

"_id" : 3,

"name" : "172.16.10.194:27020",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 39,

"optime" : {

"ts" : Timestamp(1482382649, 1),

"t" : NumberLong(1)

},

"optimeDate" : ISODate("2017-01-19T04:57:29Z"),

"lastHeartbeat" : ISODate("2017-01-19T04:58:07.412Z"),

"lastHeartbeatRecv" : ISODate("2017-01-19T04:58:04.993Z"),

"pingMs" : NumberLong(0),

"configVersion" : 2

}

],

"ok" : 1

}

repl:PRIMARY> rs.remove("172.16.10.194:27020") //删除节点

{ "ok" : 1 }

[root@localhost ~]# ps aux | grep mongodb

root 941 1.3 38.1 10880192 185740 ? Sl 04:28 0:08 /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf

root 1140 5.1 10.3 6676660 50236 ? Sl 04:37 0:01 /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb2.conf

root 1205 3.5 35.3 6686928 172072 ? Sl 04:38 0:00 /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb3.conf

root 1285 0.0 0.1 103248 872 pts/0 S+ 04:38 0:00 grep mongodb

[root@localhost ~]# kill -2 941 //模拟故障,kill -2 杀掉这个信号

[root@localhost ~]# mongo --port 27018 //登陆mongodb2.conf的配置文件查看

MongoDB shell version: 3.2.7

connecting to: 127.0.0.1:27018/test

Server has startup warnings:

2017-01-19T04:37:55.697+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

2017-01-19T04:37:55.697+0800 I CONTROL [initandlisten]

repl:PRIMARY> rs.status()

{

"set" : "repl",

"date" : ISODate("2017-01-19T20:49:04.626Z"),

"myState" : 1,

"term" : NumberLong(3),

"heartbeatIntervalMillis" : NumberLong(2000),

"members" : [

{

"_id" : 0,

"name" : "172.16.10.194:27017",

"health" : 0, //发现之前的主已经为0,代表宕机

"state" : 8,

"stateStr" : "(not reachable/healthy)",

"uptime" : 0,

"optime" : {

"ts" : Timestamp(0, 0),

"t" : NumberLong(-1)

},

"optimeDate" : ISODate("1970-01-01T00:00:00Z"),

"lastHeartbeat" : ISODate("2017-01-19T20:49:03.743Z"),

"lastHeartbeatRecv" : ISODate("2017-01-19T20:47:52.617Z"),

"pingMs" : NumberLong(0),

"lastHeartbeatMessage" : "Connection refused",

"configVersion" : -1

},

{

"_id" : 1,

"name" : "172.16.10.194:27018",

"health" : 1, //之前的从mongo会自动变成主

"state" : 1, //1代表主

"stateStr" : "PRIMARY",

"uptime" : 670,

"optime" : {

"ts" : Timestamp(1482439685, 1),

"t" : NumberLong(3)

},

"optimeDate" : ISODate("2017-01-19T20:48:05Z"),

"infoMessage" : "could not find member to sync from",

"electionTime" : Timestamp(1482439683, 1),

"electionDate" : ISODate("2017-01-19T20:48:03Z"),

"configVersion" : 3,

"self" : true

},

{

"_id" : 2,

"name" : "172.16.10.194:27019",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 649,

"optime" : {

"ts" : Timestamp(1482439685, 1),

"t" : NumberLong(3)

},

"optimeDate" : ISODate("2017-01-19T20:48:05Z"),

"lastHeartbeat" : ISODate("2017-01-19T20:49:03.643Z"),

"lastHeartbeatRecv" : ISODate("2017-01-19T20:49:03.858Z"),

"pingMs" : NumberLong(0),

"syncingTo" : "172.16.10.194:27018",

"configVersion" : 3

}

],

"ok" : 1

}

repl:PRIMARY>

[root@localhost ~]# /etc/init.d/mongodb mongodb1 start //在另一个通道重新启动mongodb1

about to fork child process, waiting until server is ready for connections.

forked process: 1317

child process started successfully, parent exiting

repl:PRIMARY> rs.status() //再次查看状态,发现mongodb1已启动,但没有主动夺回主

{

"set" : "repl",

"date" : ISODate("2017-01-19T20:52:55.850Z"),

"myState" : 1,

"term" : NumberLong(3),

"heartbeatIntervalMillis" : NumberLong(2000),

"members" : [

{

"_id" : 0,

"name" : "2017-01-19:27017",

"health" : 1,

"state" : 2, //属于从

"stateStr" : "SECONDARY",

"uptime" : 5,

"optime" : {

"ts" : Timestamp(1482439093, 1),

"t" : NumberLong(2)

},

"optimeDate" : ISODate("2017-01-19T20:38:13Z"),

"lastHeartbeat" : ISODate("2017-01-19T20:52:54.025Z"),

"lastHeartbeatRecv" : ISODate("2017-01-19T20:52:53.660Z"),

"pingMs" : NumberLong(0),

"syncingTo" : "172.16.10.194:27019",

"configVersion" : 3

},

{

"_id" : 1,

"name" : "172.16.10.194:27018",

"health" : 1,

"state" : 1, //主

"stateStr" : "PRIMARY",

"uptime" : 901,

"optime" : {

"ts" : Timestamp(1482439685, 1),

"t" : NumberLong(3)

},

"optimeDate" : ISODate("2017-01-19T20:48:05Z"),

"electionTime" : Timestamp(1482439683, 1),

"electionDate" : ISODate("2017-01-19T20:48:03Z"),

"configVersion" : 3,

"self" : true

},

{

"_id" : 2,

"name" : "172.16.10.194:27019",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 881,

"optime" : {

"ts" : Timestamp(1482439685, 1),

"t" : NumberLong(3)

},

"optimeDate" : ISODate("2017-01-19T20:48:05Z"),

"lastHeartbeat" : ISODate("2017-01-19T20:52:53.850Z"),

"lastHeartbeatRecv" : ISODate("2017-01-19T20:52:54.071Z"),

"pingMs" : NumberLong(0),

"syncingTo" : "172.16.10.194:27018",

"configVersion" : 3

}

],

"ok" : 1

}

 

 

posted on 2017-01-19 18:25  孙xiaoyu  阅读(1861)  评论(0编辑  收藏  举报