简体   繁体   English

生产中的数据库连接问题

[英]DB connection problem in production

I have a separate DB for one model in my application and in development mode the connection is working properly, in production however it isn't. 我在我的应用程序中为一个模型有一个单独的数据库,在开发模式下,连接正常工作,但在生产环境中却无法正常工作。

production:
  adapter: mysql
  host: myhost
  username: root
  password:
  database: production_db

users_production:
  adapter: mysql
  host: myhost
  username: root
  password:
  database: other_db

The model that connects to the other database is called User but the table it references in other_db is smf_users so my User.rb looks like this: 连接到另一个数据库的模型称为用户,但是在other_db引用的表是smf_users因此我的User.rb如下所示:

class User < ActiveRecord::Base
  establish_connection "users_#{RAILS_ENV}"
  set_table_name "smf_users"
end

In production I'm getting this error: 在生产中,我遇到此错误:

Mysql::Error: Table 'production_db. smf_users' doesn't exist:

Note how it is trying to connect to the wrong database and so isn't finding the correct table. 请注意它是如何尝试连接到错误的数据库,因此找不到正确的表。 As I say, this works in development mode. 正如我所说,这在开发模式下有效。

Any suggestions? 有什么建议么?

I've found when using multiple databases that odd errors show up when doing associations. 我发现在使用多个数据库时,进行关联时会出现奇数错误。 Any chance you've got another model out there which belongs_to :users and you're expecting it to Just Work? 您是否有可能在这里找到了另一个模型,它belongs_to :users并且您希望它可以正常工作? Otherwise, you have to look at caching -- it's easily possible that Rails is failing to cache the connection data for your extra database correctly. 否则,您必须查看缓存-Rails很可能无法正确缓存额外数据库的连接数据。

Try: 尝试:

establish_connection configurations[RAILS_ENV]["users_#{RAILS_ENV}"]
User.connection

establish_connection needs a hash of the connection information. Establishment_connection需要连接信息的哈希。 This should return if from database.yml. 如果从database.yml返回,则应该返回。

You might want to check your log for a message like the following: 您可能要检查日志中是否出现类似以下的消息:

"users_production database is not configured" “未配置users_production数据库”

That message would be thrown by ActiveRecord::Base if it can't find the congifuration by string in database.yml. 如果无法在database.yml中通过字符串找到配置,则该消息将由ActiveRecord :: Base抛出。

您可能会发现这很有用: http : //magicmodels.rubyforge.org/magic_multi_connections/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM