每台服务器樱笋时经有mongodb实例,私钥文件路径

条件描述:有三台ubuntu服务器,,每台服务器上曾经有mongodb实例。创造二个mongo2.4的新实例,分别作为四个复制集节点,同时保险了当前单节点环境的海东久安

规划:

3台服务器都已经有单个mongo实例运维

replSet 复制集名称: rs1

步骤

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

1.开立mongo新实例要求的目录能源   重如果数据仓库储存放目录、日志文件、配置文件、key文件(保障节点通讯)  

 
文件能够在别的地方成立,小编在/usr/local下的创始mongodb_rs作为新实例的目录,在该目录下创建能源目录

  1.数据库  mkdir db

  2.日志文件  mkdir log 

  3.配置文件  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 888 -base64>key.key
 用888毫无太长 key的长短是有限制的

好了
 近来目录能源都已经创立,先不运营。在别的二个节点重复以上进度,当然第⑥步用重新,把第①台上的key.key文件内容复制到每3个节点的key.key文件下

在每一台服务节点运维mongo的新实例(mongodb_rs目录下)  mongod -f rs.conf
稍等说话会提醒successful
 假如不成事查看rs.log日志文件查找原因,恐怕更换一下端口号

复制集成员IP与端口:

2.配置复制集 

   
选一台服务节点作为primary库192.168.1.132,在服务器上一而再mongod实例mongo
–port=29010

    1.开始化复制集  rs.initiate()  查看rs.conf()
 经常景况下生成了1个节点hostname:29010

     
 这几个hostname是主机名,把那一个主机名添加到此外2台节点的/etc/hosts文件之中

    2.添加节点  

      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)

   4.查看各种节点的气象 (当然你可以先退出 在报到查看情况)

     health:1  #寻常景况

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

STA瑞鹰TUP:刚加盟到复制集中,配置还未加载

STA景逸SUVTUP2:配置已加载完,初步化状态

 RECOVE奥迪Q3ING:正在复苏,不适用读

ARBITER: 仲裁者

DOWN:节点不可到达

UNKNOWN:未获得其它节点状态而不知是何许动静,一般爆发在唯有三个成员的架构,脑裂

REMOVED:移除复制集

ROLLBACK:数据回滚,在回滚甘休时,转移到RECOVEOdysseyING或SECONDACRUISERY状态

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

PRIMARY:主节点

SECONDAPAJEROY:备份节点

   5.为数据库创制用户名和密码
 保障程序任何境况下(主库挂了)访问叁个可信的库  如为app库创造二个账号

     use app

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

   

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

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

     1.完了了配备之后  先看看secondary是不是同步了主库的数目  
恐怕kill主库的mongo进度  等待一会儿探望是否主库发生了切换  
然后重启看主库是还是不是再一次变成了主库

节点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

5.假如要做读写分离,设置从库可读,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

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

[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

 

 

2)成立日志文件路径

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

 

3)制造主从key文件

用以标识集群的私钥的完整路径,如果每一种实例的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钦赐复制集名称为rs1,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]#

 

5)配置及早先化 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);

6)查看复制集状态

> 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>