【问题标题】:Why is rails trying to connect to mysql?为什么rails试图连接到mysql?
【发布时间】:2012-02-17 10:49:33
【问题描述】:

我一直在一个新的 Rails 应用程序中使用 mysql,但现在我想尝试一下 mongoDB,所以我安装了 mongo mapper 和 mongoid(使用 mongo session)。安装似乎很好,因为我可以创建 mongo 模型。但由于某种原因,rails 仍在尝试连接到 mysql:Can't connect to local MySQL server

这太可怕了,因为即使我没有使用 mongo,rails 也不应该为每个请求都尝试连接到 mysql。即使对于不存在的 url,它也会抛出该错误。

我可以做些什么来调试这个?我想我可以尝试从 Gemfile 中删除 mysql gem 并运行bundle install。但我仍然不喜欢即使我不使用它也试图连接的事实。它不应该尝试“惰性”连接(即:仅按需连接)吗?

开发.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

应用程序.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

【问题讨论】:

    标签: mysql ruby-on-rails ruby-on-rails-3 mongodb


    【解决方案1】:

    当 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,但您可能应该删除,因为它现在没有任何意义。

    【讨论】:

    • 致读者:这就是答案,虽然我不知道代码是否有效,因为我刚刚使用 --skip-active-record 重新安装了应用程序(生成类似于上面代码的内容)。但我仍然想知道活动记录自动连接到 mysql 的原因是什么,即使我没有在特定请求中使用它,这似乎是迟钝的。我是 ruby​​ 和 rails 的新手,但我仍然不知道它们的内部结构。
    • @HappyDeveloper:ActiveRecord 中没有“延迟连接”。它在初始化期间连接。这就是它的工作原理。 Mongoid 也是如此。不知道 MongoMapper。
    【解决方案2】:

    您需要删除配置 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
    

    【讨论】:

    • 这并没有解决问题。无论如何,为什么要这样做?为什么 rails 会尝试连接到 mysql 以获取任何请求?
    • 自动解释一个假设
    【解决方案3】:

    我想你已经检查了你的 database.yml 以确保没有任何东西在寻找 adapter: mysql

    此外,如果您在 Gemfile 中留下了 mysql,那么 mysql gem 将被炸毁,因为它无法完成它的一部分,因此必须作为依赖项失败。删除它+重新运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-09-24
      • 1970-01-01
      • 2012-05-27
      • 2010-11-20
      • 2015-07-04
      • 2020-06-02
      • 1970-01-01
      相关资源
      最近更新 更多