运行 mongoDB 在 Google Compute Engine 实例上启动时

Running mongoDB on startup on Google Compute Engine instance

我已经在 GCE 实例上设置了 Mongo 到 运行。

起初我在 / 工作并使用 sudo mongod 启动 mongo,但合理地打印了一些警告:

WARNING: You are running this process as the root user, which is not recommended.

所以我把所有东西都移到了 /home/my_google_account,我可以通过 SSH 和 运行 mongod.

进入

现在我想进行一些操作,包括 mongod、运行 在启动时 。根据我的理解,执行此操作的正确方法是使用 Google startup scripts:您可以从控制台提供如下脚本作为元数据键值对:

#! /bin/bash
apt-get update
mongod -f /home/my_google_account/mongod.conf

有效,但正如Google所说,这是作为 root 执行的:

The instance always executes startup scripts as root, and only executes those scripts after it creates any new users whose SSH keys are included in the instance metadata.

所以 mongod 进程 运行 具有 root 权限,但我仍然收到原来的警告:

WARNING: You are running this process as the root user, which is not recommended.

我该怎么办?

有没有办法让启动脚本不以 root 身份 运行? 或者我应该只在 /sudo 中工作而完全忽略警告? 似乎每个人都建议您应该在用户的主文件夹中工作,获得必要的权限,并 运行 mongod 使用您的权限。但是无论如何,启动脚本将 运行 作为 root!

试试这个 - 将用户添加到您的永久磁盘 -

useradd -ms /bin/bash mongouser
#replace /data/db with your data location
chown -R mongouser /data/db

将此添加到您的启动脚本

apt-get update
sudo mongouser -c 'mongod -f /home/my_google_account/mongod.conf'