2014-03-19 17 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(更改私人信息)。如果你不熟悉,我會挖掘命令來做到這一點。

相關問題