MongoDB作为Windows服务并设置了copySet [英] MongoDB as windows service and setting up replicaSet

查看:67
本文介绍了MongoDB作为Windows服务并设置了copySet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了MongoDB并将其设置为Windows服务.当我尝试设置plicateSet时,出现错误通常只允许对每个套接字地址(协议/网络地址/端口)使用一种.对于套接字:0.0.0.0:27017".

所以,我已经停止了Windows服务并设置了copySet.现在,repsetSet可以正常工作.但是,我没有看到Windows服务启动并运行.这是否意味着我不能同时设置copysetSet和MongoDB服务?

解决方案

您可以在Windows上同时设置副本集和MongoDB服务.由于您已经设置了副本集,因此您需要为每个副本集成员拥有一个数据目录和一个日志文件.如果要在一台计算机上运行所有副本集成员,则必须为每个副本集成员分配一个不同的端口号.提供的样本仅用于开发或功能测试.在单台计算机上设置所有副本集成员不仅会导致整体性能下降,而且还会造成单点故障.

为每个副本集成员创建一个配置文件,包括数据目录,日志文件,端口号和副本集名称.例如,我有一个由3个成员组成的副本集,一个主Mongodb运行在端口27017上,另外两个主副本,运行在端口37017上的Mongodb1和运行在端口47017上的Mongodb2.副本集名称是rs1.

这是实例Mongodb的配置文件.

# mongod.conf

# data directory
dbpath=C:\data\db

# log file
logpath=C:\mongodb-win32-i386-2.4.4\log\mongo.log

logappend=true

#port number 
port=27017

#replica set name
replSet=rs1

这是其中之一的配置文件.

# mongo.conf

# data directory
dbpath=C:\data\db2

# log file
logpath=C:\mongodb-win32-i386-2.4.4\log2\mongo.log

logappend=true

# port number
port=47017

# replica set name
replSet=rs1

以下链接提供了配置文件选项的完整列表: http://docs.mongodb.org/manual/reference/configuration-options/

将所有三个MongoDB实例添加为Windows服务.由于我未指定服务和服务的显示名称,因此MongoDB服务将使用默认的服务/服务的显示名称MongoDB

C:\mongodb-2.4.4\bin>mongod --config C:\mongodb-2.4.4\mongod.cfg --install

使用服务名称和服务显示名称安装其他两个MongoDB实例.

C:\mongodb-2.4.4\bin>mongod --config C:\mongodb-2.4.4\mongod1.cfg --serviceName MongoDB1 --serviceDisplayName MongoDB1 --install
C:\mongodb-2.4.4\bin>mongod --config C:\mongodb-2.4.4\mongod2.cfg --serviceName MongoDB2 --serviceDisplayName MongoDB2 --install

启动MongDB的所有三个实例

C:\mongodb-2.4.4\bin>net start mongodb
The Mongo DB service is starting.
The Mongo DB service was started successfully.

C:\mongodb-2.4.4\bin>net start mongodb1
The MongoDB1 service is starting.
The MongoDB1 service was started successfully.

C:\mongodb-2.4.4\bin>net start mongodb2
The MongoDB2 service is starting.
The MongoDB2 service was started successfully.

使用带查询选项的sc命令验证所有三个Windows服务的状态.

C:\mongodb-2.4.4\bin>sc query mongodb
C:\mongodb-2.4.4\bin>sc query mongodb1
C:\mongodb-2.4.4\bin>sc query mongodb2

从MongoDB Shell配置副本集.在以下示例中,端口27017上的MongoDB实例将成为主副本集成员.

C:\mongodb-2.4.4\bin>mongo --port 27017

从MongoDB Shell设置副本集配置.

> config = { _id: "rs1", members:[
... { _id : 0, host : "localhost:27017"},
... { _id : 1, host : "localhost:37017"},
... { _id : 2, host : "localhost:47017"}
... ] }

在MongoDB Shell上,初始化副本集并验证其状态.

> rs.initiate(config)
{
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}
> rs.status()
{
        "set" : "rs1",
        "date" : ISODate("2013-07-02T18:40:27Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 651,
                        "optime" : {
                                "t" : 1372790393,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2013-07-02T18:39:53Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "localhost:37017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 31,
                        "optime" : {
                                "t" : 1372790393,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2013-07-02T18:39:53Z"),
                        "lastHeartbeat" : ISODate("2013-07-02T18:40:26Z"),
                        "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
                        "pingMs" : 0,
                        "syncingTo" : "localhost:27017"
                },
                {
                        "_id" : 2,
                        "name" : "localhost:47017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 31,
                        "optime" : {
                                "t" : 1372790393,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2013-07-02T18:39:53Z"),
                        "lastHeartbeat" : ISODate("2013-07-02T18:40:26Z"),
                        "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
                        "pingMs" : 0,
                        "syncingTo" : "localhost:27017"
                }
        ],
        "ok" : 1
}
rs1:PRIMARY>

您还可以检查次要服务器的状态.在这里,我要连接到端口37017上的一个辅助端口.

C:\mongodb-2.4.4\bin>mongo --port 37017

MongoDB shell中将显示以下提示,显示辅助状态.

rs1:SECONDARY>

可以在此处找到有关部署副本集的教程: https://docs.mongodb.com/manual/tutorial/deploy-replica-设置/

I have installed MongoDB and its set up as windows service. When I try to set up replicaSet I am getting error "Only one usage of each socket address (protocol/network address/port) is normally permitted. for socket: 0.0.0.0:27017".

So, I have stopped the windows service and set up replicaSet. The replicaSet is working fine now. But, I don't see the windows service up and running. Does that means I can't set up replicaSet and MongoDB service at the same time?

解决方案

You can set up replica set and MongoDB service at the same time on Windows. Since you have already set up a replica set, you are aware that you need to have a data directory and a log file for each replica set member. If you are running all replica set members on a single machine, each replica set member must be assigned a different port number. The sample provided is for development or functional testing only. Setting up all replica set members on a single machine will constitute a single point of failure in addition to being a total performance drag.

Create a configuration file for each replica set member including data directory, log file, port number, and replica set name. For example, I have a replica set of 3 members, one primary Mongodb running on port 27017 and two secondary, Mongodb1 on port 37017, and Mongodb2 on port 47017. The replica set name is rs1.

Here is the configuration file for instance Mongodb.

# mongod.conf

# data directory
dbpath=C:\data\db

# log file
logpath=C:\mongodb-win32-i386-2.4.4\log\mongo.log

logappend=true

#port number 
port=27017

#replica set name
replSet=rs1

Here is the configuration file for one of the secondaries.

# mongo.conf

# data directory
dbpath=C:\data\db2

# log file
logpath=C:\mongodb-win32-i386-2.4.4\log2\mongo.log

logappend=true

# port number
port=47017

# replica set name
replSet=rs1

The following link provides a complete list of configuration file options: http://docs.mongodb.org/manual/reference/configuration-options/

Add all three MongoDB instances as Windows service. Since I did not specify the service and service display name, the MongoDB service will use the default service/service display name MongoDB

C:\mongodb-2.4.4\bin>mongod --config C:\mongodb-2.4.4\mongod.cfg --install

Install the other two MongoDB instances with service name and service display name.

C:\mongodb-2.4.4\bin>mongod --config C:\mongodb-2.4.4\mongod1.cfg --serviceName MongoDB1 --serviceDisplayName MongoDB1 --install
C:\mongodb-2.4.4\bin>mongod --config C:\mongodb-2.4.4\mongod2.cfg --serviceName MongoDB2 --serviceDisplayName MongoDB2 --install

Start all three instances of MongDB

C:\mongodb-2.4.4\bin>net start mongodb
The Mongo DB service is starting.
The Mongo DB service was started successfully.

C:\mongodb-2.4.4\bin>net start mongodb1
The MongoDB1 service is starting.
The MongoDB1 service was started successfully.

C:\mongodb-2.4.4\bin>net start mongodb2
The MongoDB2 service is starting.
The MongoDB2 service was started successfully.

Verify the status of all three Windows service using the sc command with query option.

C:\mongodb-2.4.4\bin>sc query mongodb
C:\mongodb-2.4.4\bin>sc query mongodb1
C:\mongodb-2.4.4\bin>sc query mongodb2

Configure replica set from MongoDB shell. In the following example, the MongoDB instance on port 27017 will be the primary replica set member.

C:\mongodb-2.4.4\bin>mongo --port 27017

Set replica set configuration from MongoDB shell.

> config = { _id: "rs1", members:[
... { _id : 0, host : "localhost:27017"},
... { _id : 1, host : "localhost:37017"},
... { _id : 2, host : "localhost:47017"}
... ] }

At MongoDB shell, initialize the replica set and verify its status.

> rs.initiate(config)
{
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}
> rs.status()
{
        "set" : "rs1",
        "date" : ISODate("2013-07-02T18:40:27Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 651,
                        "optime" : {
                                "t" : 1372790393,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2013-07-02T18:39:53Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "localhost:37017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 31,
                        "optime" : {
                                "t" : 1372790393,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2013-07-02T18:39:53Z"),
                        "lastHeartbeat" : ISODate("2013-07-02T18:40:26Z"),
                        "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
                        "pingMs" : 0,
                        "syncingTo" : "localhost:27017"
                },
                {
                        "_id" : 2,
                        "name" : "localhost:47017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 31,
                        "optime" : {
                                "t" : 1372790393,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2013-07-02T18:39:53Z"),
                        "lastHeartbeat" : ISODate("2013-07-02T18:40:26Z"),
                        "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
                        "pingMs" : 0,
                        "syncingTo" : "localhost:27017"
                }
        ],
        "ok" : 1
}
rs1:PRIMARY>

You can also check the status of the secondaries. Here I am connecting to one of the secondaries on port 37017.

C:\mongodb-2.4.4\bin>mongo --port 37017

The following prompt will present in MongoDB shell showing the secondary status.

rs1:SECONDARY>

A tutorial on deploying a replica set can be found here: https://docs.mongodb.com/manual/tutorial/deploy-replica-set/

这篇关于MongoDB作为Windows服务并设置了copySet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆