无法在生产环境中访问rails控制台中的不同数据库



我有数据库。yml,

的形式

default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
socket: /var/run/mysqld/mysql.sock
development:
tp:
<<: *default
database: tp
host: xxx.xxx.xxx.xxx
username: test
password: test
migrations_paths: db/tp_migrate
mi:
<<: *default
database: mi
host: xxx.xxx.xxx.xxx
username: test
password: test
migrations_paths: db/mi_migrate

production:
tp:
<<: *default
database: tp
host: xxx.xxx.xxx.xxx
username: test
password: test
migrations_paths: db/tp_migrate
mi:
<<: *default
database: mi
host: xxx.xxx.xxx.xxx
username: test
password: test
migrations_paths: db/mi_migrate

tp db有一个名为servant的表。Mi db有一张桌子叫房东。我有model as

application_record.rb

class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
connects_to database: { writing: :mi, reading: :mi }
end

tp_base.rb

class TpBase < ActiveRecord::Base
self.abstract_class = true
connects_to database: { writing: :tp, reading: :tp }
end

landlord.rb

class Landlord < ApplicationRecord
end

servant.rb

class servant < TpBase
end

当我打开开发控制台时,我能够访问这两个表,即仆人和房东。

<<p>开发控制台/strong>
raj@Raj:~/Desktop/testing_app(development)$ RAILS_ENV=development rails c
Running via Spring preloader in process 51558
Loading development environment (Rails 6.0.4.1)
2.6.3 :001 > Landlord.first
Landlord Load (263.3ms)  SELECT `landlord`.* FROM `landlord` ORDER BY `landlord`.`id` ASC LIMIT 1
=> #<Landlord id: 44> 
2.6.3 :002 > Servant.first
Servant Load (276.7ms)  SELECT `servant`.* FROM `servant` ORDER BY `servant`.`id` ASC LIMIT 1
=> #<servant id: 1> 

当我打开生产控制台时,我只能访问仆人表,而不能访问房东表。

<<p>生产控制台/strong>
raj@Raj:~/Desktop/testing_app(development)$ RAILS_ENV=production rails c
Loading production environment (Rails 6.0.4.1)
2.6.3 :001 > Landlord.first
Landlord Load (248.7ms)  SELECT `landlord`.* FROM `landlord` ORDER BY `landlord`.`id` ASC LIMIT 1
Traceback (most recent call last):
1: from (irb):1
ActiveRecord::StatementInvalid (Mysql2::Error: Table 'tp.landlord' doesn't exist)
2.6.3 :002 > Servant.first
Servant Load (265.9ms)  SELECT `servant`.* FROM `servant` ORDER BY `servant`.`id` ASC LIMIT 1
=> #<Servant id: 1> 

但是当我在生产控制台中手动为mi数据库建立连接时,只能访问地主表

<<p>

手动连接/strong>ActiveRecord:: Base.establish_connection (mi) .connection

raj@Raj:~/Desktop/testing_app(development)$ RAILS_ENV=production rails c
Loading production environment (Rails 6.0.4.1)
2.6.3 :001 > Landlord.first
Landlord Load (264.9ms)  SELECT `landlord`.* FROM `landlord` ORDER BY `landlord`.`id` ASC LIMIT 1
Traceback (most recent call last):
1: from (irb):1
ActiveRecord::StatementInvalid (Mysql2::Error: Table 'tp.landlord' doesn't exist)
2.6.3 :002 > Servant.first
Servant Load (263.6ms)  SELECT `servant`.* FROM `servant` ORDER BY `servant`.`id` ASC LIMIT 1
=> #<Servant id: 0, name: "", phone: "", company: "", active: "x00", has_license: "x01", license_class: "", default_truck_id: 8, gps_color: "00FFFF", gps_device_id: nil, rate_per_hour: 0.0, rate_per_km: 0.0, rate_per_drop: 0.0, rate_per_run: 0.0, exclude_accounting: "x00"> 
2.6.3 :003 > ActiveRecord::Base.establish_connection(:mi).connection
2.6.3 :004 > Landlord.first
Landlord Load (243.3ms)  SELECT `landlord`.* FROM `landlord` ORDER BY `landlord`.`id` ASC LIMIT 1
=> #<Landlord id: 44>
2.6.3 :006 > Servant.first
Servant Load (243.7ms)  SELECT `servant`.* FROM `servant` ORDER BY `servant`.`id` ASC LIMIT 1
=> #<Servant id: 1> 

我的问题是,为什么在生产模式下控制台默认情况下不加载数据库?是我做错了什么还是这是预期的行为这里没有错?

看起来你混淆了两个概念:复制和分片。当使用复制时,您需要使用connects_to database: { writing: :tp, reading: :tp },其中writing:是主副本,reading:是读副本。如果没有副本,则只需要指定writing:

看看你想要分片的代码。在这种情况下,您应该使用connects_to shards: { }

例如:

class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
connects_to shards: { 
default: { writing: :mi }
}
end

class TpBase < ActiveRecord::Base
self.abstract_class = true
connects_to shards: { 
shard: { writing: :tp }
}
end

更多信息:https://edgeguides.rubyonrails.org/active_record_multiple_databases.html horizontal-sharding

不确定这是否解决了你的问题。

最新更新