谁能解释一下为什么应用程序在生产模式和开发模式下的行为不同。我检查并重新检查了 config/database.yml
并确保用户名和密码正确。事实上,在撰写本文时,我已将开发和生产数据库设置为相同。然而,当我在生产环境中运行服务器时,Mysql2 提示访问被拒绝,但在开发环境中运行良好。
运行 rails c production
与 rails c development
时会发生同样的事情,开发中没有错误,但 Mysql2 访问在生产中被拒绝。
生产模式
$ 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)):
开发模式
$ 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)
这是我的 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
操作系统:FreeBSD 10.0 64 位
Ruby:2.1.0(使用 Rbenv 安装)
导轨:4.0.2
最佳答案
一个常见的问题是数据库用户权限设置为 %
以引用本地访问权限....
...但是在生产环境中,DB 和 Web 服务器在不同的机器上,您需要将用户权限设置为来自 Web 服务器机器的 IP、DNS 等。
例如,您可能有这样的烫发:
grant all privileges on mydb.* to myuser@'%' identified by 'mypasswd';
grant all privileges on mydb.* to myuser@localhost identified by 'mypasswd';
但这只适用于您本地的开发环境。您可能已经编写了这种权限设置脚本,在这种情况下,您需要为您的生产数据库设置不同的权限。
在生产环境中,您的 Web 服务可能位于 168.0.1.2
上,您的数据库位于 168.0.1.100
上。所以您的产品数据库需要:
grant all privileges on mydb.* to myuser@168.0.1.2 identified by 'mypasswd';
如果您添加另一台网络服务器,请记住为来自该机器的用户添加权限。
如果这些都不是响铃,请发布您的 GRANTS(更改私有(private)详细信息)。如果您不熟悉,我会找出执行此操作的命令。
关于ruby - Rails 应用程序在开发与生产中的行为不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22506779/