Ruby-on-rails 数据库配置没有指定适配器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/413659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
database configuration does not specify adapter
提问by Roberto
I'm getting this error when I'm trying to connect to a mysql database. The problem is that the application works for weeks, and then randomly I get this message. When I get this error message the application is not able to reconnect to the database until I restart it.
当我尝试连接到 mysql 数据库时出现此错误。问题是该应用程序可以运行数周,然后我随机收到此消息。当我收到此错误消息时,应用程序无法重新连接到数据库,直到我重新启动它。
I'm using a configuration file to connect to the database, and the adapter is specified...the database configuration is not generated at runtime.
我正在使用配置文件连接到数据库,并且指定了适配器...运行时未生成数据库配置。
Do you have any idea on what is going on?
你知道发生了什么吗?
回答by Fumisky Wells
when I tried to run a command line script (let's say 'my_script' here), the same error happened. The reasons were:
当我尝试运行命令行脚本(这里假设为“my_script”)时,发生了同样的错误。原因是:
- There is only production environment there.
- I missed to set RAILS_ENV for the command line.
- 那里只有生产环境。
- 我错过了为命令行设置 RAILS_ENV。
So, the following is the solution in my case:
因此,以下是我的解决方案:
$ RAILS_ENV=production my_script
$ RAILS_ENV=生产 my_script
回答by djacobs7
I just had this problem, and it was caused by a typo in my configration.yml.
我刚刚遇到了这个问题,它是由我的 configration.yml 中的拼写错误引起的。
I originally had this:
我最初有这个:
production:
adapter:mysql
When I meant to have this:
当我想拥有这个时:
production:
adapter: mysql
That one little space between adapter: and mysql makes the difference.
adapter: 和 mysql 之间的那一点空间有所不同。
回答by Benjamin Oakes
Another possible cause:
另一个可能的原因:
In Rails 3.2.x, establish_connectionhas a default argument set from the environment:
在 Rails 3.2.x 中,establish_connection有一个来自环境的默认参数集:
From connection_specification.rb:
def self.establish_connection(spec = ENV["DATABASE_URL"])
resolver = ConnectionSpecification::Resolver.new spec, configurations
spec = resolver.spec
The way ConnectionSpecification::Resolverworks depends on ENV['DATABASE_URL']giving a nilif not set. (Normally, it would be something like 'postgres://...').
ConnectionSpecification::Resolver工作方式取决于ENV['DATABASE_URL']给出一个nilif not set。(通常,它类似于'postgres://...')。
So, if you happen to have misconfigured DATABASE_URLsuch that ENV['DATABASE_URL'] == '', that will give you database configuration does not specify adapter.
因此,如果您碰巧错误配置了DATABASE_URL这样的ENV['DATABASE_URL'] == '',那会给您database configuration does not specify adapter.
回答by andrew
I had this error when I mistakenly started rails server with
当我错误地启动 rails 服务器时,我遇到了这个错误
sudo rails s -e "Production" -p 80
须藤导轨 s -e “生产” -p 80
and I should have started rails with
我应该开始使用 rails
sudo rails s -e "production" -p 80
须藤导轨 s -e “生产” -p 80
回答by rlkw1024
I found another thing that can cause this problem: "mixing in" another YAML node using &and *.
我发现了另一件可能导致此问题的事情:使用&和“混入”另一个 YAML 节点*。
I was originally doing something like the following to facilitate local, per-develop, Git-ignored config files:
我最初是在做类似以下的事情来促进本地的、每个开发的、Git 忽略的配置文件:
http://blog.lathi.net/articles/2006/03/02/config-database-yml-goodness-for-teams
http://blog.lathi.net/articles/2006/03/02/config-database-yml-goodness-for-teams
But, after some debugging, I came to find out that establish_connectionwas for some reason being called with only the mixed-in key-value pairs and not the main ones. I.e. adapter, host, and databasewere not being passed in. I have no idea why, and this used to work for me.
但是,经过一些调试后,我发现establish_connection由于某种原因,它只使用混合键值对而不是主要键值对被调用。即adapter, host, 并database没有被传入。我不知道为什么,这曾经对我有用。
Anyhow, instead of mixing in another YAML node, I now put the entire developmentand testhashes in the local config file, and establish_connectionis once again being called correctly.
无论如何,我现在没有在另一个 YAML 节点中混合,而是将整个development和test散列放在本地配置文件中,并且establish_connection再次被正确调用。
回答by SenG
For me, this command resolved the issue.
对我来说,这个命令解决了这个问题。
rake db:migrate RAILS_ENV=production
耙分贝:迁移 RAILS_ENV=生产
回答by Eternal21
I got the same error, by typing in the following command:
通过输入以下命令,我得到了同样的错误:
db:migrate RAILS_ENV=product
Should've been:
应该是:
db:migrate RAILS_ENV=production
回答by Martin Carpenter
I've found a couple of clues that this might be related to older library (ActiveRecord) or gem versions. For example, problems with fixtures even though rest of app seems okay(after an upgrade) or this trac ticket, which "stops gems from requiring an adapter from an old Active Record gem". Both of these are old, though, but it might be worth making sure your gems are up to date (if possible).
我发现了一些线索,表明这可能与较旧的库 (ActiveRecord) 或 gem 版本有关。例如,即使应用程序的其余部分看起来没问题(升级后)或此 trac 票证,设备的问题,它“阻止 gems 需要来自旧 Active Record gem 的适配器”。不过,这两个都是旧的,但可能值得确保您的 gem 是最新的(如果可能的话)。
Are you using the native rails MySQL adapter by any chance? This is now deprecated under rails, but it's conceivable it's still limping along.
您是否有机会使用本机 rails MySQL 适配器?这现在在 rails 下已被弃用,但可以想象它仍然一瘸一拐。
I've taken a very quick look at connection_specification.rb, too, which is where this error is coming from, and my best guess is that a reconnect is failing... but why (since it was obviously okay when you first started the app)? Are you doing something wild like calling ActiveRecord::Base.establish_connectionin your application controller (or elsewhere)?
我也快速查看了connection_specification.rb这个错误的来源,我最好的猜测是重新连接失败了......但是为什么(因为当你第一次启动应用程序时它显然没问题)?你在做一些疯狂的事情,比如ActiveRecord::Base.establish_connection在你的应用程序控制器(或其他地方)中调用?
Or perhaps something like: script runner is called from cron in the dead of night when the connection has dropped. Unfortunately, the runner is invoked with an incorrect RAILS_ENV. Thus the wrong stanza is read from database.yml, and that stanza contains an invalid adapter:?
或者可能是这样的:当连接断开时,在深夜从 cron 调用脚本运行程序。不幸的是,运行程序被调用了一个不正确的RAILS_ENV. 因此,读取了错误的节database.yml,并且该节包含无效的adapter:?
回答by Jones Agyemang
Do remember that RAILS_ENV=staging will look for a staging specification in your database.yml just as setting RAILS_ENV=production will look for a production specification in database.yml file.
请记住, RAILS_ENV=staging 将在您的 database.yml 中查找暂存规范,就像设置 RAILS_ENV=production 将在 database.yml 文件中查找生产规范一样。
Provide database configs, as shown below, for every rails environment you target using, for example
例如,为您使用的每个 rails 环境提供数据库配置,如下所示
bundle exec cap staging deploy
production:
adapter: mysql2
encoding: utf8
database: rails
username: rails
password: pass
host: 127.0.0.1
port: 3306
pool: 5
timeout: 5000
staging:
adapter: mysql2
encoding: utf8
database: rails
username: rails
password: pass
host: 127.0.0.1
port: 3306
pool: 5
timeout: 5000
回答by thekindofme
If you get this error while deploying with Capistrano. Make sure you are setting the correct RAILS_ENV via
如果您在使用 Capistrano 部署时遇到此错误。确保您通过以下方式设置正确的 RAILS_ENV
set :rails_env, 'production'
For example I was not explicitly setting the Rails environment in for the Capistrano staging deployment configuration. And thus Capistrano used 'staging' as the RAILS_ENV, resulting in the above error. Setting it to production like above in the staging.rb file solved the issue.
例如,我没有为 Capistrano 暂存部署配置明确设置 Rails 环境。因此 Capistrano 使用 'staging' 作为 RAILS_ENV,导致上述错误。在 staging.rb 文件中将其设置为如上所述的生产解决了这个问题。

