简体   繁体   English

将安装在 AWS EC2 实例上的 MongoDB 的存储位置设置为 EBS 存储。 以及如何在实例停止/终止后恢复它?

[英]Set storage location of MongoDB installed on AWS EC2 Instance to EBS storage. And How do I recover It after instance is stopped/terminated?

Hello everone I am new to AWS.大家好,我是 AWS 的新手。 I have created and Instance and installed MongoDB on It.我已经创建并实例化并在其上安装了 MongoDB。 But I learned from EC2 docs that the data stored on EC2 machine destroyed once they stop or terminated.但是我从 EC2 文档中了解到,存储在 EC2 机器上的数据一旦停止或终止就会被销毁。 So I have to use EBS for the data storage.所以我必须使用 EBS 进行数据存储。

On the 4th step of creating EC2-Instance I selected the volume在创建 EC2-Instance 的第 4 步中,我选择了卷在此处输入图片说明

But I don't know how to use it.但我不知道如何使用它。 Is it automatically used by EC2-Instance to store the data in it or we have to manually do it. EC2-Instance 是自动使用它来存储数据还是我们必须手动执行它。 I know that for changing the database storage we have to change the file named mongod.conf and modify the dbpath there but I don't know what path to give there.我知道要更改数据库存储,我们必须更改名为 mongod.conf 的文件并修改那里的 dbpath,但我不知道在那里给出什么路径。 and after that also if I destroyed that instance when I will get the EBS storage back.在那之后,如果我销毁了该实例,我将收回 EBS 存储。 thanks in advance for the help.提前感谢您的帮助。

In your screenshot the EBS volume is the root volume for the instance.在您的屏幕截图中,EBS 卷是实例的根卷。 So unless you add other volumes to the instance, everything persisted to disk on that instance is going to be stored on the EBS volume.因此,除非您将其他卷添加到该实例,否则该实例上持久保存到磁盘的所有内容都将存储在 EBS 卷上。 You can pick (or create) any location to store your database.您可以选择(或创建)任何位置来存储您的数据库。

After you have mounted the EBS volume correctly as per instructions here , you can use the following steps to store your data to EBS volume.按照此处的说明正确安装 EBS 卷后,您可以使用以下步骤将数据存储到 EBS 卷。

  • Edit mongod.conf file.编辑 mongod.conf 文件。
    sudo nano /etc/mongod.conf

  • Change the line dbpath=/var/lib/mongodb to the new path storage location, eg dbpath=/data/mongodbdbpath=/var/lib/mongodb行更改为新的路径存储位置,例如dbpath=/data/mongodb

  • Update the permissions of your chosen path to allow the MongoDB user to write to it, eg chown $USER -R /home/user/data/mongodb更新您选择的路径的权限以允许 MongoDB 用户对其进行写入,例如chown $USER -R /home/user/data/mongodb
    In most cases you user is mongodb.在大多数情况下,您的用户是 mongodb。 So you can replace $USER with mongodb:mongodb所以你可以用mongodb:mongodb替换$USER

  • Restart the service重启服务
    sudo service mongod stop then sudo service mongod start sudo service mongod stop然后sudo service mongod start

Note, please be careful that if you have any data in the old location and you want to keep, you'll need to stop the MongoDB service first, manually move the files and then start the service again.请注意,请注意,如果您在旧位置有任何数据并且想要保留,则需要先停止 MongoDB 服务,手动移动文件,然后再次启动该服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM