没有Rails的ActiveRecord :: ConnectionNotEstablished(未找到带有“主”连接池)。

时间:2019-05-03 16:30:25

标签: mysql ruby activerecord irb

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

我马上说。我正在使用无轨的activerecord

如果我这样运行活动记录。

ActiveRecord :: Base.connection.exec_query('从pp_ota_lang中选择*,其中代码=“ ZH”')

我收到:: ActiveRecord :: ConnectionNotEstablished(找不到带有“主要”的连接池。)错误

但这是断断续续的!

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

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('从pp_ota_lang中选择*,其中代码=“ 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上进行配置吗?

感谢所有提出建议的人:)

1 个答案:

答案 0 :(得分:1)

通过仅在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"
)