Raj*_*rma 6 ruby ruby-on-rails
我有database.yml,
数据库.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
Run Code Online (Sandbox Code Playgroud)
tp db 有一个名为servant 的表。mi db 有一个名为lord 的表。我的模型为,
应用程序记录.rb
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
connects_to database: { writing: :mi, reading: :mi }
end
Run Code Online (Sandbox Code Playgroud)
tp_base.rb
class TpBase < ActiveRecord::Base
self.abstract_class = true
connects_to database: { writing: :tp, reading: :tp }
end
Run Code Online (Sandbox Code Playgroud)
楼主.rb
class Landlord < ApplicationRecord
end
Run Code Online (Sandbox Code Playgroud)
仆人.rb
class servant < TpBase
end
Run Code Online (Sandbox Code Playgroud)
当我打开开发控制台时,我可以访问表,即仆人和房东。
开发控制台
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>
Run Code Online (Sandbox Code Playgroud)
当我打开生产控制台时,我只能访问仆人表,但无法访问房东表。
生产控制台
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>
Run Code Online (Sandbox Code Playgroud)
但是当我在生产控制台中手动为 mi 数据库建立连接时,只能访问楼主表
手动连接
ActiveRecord::Base.built_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>
Run Code Online (Sandbox Code Playgroud)
我的问题是,为什么在生产模式下控制台默认不加载数据库?我做错了什么还是这是预期的行为,这里没有任何问题?
看起来您混淆了两个概念:复制和分片。使用复制时,您需要使用主节点和只读副本的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
Run Code Online (Sandbox Code Playgroud)
更多信息: https://edgeguides.rubyonrails.org/active_record_multiple_databases.html#horizo ntal-sharding
但不确定这是否能解决您的问题。
| 归档时间: |
|
| 查看次数: |
659 次 |
| 最近记录: |