Anand Shah
Anand Shah

Reputation: 14913

Rails app behaving differently in development vs production

Can someone please shed some light on why the app is behaving differently in production vs development mode. I have checked and re-checked config/database.yml and ensured that the username and password are correct. In fact as of writing this I have set both, development and production database to be same. Yet, when I run the server in production environment, Mysql2 complains about access denied, but works fine in development environment.

Same thing happens when running rails c production vs rails c development , no error in development but Mysql2 access denied in production.

Production mode

$ rails s -e production
=> Booting WEBrick
=> Rails 4.0.2 application starting in production on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2014-03-19 18:20:22] INFO  WEBrick 1.3.1
[2014-03-19 18:20:22] INFO  ruby 2.1.0 (2013-12-25) [x86_64-freebsd10.0]
[2014-03-19 18:20:22] INFO  WEBrick::HTTPServer#start: pid=10800 port=3000
I, [2014-03-19T18:20:30.569167 #10800]  INFO -- : Started GET "/" for 192.168.1.102 at 2014-03-19 18:20:30 +0200
F, [2014-03-19T18:20:30.709229 #10800] FATAL -- :
Mysql2::Error (Access denied for user 'root'@'localhost' (using password: YES)):

Development Mode

$ rails s -e development
=> Booting WEBrick
=> Rails 4.0.2 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2014-03-19 18:22:53] INFO  WEBrick 1.3.1
[2014-03-19 18:22:53] INFO  ruby 2.1.0 (2013-12-25) [x86_64-freebsd10.0]
[2014-03-19 18:22:53] INFO  WEBrick::HTTPServer#start: pid=10898 port=3000    

Started GET "/" for 192.168.1.102 at 2014-03-19 18:23:03 +0200
Processing by Rails::WelcomeController#index as HTML
  Rendered /home/user/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.0.2/lib/rails/templates/rails/welcome/index.html.erb (2.3ms)
Completed 200 OK in 24ms (Views: 11.6ms | ActiveRecord: 0.0ms)

And here is my config/database.yml.

development:
  adapter: mysql2
  encoding: utf8
  database: amo
  pool: 5
  username: root
  password: mypass
  host: localhost

production:
  adapter: mysql2
  encoding: utf8
  database: amo
  pool: 5
  username: root
  password: mypass
  host: localhost

O/S : FreeBSD 10.0 64bit

Ruby : 2.1.0 (installed using Rbenv)

Rails : 4.0.2

Upvotes: 2

Views: 732

Answers (1)

New Alexandria
New Alexandria

Reputation: 7334

A common problem is that the DB user permissions are setup with % to refer to local access permissions....

...but in the prod environment, where the DB and web servers are on different machines, you need to set the user permission to come from the IP, DNS, etc of the web server machine.

For instance, you may have perms like this:

grant all privileges on mydb.* to myuser@'%' identified by 'mypasswd';
grant all privileges on mydb.* to myuser@localhost identified by 'mypasswd';

But this will only work for your local dev environment. You may have this kind of permission setup scripted, in which case you'd need different setup perms for your prod DB.

In the prod environment, you may have your web service on 168.0.1.2 and your DB on 168.0.1.100. So your prod DB would need:

grant all privileges on mydb.* to [email protected] identified by 'mypasswd';

If you add another web server, remember to add permissions for users coming from that machine.

If none of this is ringing a bell, post your GRANTS (change the private details). I'll dig out the commands to do this, if you aren't familiar.

Upvotes: 0

Related Questions