我可以通过以下方式设置 mongodb 副本集:
mongo --port 27020 < config.js
config.js
javascript配置文件在哪里:
config = {
_id: "rsRunly",
members: [{
_id: 0,
host: "o502:27018"
}, {
_id: 1,
host: "o502:27019"
}, {
_id: 3,
host: "o502:27021"
}]
};
rs.initiate(config);
rs.status();
但不能内联:
mongo --port 27020 < 'config = {_id: "rsRunly", members: [ {_id: 0, host: "o502:#27020"}, {_id: 1, host: "o502:27019"}, {_id: 3, host: "o502:27021"}]}; rs.initiate(config); rs.status();';
得到错误:
2014-08-05T06:48:34.218-0400 SyntaxError: Unexpected identifier at (connect)
exception: connect failed
我尝试使用--eval
mongo --port 27020 --eval 'config = {_id: "rsRunly", members: [ {_id: 0, host: "o502:27020"}, {_id: 1, host: "o502:27019"}, {_id: 3, host: "o502:27021"}]}; rs.initiate(config); rs.status();'
它给了我输出:
MongoDB shell version: 2.6.3
connecting to: 127.0.0.1:27020/test
[object Object]
但是没有创建副本集。
为什么我需要内联创建副本集配置?因为我正在创建一个大sh
文件,首先启动所有 mongo 节点,然后启动副本集。我在那里使用 bash 变量,例如$port1
, $port2
,$port3
以保持 bash 代码干燥。因此,如果我要使用而不是内联配置,我必须在我的文件和文件config.js
中编辑端口号 2 次。sh
config.js
那么如何在config.js
文件中而不是使用 bash 命令启动副本集呢?