设为首页 加入收藏

TOP

Mongodb集群部署---ReplicaSet+Sharding(二)
2014-11-24 03:21:23 来源: 作者: 【 】 浏览:13
Tags:Mongodb 集群 部署 ---ReplicaSet Sharding
b/bin
./mongod -shardsvr -f shard12.conf

参数解释:

dbpath:数据存放目录

logpath:日志存放路径

pidfilepath:进程文件,方便停止mongodb

directoryperdb:为每一个数据库按照数据库名建立文件夹存放

logappend:以追加的方式记录日志

replSet:replica set的名字

bind_ip:mongodb所绑定的ip地址

port:mongodb进程所使用的端口号,默认为27017

oplogSize:mongodb操作日志文件的最大大小。单位为Mb,默认为硬盘剩余空间的5%

fork:以后台方式运行进程

noprealloc:不预先分配存储

初始化replica set

配置主,备,仲裁节点,可以通过客户端连接mongodb,也可以直接在三个节点中选择一个连接mongodb。
用mongo连接其中一个mongod,执行:

[root@localhost bin]# ./mongo 172.17.253.217:27017
MongoDB shell version: 2.4.9
connecting to: 172.17.253.217:27017/test
> use admin
switched to db admin
> config={_id:'shard1',members:[{_id:0,host:'172.17.253.216:27017',priority:2},{_id:1,host:'172.17.253.217:27017',priority:1},{_id:2,host:'172.17.253.67:27017',arbiterOnly:true}]}
{
	"_id" : "shard1",
	"members" : [
		{
			"_id" : 0,
			"host" : "172.17.253.216:27017",
			"priority" : 2
		},
		{
			"_id" : 1,
			"host" : "172.17.253.217:27017",
			"priority" : 1
		},
		{
			"_id" : 2,
			"host" : "172.17.253.67:27017",
			"arbiterOnly" : true
		}
	]
}
> rs.initiate(config)#使配置生效  

{
	"info" : "Config now saved locally.  Should come online in about a minute.",
	"ok" : 1
}

config是可以任意的名字,当然最好不要是mongodb的关键字,conf,config都可以。最外层的_id表示replica set的名字,members里包含的是所有节点的地址以及优先级。优先级最高的即成为主节点,即这里的172.17.253.216:27017。特别注意的是,对于仲裁节点,需要有个特别的配置——arbiterOnly:true。这个千万不能少了,不然主备模式就不能生效。 

配置的生效时间根据不同的机器配置会有长有短,配置不错的话基本上十几秒内就能生效,有的配置需要一两分钟。如果生效了,执行rs.status()命令会看到如下信息:

> rs.status()
{
    "set" : "shard1",
    "date" : ISODate("2014-02-13T17:39:46Z"),
    "myState" : 2,
    "members" : [
        {
            "_id" : 0,
            "name" : "172.17.253.216:27017",
            "health" : 1,
            "state" : 6,
            "stateStr" : "UNKNOWN",
            "uptime" : 42,
            "optime" : Timestamp(0, 0),
            "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
            "lastHeartbeat" : ISODate("2014-02-13T17:39:44Z"),
            "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
            "pingMs" : 1,
            "lastHeartbeatMessage" : "still initializing"
        },
        {
            "_id" : 1,
            "name" : "172.17.253.217:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 3342,
            "optime" : Timestamp(1392313137, 1),
            "optimeDate" : ISODate("2014-02-13T17:38:57Z"),
            "self" : true
        },
        {
            "_id" : 2,
            "name" : "172.17.253.67:27017",
            "health" : 1,
            "state" : 5,
            "stateStr" : "STARTUP2",
            "uptime" : 40,
            "lastHeartbeat" : ISODate("2014-02-13T17:39:44Z"),
            "lastHeartbeatRecv" : ISODate("2014-02-13T17:39:44Z"),
            "pingMs" : 0
        }
    ],
    "ok" : 1
}
> rs.initiate()
{
	"errmsg" : "exception: Can't take a write lock while out of disk space",
	"code" : 14031,
	"ok" : 0
}

我们会发现,本来应该是主库的显示unkown,仲裁库显示STARTUP2。报错:
Can't take a write lock while out of disk space
这个如何解决呢,经过百度一番之后,

将lock文件删除
rm /var/lib/mongodb/mongod.lock

最好也把journal日志删除,那玩意也很占硬盘,重启mongodb服务

在次尝试:
root@Labs06 bin]# ./mongo 172.17.253.216:27017
MongoDB shell version: 2.4.9
connecting to: 172.17.253.216:27017/test
shard1:PRIMARY> 
shard1:PRIMARY> rs.status()
{
	"set" : "shard1",
	"date" : ISODate("2014-02-13T10:53:12Z"),
	"myState" : 1,
	"members" : [
		{
			"_id" : 0,
			"name" : "172.17.253.216:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 921,
			"optime" : Timestam
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇java hibernate在postgres数据库.. 下一篇SQLite主键与RowID的关系

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Java 编程和 c 语言 (2025-12-25 08:19:48)
·. net内存管理宝典这 (2025-12-25 08:19:46)
·C++为什么不加上内存 (2025-12-25 08:19:44)
·MySQL 安装及连接-腾 (2025-12-25 06:20:28)
·MySQL的下载、安装、 (2025-12-25 06:20:26)