生育环境下搭建mongodb复制集高可用环境,单机复制集安装

环境描述:有三台ubuntu服务器,,每台服务器阳节经有mongodb实例。成立一个mongo2.肆的新实例,分别作为多个复制集节点,同时确定保障了当下单节点环境的兴高采烈

规划:

三台服务器都早就有单个mongo实例运转

replSet 复制集名称: rs一

步骤

MongoDB数据库安装安装路线为:/usr/local/mongodb/

一.开立mongo新实例须求的目录能源   主即使数据仓库储存放目录、日志文件、配置文件、key文件(有限扶助节点通讯)  

 
文件能够在任哪儿方创立,小编在/usr/local下的创设mongodb_rs作为新实例的目录,在该目录下开创财富目录

  1.数据库  mkdir db

  二.日志文件  mkdir log 

  叁.配备文件  touch rs.conf  能够cat
/etc/mongodb.conf>rs.conf开首化,增添或涂改下边几项

port = 29010             #改动端口 先lsof -i:29010查看是或不是被占用

fork=true                   #后台demon进程运营

journal=true              #启用可相信性日志

oplogSize=2048  

smallfiles=true

replSet=test_rs         #复制集名称

keyFile=/usr/local/mongodb_rs/key.key   #复制集通信key

4.key文本  touch key.key  生成秘钥 openssl rand 88八 -base6四>key.key
 用88八永不太长 key的长度是有限量的

好了
 近来目录财富都早就创设,先不运营。在别的2个节点重复以上进度,当然第伍步用重新,把第3台上的key.key文件内容复制到每一种节点的key.key文件下

在每1台服务节点运转mongo的新实例(mongodb_rs目录下)  mongod -f rs.conf
稍等说话会唤起successful
 假设不成事查看rs.log日志文件查找原因,也许改换一下端口号

复制集成员IP与端口:

二.陈设复制集 

   
选一台服务节点作为primary库19贰.168.一.132,在服务器上一而再mongod实例mongo
–port=290拾

    壹.初步化复制集  rs.initiate()  查看rs.conf()
 不奇怪情状下生成了3个节点hostname:29010

     
 这么些hostname是主机名,把那么些主机名增多到其它2台节点的/etc/hosts文件之中

    贰.加多节点  

      rs.add(“192.168.1.133:29010”)

      rs.add(“192.168.1.134:29010”)

 
 3.装置优先级,当主节点当机之后重启的时候任然能成为primary节点,一个节点为0
 不会成为主节点

    cfg = rs.conf()

    cfg.members[0].priority=3

    cfg.members[1].priority=1

    cfg.members[0].priority=0

      重新加载

     rs.reconfg(cfg)

   四.翻看各类节点的地方 (当然你能够先退出 在登6查看情状)

     health:1  #好端端情形

    “stateStr” : “PRIMARY”   #标志主节点

STARTUP:刚加盟到复制集中,配置还未加载

STARTUP贰:配置已加载完,早先化状态

 RECOVE瑞虎ING:正在苏醒,不适用读

ARBITER: 仲裁者

DOWN:节点不可达到

UNKNOWN:未获得其余节点状态而不知是怎么样情状,壹般产生在唯有三个分子的架构,脑裂

REMOVED:移除复制集

ROLLBACK:数据回滚,在回滚截止时,转移到RECOVEHummerH贰ING或SECONDA福特ExplorerY状态

FATAL:出错。查看日志grep “replSet FATAL”寻找错原因,重新做壹道

PRIMARY:主节点

SECONDAOdysseyY:备份节点

   五.为数据库创造用户名和密码
 有限支撑程序任何情形下(主库挂了)访问三个可信的库  如为app库成立2个账号

     use app

    db.addUser(“test”,”123″)

   

节点1: localhost:28010   (默认的primary节点)

三.测试数据同步  主从切换

     一.成功了安插之后  先看看secondary是不是同步了主库的数目  
恐怕kill主库的mongo进程  等待1会儿看望是或不是主库产生了切换  
然后重启看主库是还是不是重新成为了主库

节点2: localhost:20811

4.使用pymongo连接mongodb数据库 

import pymongo

client=pymongo.MongoClient(“192.168.1.132:29010″,replicaSet=”test_rs”)

db = client[“app”]

db.authenticate(‘test’,’123′)

db.user.find()

db.connection.host  #打字与印刷当前线总指挥部是服务器

应用pymongo连接数据库之后能够测试   ,当kill主库之后
 再度打字与印刷db.connection.host看看是不是照旧上次那多少个host  
如若区别表达故障转移了

节点3: localhost:28012

五.要是要做读写分离,设置从库可读,pymongo也加强了那样的api,大家风乐趣能够看看

复制集各节点的数据文件,日志文件,私钥文件路线:

节点1: /data/data/r0  , /data/log/r0.log , /data/key/r0

节点2: /data/data/r1  , /data/log/r1.log , /data/key/r1

节点3: /data/data/r2  , /data/log/r2.log , /data/key/r2

 

$ wget
https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.2.8.tgz

–2016-07-22 11:17:12– 
https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.2.8.tgz

Resolving fastdl.mongodb.org (fastdl.mongodb.org)… 54.182.5.247,
54.182.5.9, 54.182.5.45, …

Connecting to fastdl.mongodb.org
(fastdl.mongodb.org)|54.182.5.247|:443… connected.

HTTP request sent, awaiting response… 200 OK

Length: 71943658 (69M) [application/x-gzip]

Saving to: ‘mongodb-linux-x86_64-rhel70-3.2.8.tgz’

 

100%[===============================================================================>]
71,943,658   116KB/s   in 16m 37s

 

2016-07-22 11:33:51 (70.5 KB/s) –
‘mongodb-linux-x86_64-rhel70-3.2.8.tgz’ saved [71943658/71943658]

 

$tar zxvf mongodb-linux-x86_64-rhel70-3.2.8.tgz -C /usr/local/

$mv mongodb-linux-x86_64-rhel70-3.2.8 mongodb

壹) 创设数据文件存款和储蓄路径

[root@node222 mongodb]# mkdir -p /data02/mongors/data/r0

[root@node222 mongodb]# mkdir -p /data02/mongors/data/r1

[root@node222 mongodb]# mkdir -p /data02/mongors/data/r2

 

 

二)创造日志文件路线

[root@node222 mongodb]# mkdir -p /data02/mongors/log

 

三)创设主从key文件

用以标记集群的私钥的1体化路线,若是每一种实例的key
file内容不一样等,程序将不能够正常使用

[root@node222 mongodb]# mkdir -p /data02/mongors/key

[root@node222 mongodb]# echo “this is rs1  key” >
/data02/mongors/key/r0

[root@node222 mongodb]# echo “this is rs1  key” >
/data02/mongors/key/r1

[root@node222 mongodb]# echo “this is rs1  key” >
/data02/mongors/key/r2

[root@node222 mongodb]# chmod 600 /data02/mongors/key/r*

 

4)启动3个实例

依次拉长运营参数,当中八个MongoDB实例:

[root@node222 mongodb]# /usr/local/mongodb/bin/mongod –replSet rs1
–keyFile /data02/mongors/key/r0 –fork –port 28010 –dbpath
/data02/mongors/data/r0 –logpath=/data02/mongors/log/r0.log –logappend

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

forked process: 16629

child process started successfully, parent exiting

[root@node222 mongodb]# /usr/local/mongodb/bin/mongod –replSet rs1
–keyFile /data02/mongors/key/r1 –fork –port 28011 –dbpath
/data02/mongors/data/r1 –logpath=/data02/mongors/log/r1.log –logappend

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

forked process: 16666

child process started successfully, parent exiting

[root@node222 mongodb]# /usr/local/mongodb/bin/mongod –replSet rs1
–keyFile /data02/mongors/key/r2 –fork –port 28012 –dbpath
/data02/mongors/data/r2 –logpath=/data02/mongors/log/r2.log –logappend

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

forked process: 16703

child process started successfully, parent exiting

[root@node222 mongodb]#

 

注:运行命令的参数分别为,mongod为主运行命令,
replSet钦点复制集名字为rs一,keyfile钦命公钥文件
,fork钦点运维情势为deamo后台运行;port钦定端口号,dbpath内定数据文件目录,logpath钦赐日志文件,logappend钦命错误日志为日志追加方式;

 

经过进度和端口,验证运营状态

[root@node222 mongodb]# ps -ef|grep mongo

root     16629     1  0 17:36 ?        00:00:00
/usr/local/mongodb/bin/mongod –replSet rs1 –keyFile
/data02/mongors/key/r0 –fork –port 28010 –dbpath
/data02/mongors/data/r0 –logpath=/data02/mongors/log/r0.log –logappend

root     16666     1  0 17:36 ?        00:00:00
/usr/local/mongodb/bin/mongod –replSet rs1 –keyFile
/data02/mongors/key/r1 –fork –port 28011 –dbpath
/data02/mongors/data/r1 –logpath=/data02/mongors/log/r1.log –logappend

root     16703     1  0 17:36 ?        00:00:00
/usr/local/mongodb/bin/mongod –replSet rs1 –keyFile
/data02/mongors/key/r2 –fork –port 28012 –dbpath
/data02/mongors/data/r2 –logpath=/data02/mongors/log/r2.log –logappend

root     16739 12972  0 17:37 pts/1    00:00:00 grep –color=auto mongo

 

[root@node222 mongodb]# netstat -tunlp |grep mong

tcp        0      0 0.0.0.0:28010           0.0.0.0:*              
LISTEN      16629/mongod       

tcp        0      0 0.0.0.0:28011           0.0.0.0:*              
LISTEN      16666/mongod       

tcp        0      0 0.0.0.0:28012           0.0.0.0:*              
LISTEN      16703/mongod       

[root@node222 mongodb]#

 

伍)配置及初始化 Replica Sets

登入到primary服务器上:

# /usr/local/mongodb/bin/mongo -port 28010

铺排复制集:

> config = {_id: ‘rs1’, members: [

                           {_id: 0, host:
‘localhost:28010’,priority:1},

                           {_id: 1, host: ‘localhost:28011’},

                           {_id: 2, host: ‘localhost:28012’}]

            }

开始化配置,使地方的配置生效:

>  rs.initiate(config);

陆)查看复制集状态

> rs.status()

在primary节点上查看复制集状态:

> rs.isMaster()

 

能够再primary节点举行各个添删改查等各类数据操作,别的非master节点没办法开始展览各类数据操作,也不能够倡导复制集修改命令

 

进程如下:(securecrt实行时候总是会复制一下回显)

[root@node222 mongodb]#  /usr/local/mongodb/bin/mongo -port 28010

MongoDB shell version: 3.2.8

connecting to: 127.0.0.1:28010/test

> use adminduse admind

switched to db admind

> use adminuse admin

switched to db admin

> config = {_id: ‘rs1’, members: [config = {_id: ‘rs1’, members:
[

…              {_id: 0, host:
‘localhost:28010’,priority:1},             {_id: 0, host:
‘localhost:28010’,priority:1},

…              {_id: 1, host: ‘localhost:28011’},             {_id:
1, host: ‘localhost:28011’},

…              {_id: 2, host: ‘localhost:28012’}]             {_id:
2, host: ‘localhost:28012’}]

…       }      }

{

        “_id” : “rs1”,

        “members” : [

                {

                        “_id” : 0,

                        “host” : “localhost:28010”,

                        “priority” : 1

                },

                {

                        “_id” : 1,

                        “host” : “localhost:28011”

                },

                {

                        “_id” : 2,

                        “host” : “localhost:28012”

                }

        ]

}

> rs.initiate(config);rs.initiate(config);

{ “ok” : 1 }

rs1:OTHER>

rs1:SECONDARY>

rs1:SECONDARY> rs.status()rs.status()

{

        “set” : “rs1”,

        “date” : ISODate(“2016-07-22T09:40:47.831Z”),

        “myState” : 1,

        “term” : NumberLong(1),

        “heartbeatIntervalMillis” : NumberLong(2000),

        “members” : [

                {

                        “_id” : 0,

                        “name” : “localhost:28010”,

                        “health” : 1,

                        “state” : 1,

                        “stateStr” : “PRIMARY”,

                        “uptime” : 266,

                        “optime” : {

                                “ts” : Timestamp(1469180428, 2),

                                “t” : NumberLong(1)

                        },

                        “optimeDate” : ISODate(“2016-07-22T09:40:28Z”),

                        “infoMessage” : “could not find member to sync
from”,

                        “electionTime” : Timestamp(1469180428, 1),

                        “electionDate” :
ISODate(“2016-07-22T09:40:28Z”),

                        “configVersion” : 1,

                        “self” : true

                },

                {

                        “_id” : 1,

                        “name” : “localhost:28011”,

                        “health” : 1,

                        “state” : 2,

                        “stateStr” : “SECONDARY”,

                        “uptime” : 31,

                        “optime” : {

                                “ts” : Timestamp(1469180428, 2),

                                “t” : NumberLong(1)

                        },

                        “optimeDate” : ISODate(“2016-07-22T09:40:28Z”),

                        “lastHeartbeat” :
ISODate(“2016-07-22T09:40:46.077Z”),

                        “lastHeartbeatRecv” :
ISODate(“2016-07-22T09:40:47.815Z”),

                        “pingMs” : NumberLong(0),

                        “syncingTo” : “localhost:28010”,

                        “configVersion” : 1

                },

                {

                        “_id” : 2,

                        “name” : “localhost:28012”,

                        “health” : 1,

                        “state” : 2,

                        “stateStr” : “SECONDARY”,

                        “uptime” : 31,

                        “optime” : {

                                “ts” : Timestamp(1469180428, 2),

                                “t” : NumberLong(1)

                        },

                        “optimeDate” : ISODate(“2016-07-22T09:40:28Z”),

                        “lastHeartbeat” :
ISODate(“2016-07-22T09:40:46.104Z”),

                        “lastHeartbeatRecv” :
ISODate(“2016-07-22T09:40:47.813Z”),

                        “pingMs” : NumberLong(0),

                        “syncingTo” : “localhost:28010”,

                        “configVersion” : 1

                }

        ],

        “ok” : 1

}

rs1:PRIMARY> rs.isMaster()rs.isMaster()

{

        “hosts” : [

                “localhost:28010”,

                “localhost:28011”,

                “localhost:28012”

        ],

        “setName” : “rs1”,

        “setVersion” : 1,

        “ismaster” : true,

        “secondary” : false,

        “primary” : “localhost:28010”,

        “me” : “localhost:28010”,

        “electionId” : ObjectId(“7fffffff0000000000000001”),

        “maxBsonObjectSize” : 16777216,

        “maxMessageSizeBytes” : 48000000,

        “maxWriteBatchSize” : 1000,

        “localTime” : ISODate(“2016-07-22T09:41:07.987Z”),

        “maxWireVersion” : 4,

        “minWireVersion” : 0,

        “ok” : 1

}

rs1:PRIMARY>