Startup script for MongoDB using EC2 user data on a launched instance

时间:2018-06-04 17:33:50

标签: mongodb amazon-ec2 user-data

We have a MongoDb running in our r4.xLarge EC2 instance. I just found that every time, our instance shutsdown (for any reason) and is restarted, mongo doesn't start up as well.

We added a startup script as follows in the user data section for the EC2 instance.

#!/bin/bash

service mongod restart

service mongod status

We are still seeing the same behaviour as before, that the commands don't get executed and one needs to SSH into the instance to initiate a restart.

Can anyone point out any issue with the script or provide any specific insights regarding this?

Thanks

1 个答案:

答案 0 :(得分:1)

经过一些研究后我发现,由于我们在第一次启动实例时没有添加用户数据脚本,所以我不得不在#!/ bin / bash

上面添加一个多部分

脚本现在看起来像这样:

Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
sudo service mongod restart
sudo service mongod status

故事的道德,如果您忘记在实例的启动时间添加实例用户数据脚本并在事后执行,请将该部分附加到#!/ bin / bash

上方

这是因为:

  • AWS使用mime-multipart附加用户数据文件。
  • 它还使用scripts-user, always来运行文件。