在OSX上测试mongo复制

Mik*_*e T 5 mongodb

在两个不同的终端,我启动mongod:

mongod --dbpath 1 --port 27001 --smallfiles --oplogSize 50 --logpath log.1 --replSet test
mongod --dbpath 2 --port 27002 --smallfiles --oplogSize 50 --logpath log.2 --replSet test
Run Code Online (Sandbox Code Playgroud)

(已创建数据子目录1和2).

我进入第三个窗口并对其中一个mongod实例运行mongo shell

mongo --port 27001
Run Code Online (Sandbox Code Playgroud)

在shell中,我将配置设置为

cfg = { _id: "test", members: [ {_id:0, host: "localhost:27001"}, {_id:1, host:"localhost:27002"} ] }
Run Code Online (Sandbox Code Playgroud)

然后运行:

use admin
rs.initiate(cfg)
Run Code Online (Sandbox Code Playgroud)

shell的响应是:

{
"startupStatus" : 4,
"info" : "hotest",
"errmsg" : "all members and seeds must be reachable to initiate set",
"ok" : 0
}
Run Code Online (Sandbox Code Playgroud)

例如,端口27001上的日志文件报告:

Sun Nov 18 18:32:56 [rsStart] trying to contact macbookpro.local:27001
Sun Nov 18 18:32:56 [rsStart] couldn't connect to macbookpro.local:27001: couldn't connect to server macbookpro.local:27001
Sun Nov 18 18:32:56 [rsStart] replSet can't get local.system.replset config from self or any seed (yet)
Run Code Online (Sandbox Code Playgroud)

似乎rs.initiate(cfg)无法连接到端口27001上的mongod实例.但是当我在没有--replSet的情况下运行mongod时,mongo shell连接就好了.

目前尚不清楚我搞砸了哪里,但我敢打赌这很明显.

Mik*_*e T 11

找到了!

事实证明,当使用brew在OSX上安装MongoDB时,会在/usr/local/etc/mongo.conf中创建配置文件.在这个文件中有一行:

#仅接受本地连接bind_ip = 127.0.0.1

好吧,如果你看日志rsStart错误是:

Sun Nov 18 18:32:56 [rsStart] trying to contact macbookpro.local:27001
Sun Nov 18 18:32:56 [rsStart] couldn't connect to macbookpro.local:27001: couldn't connect to server macbookpro.local:27001
Sun Nov 18 18:32:56 [rsStart] replSet can't get local.system.replset config from self or any seed (yet)
Run Code Online (Sandbox Code Playgroud)

如果我ping macbookpro.local,返回的IP地址是192.168.41.1.192.168.41.1 <> 127.0.0.1所以mongod拒绝回复.从配置中删除此行可以成功设置复制.

啧.我是个白痴.看了几个小时,并一直认为连接到网络时的机器名称是127.0.0.1.让这成为你的一课 - 不要老去!