首页 > 解决方案 > 没有 Rails 的 ActiveRecord::ConnectionNotEstablished(没有找到“主”的连接池。)

问题描述

我在 irb 做一些工作。此错误是间歇性的。

#

让我马上说。我正在使用没有导轨的 activerecord

#

如果我像这样运行活动记录..

ActiveRecord::Base.connection.exec_query('select * from pp_ota_lang where code = "ZH"')

我得到:ActiveRecord::ConnectionNotEstablished (No connection pool with 'primary' found.) 错误

但它是间歇性的!

如果我创建这样的模型...

class PP_OTA_INFO < ActiveRecord::Base
  establish_connection(
    adapter:  "mysql2",
    host:     "localhost",
    username: "root",
    password: "password",
    database: "ota4user"
  )
  self.table_name = "pp_ota_info"
end

PP_OTA_INFO.connection.select_all('select * from pp_ota_lang where code = "ZH"') 这可以正常工作。

我在mysql设置中遗漏了什么吗?我用 brew 设置了 mysql 并使用了所有默认值。我可以从命令提示符连接到 mysql 并运行任何东西。

这是irb中的一个会话

colmac@Xazoola-2:~ $ irb
irb(main):001:0> **require 'active_record'**
=> true
irb(main):002:0> ActiveRecord::Base.clear_active_connections!
=> []
irb(main):003:0> ActiveRecord::Base.connection.exec_query('select * from pp_ota_lang where code = "ZH"')
Traceback (most recent call last):
        7: from /usr/local/opt/ruby/bin/irb:27:in `<main>'
        6: from /usr/local/opt/ruby/bin/irb:27:in `load'
        5: from /usr/local/Cellar/ruby/2.6.3/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
        4: from (irb):3
        3: from /usr/local/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:90:in `connection'
        2: from /usr/local/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
        1: from /usr/local/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1013:in `retrieve_connection'
**ActiveRecord::ConnectionNotEstablished (No connection pool with 'primary' found.)**
irb(main):004:0> **ActiveRecord::Base.clear_active_connections!**
=> []
irb(main):005:0> **require 'mysql2'**
=> true
irb(main):006:0> ActiveRecord::Base.connection.exec_query('select * from pp_ota_lang where code = "ZH"')
Traceback (most recent call last):
        7: from /usr/local/opt/ruby/bin/irb:27:in `<main>'
        6: from /usr/local/opt/ruby/bin/irb:27:in `load'
        5: from /usr/local/Cellar/ruby/2.6.3/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
        4: from (irb):6
        3: from /usr/local/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:90:in `connection'
        2: from /usr/local/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
        1: from /usr/local/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1013:in `retrieve_connection'
ActiveRecord::ConnectionNotEstablished (No connection pool with 'primary' found.)
irb(main):007:0> **ActiveRecord::Base.clear_active_connections!**
=> []
irb(main):008:0> **ActiveRecord::Base.clear_all_connections!**
=> []
irb(main):009:0> class PP_OTA_INFO < ActiveRecord::Base
irb(main):010:1>   establish_connection(
irb(main):011:2*     adapter:  "mysql2",
irb(main):012:2*     host:     "localhost",
irb(main):013:2*     username: "root",
irb(main):014:2*     password: "password",
irb(main):015:2*     database: "ota4user"
irb(main):016:2>   )
irb(main):017:1>   self.table_name = "pp_ota_info"
irb(main):018:1> end
=> "pp_ota_info"
irb(main):019:0> **PP_OTA_INFO.connection.select_all('select * from pp_ota_lang where code = "ZH"')**

=> #<ActiveRecord::Result:0x00007fd54c297ad8 @columns=["id", "name", "code"], @rows=[***[27, "Chinese Simplified", "ZH"]***], @hash_rows=nil, @column_types={}>

irb(main):020:0> **ActiveRecord::Base.clear_all_connections!**

as you can see i had a connection...

=> [#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x00007fd54c888f50 @mon_mutex=#<Thread::Mutex:0x00007fd54c888e60>, @mon_mutex_owner_object_id=70277044389800, @mon_owner=nil, @mon_count=0, @query_cache_enabled=#<Concurrent::Map:0x00007fd54c888e38 entries=0 default_proc=#<Proc:0x00007fd54c888d98@/usr/local/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/query_cache.rb:31>>, @spec=#<ActiveRecord::ConnectionAdapters::ConnectionSpecification:0x00007fd54c889220 @name="PP_OTA_INFO", @config={:adapter=>"mysql2", :host=>"localhost", :username=>"root", :password=>"password", :database=>"ota4user"}, @adapter_method="mysql2_connection">, @checkout_timeout=5, @idle_timeout=300.0, @size=5, @thread_cached_conns=#<Concurrent::Map:0x00007fd54c888d48 entries=0 default_proc=nil>, @connections=[], @automatic_reconnect=true, @now_connecting=0, @threads_blocking_new_connections=0, @available=#<ActiveRecord::ConnectionAdapters::ConnectionPool::ConnectionLeasingQueue:0x00007fd54c888ca8 @lock=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x00007fd54c888f50 ...>, @cond=#<MonitorMixin::ConditionVariable:0x00007fd54c888c58 @monitor=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x00007fd54c888f50 ...>, @cond=#<Thread::ConditionVariable:0x00007fd54c888c30>>, @num_waiting=0, @queue=[]>, @lock_thread=false, @reaper=#<ActiveRecord::ConnectionAdapters::ConnectionPool::Reaper:0x00007fd54c888be0 @pool=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x00007fd54c888f50 ...>, @frequency=60.0>>]


**Now with direct sql**

irb(main):021:0>  ActiveRecord::Base.connection.exec_query('select * from pp_ota_lang where code = "ZH"')
Traceback (most recent call last):
        7: from /usr/local/opt/ruby/bin/irb:27:in `<main>'
        6: from /usr/local/opt/ruby/bin/irb:27:in `load'
        5: from /usr/local/Cellar/ruby/2.6.3/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
        4: from (irb):21
        3: from /usr/local/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:90:in `connection'
        2: from /usr/local/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
        1: from /usr/local/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1013:in `retrieve_connection'
**ActiveRecord::ConnectionNotEstablished (No connection pool with 'primary' found.)**
irb(main):022:0> 

有任何想法吗 ?有什么我必须在mysql上配置的吗?

感谢所有有建议的人:)

标签: mysqlrubyactiverecordirb

解决方案


通过仅在 PP_OTA_INFO 类中建立连接,它不会为 ActiveRecord::Base 建立连接。

仅使用表名设置您的类,然后建立与 ActiveRecord::Base 的连接:

class PP_OTA_INFO < ActiveRecord::Base
  self.table_name = "pp_ota_info"
end

ActiveRecord::Base.establish_connection(
  adapter:  "mysql2",
  host:     "localhost",
  username: "root",
  password: "password",
  database: "ota4user"
)

推荐阅读