每台服务器上一度有mongodb实例,1) 创制数据文件存储路径

环境描述:有三台ubuntu服务器,,每台服务器上已经有mongodb实例。成立3个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的尺寸是有限量的

好了
 近年来目录资源都已经创办,先不启动。在其他2个节点重复以上进度,当然第4步用重新,把第一台上的key.key文件内容复制到每一个节点的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()
 正常情状下生成了一个节点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”   #标识主节点

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

STARTUP2:配置已加载完,初步化状态

 RECOVERING:正在苏醒,不适用读

ARBITER: 仲裁者

DOWN:节点不可到达

UNKNOWN:未获取其余节点状态而不知是什么样景况,一般发生在唯有两个分子的架构,脑裂

REMOVED:移除复制集

ROLLBACK:数据回滚,在回滚截至时,转移到RECOVERING或SECONDARY状态

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

PRIMARY:主节点

SECONDARY:备份节点

   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>