简体   繁体   English

如何在 AWS Elastic Beanstalk 上为 Rails 应用程序设置数据库种子

[英]How do I seed a database for Rails app on AWS Elastic Beanstalk

I built a small Rails app locally and am trying to deploy it to AWS Elastic Beanstalk.我在本地构建了一个小型 Rails 应用程序,并尝试将其部署到 AWS Elastic Beanstalk。

This is the guide I have been following:这是我一直遵循的指南:

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Ruby_rails.html http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Ruby_rails.html

The deployment was successful but the seeding of the database did not occur so all of my static list values are empty.部署成功,但数据库的种子没有发生,所以我所有的静态列表值都是空的。

Although I don't think it is the "ideal" solution, I did try to manually seed the database by SSHing into the EB instance.尽管我认为这不是“理想”的解决方案,但我确实尝试通过 SSH 连接到 EB 实例来手动为数据库做种。

eb ssh
cd /var/app/current/
rake db:seed

and then the result is:然后结果是:

[ec2-user@ip-172-31-13-16 current]$ rake db:seed
Rails Error: Unable to access log file. Please ensure that /var/app/current/log/production.log exists and is writable (ie, make it writable for user and group: chmod 0664 /var/app/current/log/production.log). The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
  ActiveRecord::SchemaMigration Load (0.1ms)  SELECT "schema_migrations".* FROM "schema_migrations"
   (0.1ms)  begin transaction

 ...

(0.1ms)  rollback transaction
rake aborted!
ActiveRecord::StatementInvalid: SQLite3::ReadOnlyException: attempt to write a readonly database: INSERT INTO "users" ("first_name", "last_name", "email", "encrypted_password", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?)
/var/app/current/db/seeds.rb:9:in `<top (required)>'
SQLite3::ReadOnlyException: attempt to write a readonly database
/var/app/current/db/seeds.rb:9:in `<top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

What is the proper way to do this?这样做的正确方法是什么?

Thanks in advance!提前致谢!

Both errors ( Unable to access log file and attempt to write a readonly database ) are due to file access permissions.这两个错误( Unable to access log fileattempt to write a readonly database )都是由于文件访问权限造成的。

When you ssh into an EC2 instance, you usually log in as a user who doesn't have write access to the capistrano deploy directory.当您通过 ssh 连接到 EC2 实例时,您通常以对 capistrano 部署目录没有写访问权限的用户身份登录。 (You have read access, so you can see the files, but not write - so you can't write logs or create a new sqlite database file). (您具有读取权限,因此您可以查看文件,但不能写入 - 因此您无法写入日志或创建新的 sqlite 数据库文件)。

You can use sudo to run rake as another user:您可以使用 sudo 以另一个用户身份运行 rake:

sudo -u app env PATH=$PATH RAILS_ENV=production bundle exec rake db:seed

(Change "-u app" to whatever username your app runs as - or leave it out just to run the command as root) (将“-u app”更改为您的应用程序运行的任何用户名 - 或者仅以root身份运行命令而将其省略)

Try RAILS_ENV=production bundle exec rake db:seed尝试RAILS_ENV=production bundle exec rake db:seed

it executes the rake script with the command db:seed in the context of the current bundle.它在当前包的上下文中使用命令 db:seed 执行 rake 脚本。

In case you're here and the above solutions didn't work for you.如果您在这里并且上述解决方案对您不起作用。

Apart from using the command provided in this answer above by benchwarmer:除了使用 benchwarmer 在上面这个答案中提供的命令之外:

https://stackoverflow.com/a/17232607/1216245 https://stackoverflow.com/a/17232607/1216245

I had to run the seed command providing env vars for the master key and all rds settings.我必须运行为主密钥和所有 rds 设置提供环境变量的种子命令。

bundle exec rake db:seed RAILS_ENV=production RAILS_MASTER_KEY=<your master key> RDS_HOSTNAME=<your rds hostname> RDS_PASSWORD=<...> RDS_USERNAME=<...> RDS_DB_NAME=<...> RDS_PORT=<...>

And it worked, finally :)它终于奏效了:)

You can check all this in the Configuration panel for your environment in the AWS console (dashboard).您可以在 AWS 控制台(仪表板)中针对您的环境在配置面板中检查所有这些。

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

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