ITPub博客

首页 > 数据库 > NoSQL > mongodb集群搭建

mongodb集群搭建

原创 NoSQL 作者:celestezhang 时间:2015-08-20 14:32:18 0 删除 编辑

2.  禁用disable Transparent Huge Pages(THP) 三种配置方式

mongodb官网:THP在数据库工作负载下表现不佳,往往会有稀疏而不连续的内存访问模式。必须禁用THP对Linux机器运行使用,以确保MongoDB最佳的性能。

操作步骤:

修改/etc/grub.conf

在kernel后增加transparent_hugepage=never

        kernel /vmlinuz-2.6.32-220.el6.x86_64 ro root=/dev/mapper/vg_odsmongodb1-lv_root rd_LVM_LV=vg_odsmongodb1/lv_swap rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD quiet SYSFONT=latarcyrheb-sun16

 rhgb crashkernel=auto rd_LVM_LV=vg_odsmongodb1/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM transparent_hugepage=never

MongoDB Replica Set部署步骤

Replica Set使用的是n个mongod节点,构建具备自动的容错功能(auto-failover),自动恢复的(auto-recovery)的高可用方案。

使用Replica Set来实现读写分离。通过在连接时指定或者在主库指定slaveOk,由Secondary来分担读的压力,Primary只承担写操作。

对于Replica Set中的secondary 节点默认是不可读的。

mongodb集群的搭建

172.17.5.51  primary

172.17.5.52  secondary

172.17.5.53  arbiter

1创建安装mongodb的目录

mkdir -p /data/mongodb

2

[root@QA-5-51 local]# tar -zxvf mongodb-linux-x86_64-rhel62-3.0.5.tgz -C /data/mongodb/

如果报以下的错误是解压的时候出现的问题

[root@QA-5-51 local]# cd /data/mongodb/

[root@QA-5-51 mongodb]# mv mongodb-linux-x86_64-rhel62-3.0.5/ mongodb/

 [root@QA-5-51 mongodb]# mkdir -p /data/mongodb/data

[root@QA-5-51 mongodb]# mkdir -p /data/mongodb/log

[root@QA-5-51 mongodb]# iptables -F

[root@QA-5-51 mongodb]# iptables –L

[root@QA-5-51 bin]# pwd

/data/mongodb/mongodb/bin

[root@QA-5-44 bin]# cat mongodb.conf

port=27017

dbpath=/data/mongodb/data

logpath=/data/mongodb/log/mongodb.log

logappend=true

fork=true

maxConns=5000

replSet=shared1

[root@QA-5-51 bin]# numactl --interleave=all /data/mongodb/mongodb/bin/mongod --config /data/mongodb/mongodb/bin/mongodb.conf

vi /etc/rc.d/rc.local

numactl --interleave=all /data/mongodb/mongodb/bin/mongod --config /data/mongodb/mongodb/bin/mongodb.conf

[root@QA-5-50 bin]# yum install open*

[root@QA-5-51 bin]# ./mongod --replSet replsettest --dbpath /data/mongodb/data --oplogSize 100 --logpath /data/mongodb/log/mongodb.log --logappend –fork

 

 

 

 

config={_id:"shared1",members:[   

 

{_id:0,host:"172.17.5.51:27017"},  

 

{_id:1,host:"172.17.5.52:27017"},  

 

{_id:2,host:"172.17.5.53:27017",arbiterOnly:true}]}

> config={_id:"shared1",members:[    config={_id:"shared1",members:[   

...

... {_id:0,host:"172.17.5.51:27017"},   {_id:0,host:"172.17.5.51:27017"},  

...

... {_id:1,host:"172.17.5.52:27017"},   {_id:1,host:"172.17.5.52:27017"},  

...

... {_id:2,host:"172.17.5.53:27017",arbiterOnly:true}]}   {_id:2,host:"172.17.5.53:27017",arbiterOnly:true}]}  

{

        "_id" : "shared1",

        "members" : [

                {

                        "_id" : 0,

                        "host" : "172.17.5.51:27017"

                },

                {

                        "_id" : 1,

                        "host" : "172.17.5.52:27017"

                },

                {

                        "_id" : 2,

                        "host" : "172.17.5.53:27017",

                        "arbiterOnly" : true

                }

        ]

}

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

{ "ok" : 1 }

 

 

 

shared1:PRIMARY> rs.conf()rs.conf()  //查看配置内容

{

        "_id" : "shared1",

        "version" : 1,

        "members" : [

                {

                        "_id" : 0,

                        "host" : "172.17.5.51:27017",

                        "arbiterOnly" : false,

                        "buildIndexes" : true,

                        "hidden" : false,

                        "priority" : 1,

                        "tags" : {

 

                        },

                        "slaveDelay" : 0,

                        "votes" : 1

                },

                {

                        "_id" : 1,

                        "host" : "172.17.5.52:27017",

                        "arbiterOnly" : false,

                        "buildIndexes" : true,

                        "hidden" : false,

                        "priority" : 1,

                        "tags" : {

 

                        },

                        "slaveDelay" : 0,

                        "votes" : 1

                },

                {

                        "_id" : 2,

                        "host" : "172.17.5.53:27017",

                        "arbiterOnly" : true,

                        "buildIndexes" : true,

                        "hidden" : false,

                        "priority" : 1,

                        "tags" : {

 

                        },

                        "slaveDelay" : 0,

                        "votes" : 1

                }

        ],

        "settings" : {

                "chainingAllowed" : true,

                "heartbeatTimeoutSecs" : 10,

                "getLastErrorModes" : {

 

                },

                "getLastErrorDefaults" : {

                        "w" : 1,

                        "wtimeout" : 0

                }

        }

}

验证集群

shared1:PRIMARY> db.test.insert({"name":"cathy","age":24}) db.test.insert({"name":"cathy","age":24})

 

WriteResult({ "nInserted" : 1 })

shared1:PRIMARY>

shared1:PRIMARY> db.test.find()db.test.find()

{ "_id" : ObjectId("55d54911e7311a57758fb214"), "name" : "cathy", "age" :

从库查看

shared1:SECONDARY> db.test.find()db.test.find()

Error: error: { "$err" : "not master and slaveOk=false", "code" : 13435 }

shared1:SECONDARY>

shared1:SECONDARY> db.gerdb.gedb.ge

test.ge

shared1:SECONDARY> db.getMongo().setSlaveOk()db.getMongo().setSlaveOk()

shared1:SECONDARY> db.test.find()db.test.find()

{ "_id" : ObjectId("55d54911e7311a57758fb214"), "name" : "cathy", "age" : 24 }

第三个节点查看

shared1:ARBITER> db.test.find()db.test.find()

Error: error: { "$err" : "not master and slaveOk=false", "code" : 13435 }

shared1:ARBITER>

 

测试故障切换

[root@QA-5-51 bin]# ./mongo --port 27017

MongoDB shell version: 3.0.5

connecting to: 127.0.0.1:27017/test

Server has startup warnings:

2015-08-19T22:50:09.064-0400 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

2015-08-19T22:50:09.064-0400 I CONTROL  [initandlisten]

2015-08-19T22:50:09.064-0400 I CONTROL  [initandlisten]

2015-08-19T22:50:09.064-0400 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.

2015-08-19T22:50:09.064-0400 I CONTROL  [initandlisten] **        We suggest setting it to 'never'

2015-08-19T22:50:09.064-0400 I CONTROL  [initandlisten]

shared1:PRIMARY> use adminuse admin

switched to db admin

shared1:PRIMARY> db.shutdownServer()db.shutdownServer()

shared1:PRIMARY> rs.status()rs.status()

{

        "set" : "shared1",

        "date" : ISODate("2015-08-20T03:37:34.569Z"),

        "myState" : 1,

        "members" : [

                {

                        "_id" : 0,

                        "name" : "172.17.5.51:27017",

                        "health" : 0,

                        "state" : 8,

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

                        "uptime" : 0,

                        "optime" : Timestamp(0, 0),

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

                        "lastHeartbeat" : ISODate("2015-08-20T03:37:33.999Z"),

                        "lastHeartbeatRecv" : ISODate("2015-08-20T03:34:57.770Z"),

                        "pingMs" : 0,

                        "lastHeartbeatMessage" : "Failed attempt to connect to 172.17.5.51:27017; couldn't connect to server 172.17.5.51:27017 (172.17.5.51), connection attempt failed",

                        "configVersion" : -1

                },

                {

                        "_id" : 1,

                        "name" : "172.17.5.52:27017",

                        "health" : 1,

                        "state" : 1,

                        "stateStr" : "PRIMARY",

                        "uptime" : 2452,

                        "optime" : Timestamp(1440041233, 2),

                        "optimeDate" : ISODate("2015-08-20T03:27:13Z"),

                        "electionTime" : Timestamp(1440041699, 1),

                        "electionDate" : ISODate("2015-08-20T03:34:59Z"),

                        "configVersion" : 1,

                        "self" : true

                },

                {

                        "_id" : 2,

                        "name" : "172.17.5.53:27017",

                        "health" : 1,

                        "state" : 7,

                        "stateStr" : "ARBITER",

                        "uptime" : 1435,

                        "lastHeartbeat" : ISODate("2015-08-20T03:37:33.841Z"),

                        "lastHeartbeatRecv" : ISODate("2015-08-20T03:37:33.842Z"),

                        "pingMs" : 0,

                        "configVersion" : 1

                }

        ],

        "ok" : 1

}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29898569/viewspace-1777509/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: MongoDB集群部署
请登录后发表评论 登录
全部评论

注册时间:2014-09-19

  • 博文量
    39
  • 访问量
    125985