2012-02-17 45 views
0

我一直在新的rails應用程序中使用mysql,但現在我想給mongoDB一個嘗試,所以我安裝了mongo mapper和mongoid(使用mongo會話)。安裝似乎很好,因爲我可以創建mongo模型。但由於某些原因,rails仍然試圖連接到mysql:Can't connect to local MySQL server爲什麼rails試圖連接到mysql?

這太可怕了,因爲即使我沒有使用mongo,rails也不應該嘗試連接到每個請求的mysql。即使對於不存在的網址,它也會拋出該錯誤。

我能做些什麼來調試呢?我想我可以嘗試從Gemfile中刪除mysql gem並運行bundle install。但我仍然不喜歡即使在不使用它時也試圖連接的事實。它不應該試圖連接'懶惰'(即:只在需要時)?

development.rb:

Myapp::Application.configure do 
    # Settings specified here will take precedence over those in config/application.rb 

    # In the development environment your application's code is reloaded on 
    # every request. This slows down response time but is perfect for development 
    # since you don't have to restart the web server when you make code changes. 
    config.cache_classes = false 

    # Log error messages when you accidentally call methods on nil. 
    config.whiny_nils = true 

    # Show full error reports and disable caching 
    config.consider_all_requests_local  = true 
    config.action_controller.perform_caching = false 

    # Don't care if the mailer can't send 
    config.action_mailer.raise_delivery_errors = false 

    # Print deprecation notices to the Rails logger 
    config.active_support.deprecation = :log 

    # Only use best-standards-support built into browsers 
    config.action_dispatch.best_standards_support = :builtin 

    # Raise exception on mass assignment protection for Active Record models 
    config.active_record.mass_assignment_sanitizer = :strict 

    # Log the query plan for queries taking more than this (works 
    # with SQLite, MySQL, and PostgreSQL) 
    config.active_record.auto_explain_threshold_in_seconds = 0.5 

    # Do not compress assets 
    config.assets.compress = false 

    # Expands the lines which load the assets 
    config.assets.debug = true 
end 

application.rb中:

require File.expand_path('../boot', __FILE__) 

require 'rails/all' 

if defined?(Bundler) 
    # If you precompile assets before deploying to production, use this line 
    Bundler.require(*Rails.groups(:assets => %w(development test))) 
    # If you want your assets lazily compiled in production, use this line 
    # Bundler.require(:default, :assets, Rails.env) 
end 

module Myapp 
    class Application < Rails::Application 
# Settings in config/environments/* take precedence over those specified here. 
# Application configuration should go into files in config/initializers 
# -- all .rb files in that directory are automatically loaded. 

# Custom directories with classes and modules you want to be autoloadable. 
# config.autoload_paths += %W(#{config.root}/extras) 

# Only load the plugins named here, in the order given (default is alphabetical). 
# :all can be used as a placeholder for all plugins not explicitly named. 
# config.plugins = [ :exception_notification, :ssl_requirement, :all ] 

# Activate observers that should always be running. 
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer 

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. 
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. 
# config.time_zone = 'Central Time (US & Canada)' 

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. 
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] 
# config.i18n.default_locale = :de 

# Configure the default encoding used in templates for Ruby 1.9. 
config.encoding = "utf-8" 

# Configure sensitive parameters which will be filtered from the log file. 
config.filter_parameters += [:password] 

# Use SQL instead of Active Record's schema dumper when creating the database. 
# This is necessary if your schema can't be completely dumped by the schema dumper, 
# like if you have constraints or database-specific column types 
# config.active_record.schema_format = :sql 

# Enforce whitelist mode for mass assignment. 
# This will create an empty whitelist of attributes available for mass-assignment for all models 
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible 
# parameters by using an attr_accessible or attr_protected declaration. 
# config.active_record.whitelist_attributes = true 

# Enable the asset pipeline 
config.assets.enabled = true 

# Version of your assets, change this if you want to expire all your assets 
config.assets.version = '1.0' 

config.generators do |g| 
    g.orm :mongo_mapper 
end 
    end 
end 
+0

向我們顯示您的配置 – Joe 2012-02-17 10:51:15

回答

3

當ActiveRecord是應用程序的一部分時,它會嘗試在啓動時建立與數據庫的連接。如果無法連接,應用程序將無法啓動。

的問題是在這裏:

require 'rails/all' 

此行包括所有的 「通常」 其中導軌組件,ActiveRecord的。如果你去它的定義,它應該像這樣(爲Rails 3.2):

require "rails" 

%w(
    active_record 
    action_controller 
    action_mailer 
    active_resource 
    rails/test_unit 
    sprockets 
).each do |framework| 
    begin 
    require "#{framework}/railtie" 
    rescue LoadError 
    end 
end 

把這個代碼,刪除active_record線,並把它而不是你的rails/all線。現在,不包括ActiveRecord的,當它看到在代碼中的ActiveRecord引用您的應用程序會大聲失敗,就像這樣:

config.active_record.mass_assignment_sanitizer = :strict 

你需要將它刪除這些。你不需要刪除database.yml,但你可能應該,因爲它現在沒有意義。

+0

給讀者:這是答案,雖然我不知道代碼是否工作,因爲我剛剛重新安裝了應用程序--skip-active-record(它產生了類似於上面的代碼) 。但是我仍然想知道活動記錄自動連接到mysql的原因是什麼,即使我沒有在特定的請求中使用它,這似乎是遲緩的。我對Ruby和Rails很陌生,但我仍然不知道它們的內部結構。 – HappyDeveloper 2012-02-17 12:24:50

+0

@HappyDeveloper:ActiveRecord中沒有「懶連接」。它在初始化期間連接。這就是它的工作原理。 Mongoid也是這樣。不知道MongoMapper。 – 2012-02-17 12:28:40

0

你需要刪除其中配置的ActiveRecord行:

# Raise exception on mass assignment protection for Active Record models 
config.active_record.mass_assignment_sanitizer = :strict 

# Log the query plan for queries taking more than this (works 
# with SQLite, MySQL, and PostgreSQL) 
config.active_record.auto_explain_threshold_in_seconds = 0.5 
+1

這並沒有解決問題。無論如何,爲什麼它呢?爲什麼rails試圖連接到mysql的任何請求? – HappyDeveloper 2012-02-17 10:58:14

+0

whith auto_explain a假設 – shingara 2012-02-17 11:21:42

0

我相信您檢查database.yml以確保沒有什麼是尋找adapter: mysql

另外,如果你已經將MySQL留在你的Gemfile中,那麼mysql gem將會彈出,因爲它不能完成它,因此必須作爲依賴失敗。刪除它+重新運行。

相關問題