无法通过脚本运行mongo

时间:2018-10-11 09:05:22

标签: mongodb bash shell

在安装mongodb之后,我试图通过脚本启动mongo,但是它引发了错误,但是当我从终端尝试“ mongo”时,它运行良好。

从脚本运行mongo时出错:

MongoDB shell version: 3.2.21
connecting to: test
2018-10-11T10:38:32.855+0200 W NETWORK  [thread1] Failed to connect to 
127.0.0.1:27017, in(checking socket for error after poll), reason: 
errno:111 Connection refused

2018-10-11T10:38:32.877+0200 E QUERY    [thread1] Error: couldn't 
connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:229:14

@(connect):1:6

exception: connect failed

这是我脚本的一部分

apt-get install -y mongodb-org

systemctl enable mongod

systemctl start mongod

echo " " >> /etc/mongod.conf
echo "replication:" >> /etc/mongod.conf
echo "   oplogSizeMB: 1" >> /etc/mongod.conf
echo "   replSetName: rs0" >> /etc/mongod.conf

sed '24d' /etc/mongod.conf

systemctl restart mongod

export LC_ALL=C

mongo --eval "rs.initiate()"

但是当我只在终端上运行“ mongo”命令时,它就起作用了:

# mongo
MongoDB shell version: 3.2.21
connecting to: test

rs0:OTHER>

请问可能是什么问题?

1 个答案:

答案 0 :(得分:0)

尝试以下行:

/bin/bash -c "mongo --eval \"rs.initiate()\""