gpt4 book ai didi

mysql - ActiveRecord::ConnectionNotEstablished(未找到 'primary' 的连接池。)没有 Rails

转载 作者:行者123 更新时间:2023-11-29 07:21:45 25 4
gpt4 key购买 nike

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

#

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

#

如果我像这样运行事件记录..

ActiveRecord::Base.connection.exec_query('select * from pp_ota_lang where code = "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('select * from pp_ota_lang where code = "ZH"')这绝对有效。

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

这是 irb 中的一个 session

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 上配置什么吗?

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

最佳答案

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

关于mysql - ActiveRecord::ConnectionNotEstablished(未找到 'primary' 的连接池。)没有 Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55973883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com