我在irb做些工作。这个错误是间歇性的。
,让我马上说。我使用的是没有rails的activerecord
如果我像这样运行活动记录..。
ActiveRecord::Base.connection.exec_query('select *来自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"
endPP_OTA_INFO.connection.select_all('select * from 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上有什么需要配置的吗?
感谢诸位的建议:)
发布于 2019-05-03 21:06:04
通过仅在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"
)https://stackoverflow.com/questions/55973883
复制相似问题