您的位置:首页 > 数据库 > Mongodb

Mongodb集群部署以及集群维护命令

2014-12-05 23:02 274 查看
http://blog.csdn.net/luonanqin/article/details/8497860

MongoDB集群帮助手册

一、Mongodb集群架构简介

这是一种可以水平扩展的模式,在数据量很大时特给力,实际大规模应用一般会采用这种架构去构建monodb系统。

 要构建一个 MongoDB Sharding Cluster,需要三种角色:

l  Shard Server: mongod 实例,用于存储实际的数据块,实际生产环境中一个shard server角色可由几台机器组个一个relica set承担,防止主机单点故障

l  Config Server: mongod 实例,存储了整个 Cluster Metadata,其中包括 chunk 信息。

l  Route Server: mongos 实例,前端路由,客户端由此接入,且让整个集群看上去像单一数据库,前端应用可以透明使用。

1.    分别在3台机器运行一个mongod实例(称为mongod shard11,mongod shard12,mongod shard13)组织replica set1,作为cluster的shard1

2.    分别在3台机器运行一个mongod实例(称为mongod shard21,mongod shard22,mongod shard23)组织replica set2,作为cluster的shard2

3.    每台机器运行一个mongod实例,作为3个config server

4.    每台机器运行一个mongs进程,用于客户端连接

主机 

IP

端口信息

Server1

10.1.1.1

mongod shard11:27017

mongod shard12:27018

mongod config1:20000

mongs1:30000

Server2

10.1.1.2

mongod shard12:27017

mongod shard22:27018

mongod config2:20000

mongs2:30000

Server3

10.1.1.3

mongod shard13:27017

mongod shard23:27018

mongod config3:20000

mongs3:30000

二、集群配置

2.1软件准备
1.    安装monodb软件

su – mongodb

tar zxvf mongodb-linux-x86_64-1.6.2.tar

创建数据目录

根据本例sharding架构图所示,在各台sever上创建shard数据文件目录
Server1:

su – monodb

cd /monodb

mkdir -p data/shard11

mkdir -p data/shard21

Server2:

su – monodb

cd /monodb

mkdir -p data/shard11

mkdir -p data/shard22

Server3:

su – monodb

cd /monodb

mkdir -p data/shard13

mkdir -p data/shard23

2.2配置relica sets(复制集)
1.    配置shard1所用到的replica sets:

Server1:

cd /mongodb/mongodb-linux-x86_64-1.6.2/bin

./mongod –shardsvr –replSet shard1 –port 27017 –dbpath /mongodb/data/shard11 –oplogSize 100 –logpath /mongodb/data/shard11.log –logappend –fork

Server2:

cd /mongodb/mongodb-linux-x86_64-1.6.2/bin

./mongod –shardsvr –replSet shard1 –port 27017 –dbpath /mongodb/data/shard12 –oplogSize 100 –logpath /mongodb/data/shard12.log –logappend –fork

Server3:

cd /mongodb/mongodb-linux-x86_64-1.6.2/bin

./mongod –shardsvr –replSet shard1 –port 27017 –dbpath  /mongodb/data/shard13 –oplogSize 100 –logpath /mongodb/data/shard13.log –logappend –fork

初始化replica set

用mongo连接其中一个mongod,执行:

> config = {_id: ‘shard1′, members: [

                          {_id: 0, host: '10.1.1.1:27017'},

                          {_id: 1, host: '10.1.1.2:27017'},

                          {_id: 2, host: '10.1.1.3:27017'}]

           }

> rs.initiate(config);

同样方法,配置shard2用到的replica sets:
server1:

cd /mongodb/mongodb-linux-x86_64-1.6.2/bin

./mongod –shardsvr –replSet shard2 –port 27018 –dbpath /mongodb/data/shard21 –oplogSize 100 –logpath /mongodb/data/shard21.log –logappend –fork

server2:

cd /mongodb/mongodb-linux-x86_64-1.6.2/bin

./mongod –shardsvr –replSet shard2 –port 27018 –dbpath /mongodb/data/shard22 –oplogSize 100 –logpath /mongodb/data/shard22.log –logappend –fork

server3:

cd /mongodb/mongodb-linux-x86_64-1.6.2/bin

./mongod –shardsvr –replSet shard2 –port 27018 –dbpath /mongodb/data/shard23 –oplogSize 100 –logpath /mongodb/data/shard23.log –logappend –fork

初始化replica set

用mongo连接其中一个mongod,执行:

> config = {_id: ‘shard2′, members: [

                          {_id: 0, host: '10.1.1.1:27018'},

                          {_id: 1, host: '10.1.1.2:27018'},

                          {_id: 2, host: '10.1.1.3:27018'}]

           }

> rs.initiate(config);

到此就配置好了二个replica sets,也就是准备好了二个shards

2.3配置三台config server
Server1:

mkdir -p /mongodb/data/config

./mongod –configsvr –dbpath /mongodb/data/config –port 20000 –logpath /mongodb/data/config.log –logappend –fork   #config server也需要dbpath

 
Server2:
mkdir -p /mongodb/data/config

./mongod –configsvr –dbpath /mongodb/data/config –port 20000 –logpath /mongodb/data/config.log –logappend –fork

Server3:

mkdir -p /mongodb/data/config

./mongod –configsvr –dbpath /mongodb/data/config –port 20000 –logpath /mongodb/data/config.log –logappend –fork

2.4配置mongs
在server1,server2,server3上分别执行:

./mongos –configdb 10.1.1.1:20000,10.1.1.2:20000,10.1.1.3:20000 –port 30000 –chunkSize 5 –logpath /mongodb/data/mongos.log –logappend –fork 

#mongs不需要dbpath

2.5添加复制集
连接到其中一个mongos进程,并切换到admin数据库做以下配置

1. 连接到mongs,并切换到admin

./mongo 10.1.1.1:30000/admin

>db

Admin

2. 加入shards

如里shard是单台服务器,用>db.runCommand( { addshard : “<serverhostname>[:<port>]” } )这样的命令加入,如果shard是replica sets,用replicaSetName/<serverhostname>[:port][,serverhostname2[:port],…]这样的格式表示,例如本例执行:

>db.runCommand( { addshard : “shard1/10.1.1.1:27017,10.1.1.2:27017,10.1.1.3:27017″,name:”s1″,maxsize:20480} );

>db.runCommand( { addshard : “shard2/10.1.1.1:27018,10.1.1.2:27018,10.1.1.3:27018″,name:”s2″,maxsize:20480} );

注意:在添加第二个shard时,出现error:test database 已经存在的错误,这里用mongo命令连接到第二个replica set,用db.dropDatabase()命令把test数据库给删除然后就可加入

3. 可选参数

Name:用于指定每个shard的名字,不指定的话系统将自动分配

maxSize:指定各个shard可使用的最大磁盘空间,单位megabytes

4. Listing shards

>db.runCommand( { listshards : 1 } )

如果列出了以上二个你加的shards,表示shards已经配置成功

5. 

2.6数据库分片以及Collecton分片
1、激活数据库分片

命令:

> db.runCommand( { enablesharding : “<dbname>” } );

通过执行以上命令,可以让数据库跨shard,如果不执行这步,数据库只会存放在一个shard,一旦激活数据库分片,数据库中不同的collection将被存放在不同的shard上,但一个collection仍旧存放在同一个shard上,要使单个collection也分片,还需单独对collection作些操作

2、Collection分片

要使单个collection也分片存储,需要给collection指定一个分片key,通过以下命令操作:

> db.runCommand( { shardcollection : “<namespace>”,key : <shardkeypatternobject> });

注:

 a. 分片的collection系统会自动创建一个索引(也可用户提前创建好)

 b. 分片的collection只能有一个在分片key上的唯一索引,其它唯一索引不被允许

One note: a sharded collection can have only one unique index, which must exist on the shard key. No other unique indexes can exist on the collection.

2.7分片collection例子
>db.runCommand( { shardcollection : “test.c1″,key : {id: 1} } )

>for (var i = 1; i <= 200003; i++) db.c1.save({id:i,value1:”1234567890″,value2:”1234567890″,value3:”1234567890″,value4:”1234567890″});

> db.c1.stats()(该命令可以查看表的存储状态)

2.8 Mongodb运行时添加节点
1、查看复制集节点配置

./mongo 192.168.1.207:27017

 rs0:PRIMARY> rs.conf();

{

        "_id" : "rs0",

        "version" : 3,

        "members" : [

                {

                        "_id" : 0,

                        "host" : "192.168.1.207:27017"

                },

                {

                        "_id" : 1,

                        "host" : "192.168.1.207:27018"

                },

        ]

}

2、添加节点

添加新的数据目录

  mkdir –p /mongodb/data/shard4

启动节点

./mongod –shardsvr –replSet shard1 –port 27019 –dbpath  /mongodb/data/shard4 –oplogSize 100 –logpath /mongodb/data/shard4.log –logappend –fork

     连接主节点

mongo 192.168.1.207:27017

        rs0:PRIMARY> rs.isMaster();

{

        "setName" : "rs0",

        "ismaster" : true,

        "secondary" : false,

        "hosts" : [

                "192.168.1.207:27017",

                "192.168.1.207:27018"

        ],

        "primary" : "192.168.1.207:27017",

        "me" : "192.168.1.207:27018",

        "maxBsonObjectSize" : 16777216,

        "localTime" : ISODate("2013-05-22T13:04:36.501Z"),

        "ok" : 1

}

添加节点

 rs0:PRIMARY> rs.add("192.168.1.207:27019");

{ "ok" : 1 }

3、再次查看状态

 rs0:PRIMARY> rs.conf();

{

        "_id" : "rs0",

        "version" : 3,

        "members" : [

                {

                        "_id" : 0,

                        "host" : "192.168.1.207:27017"

                },

                {

                        "_id" : 1,

                        "host" : "192.168.1.207:27018"

                },

{

                        "_id" : 1,

                        "host" : "192.168.1.207:27019"

                },

]

}

    添加成功

 

2.9 Mongodb运行时移除节点
其他步骤同上,其中只是将添加命令改为移除命令

 rs0:PRIMARY> rs.remove("192.168.1.207:27019");

最后还需要关掉192.168.1.207:27019 该服务

Ps –ef | grep mongo 查找该服务

然后通过kill -9 pid 关闭服务。

 

2.10 Mongodb运行时移除分片
1、连接mongos节点 ./mongo 192.168.1.207:30000/admin

2、运行db.runCommand( { removeshard: "your_shard_name" } )

{ msg : "draining started successfully", state: "started", shard :"mongodb0", ok : 1 }

3、查看状态,我们可以反复执行上面语句查看执行状态

db.runCommand( { removeshard: "your_shard_name" } )

{ msg: "draining ongoing", state: "ongoing", remaining: { chunks: 42, dbs : 1 }, ok: 1

说明正在迁移中。

4、移除非shard数据

db.runCommand( { movePrimary: "myapp", to: "mongodb1" })

这次就不是立即返回了,需要很久,然后会返回如下:

{ "primary" : "mongodb1", "ok" : 1 }

5、面步骤都完成后,还需要再执行一次RemoveShard,清理残余数据。

db.runCommand( { removeshard: "mongodb0" } )

显示completed后,就可以安心的关闭mongod的进程了。

 

三、Mongodb问题以及解决方案

3.1问题一: mongodb启
a443
动成功但远程无法连接

     问题描述:linux环境下mongodb启动充公但远程无法连接

     解决方案:最可能的原因是调试 没有关闭iptables和selinux

               只需要执行命令:/etc/init.d/iptables stop
http://blog.csdn.net/luonanqin/article/details/8497860
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: