2014-03-19 19 views
2

有人可以阐述为什么应用程序在生产与开发模式下表现不同。我已检查并重新检查config/database.yml并确保用户名和密码正确。事实上,在写这篇文章的时候,我已经设定了开发和生产数据库都是一样的。然而,当我在生产环境中运行服务器时,Mysql2抱怨访问被拒绝,但在开发环境中工作正常。Rails应用程序在开发与生产中的表现有所不同

运行时发生同样的事rails c production vs 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 

O/S:10.0 FreeBSD的64位

红宝石:2.1.0(安装在使用Rbenv)

滑轨:4.0.2

+1

显示你的'config/database.yml'。 –

+0

当然。正在编辑我的帖子以添加它并看到您的评论。完成:) –

+0

你有没有解决你的问题? –

回答

0

一个常见的问题是,DB用户权限与%设置为指本地访问权限....

...但在产品环境中,DB和Web服务器在不同的机器上,您需要将用户权限设置为来自Web服务器机器的IP,DNS等。

例如,你可能有烫发这样的:

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

但这只会为你的本地开发环境中工作。您可能会有这种权限设置脚本,在这种情况下,您的prod数据库需要不同的安装权限。

在产品环境中,您可以在168.0.1.2上使用您的网络服务,在168.0.1.100上使用您的数据库。因此,您的prod数据库将需要:

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

如果您添加其他Web服务器,请记得为来自该计算机的用户添加权限。

如果这些都没有敲响,请发布您的GRANTS(更改私人信息)。如果你不熟悉,我会挖掘命令来做到这一点。

相关问题