在运行的应用程序中将模型动态连接到数据库?
我已经阅读了许多有关此主题的现有问题/线程,但请记住,它们都没有直接解决我的问题。 另请记住,这不是 database.yml 的情况,因为我不会提前知道数据库信息。
也就是说,我需要一个动态连接到 Rails 应用程序中多个数据库的解决方案。 我的情况是,我有多个数据记录站点,所有站点都有一个简单的数据表(事件、时间戳、值)。 这些站点需要(并将)保持原样,因为本地应用程序需要使用它们。
我需要创建一个应用程序来维护“MYSQL_LOGINS”表,并使用每个登录名连接到各种数据库中的任何一个,并为其中的数据提供图表/图形。
对于我连接的所有 MySQL 数据库,我的“数据”模型将保持不变,我只需要能够告诉我的模型即时连接到不同的数据库。 我希望这非常简单,但我们拭目以待。
I've read many of the existing questions/threads on this subject, but keep in mind that none of them have directly addressed my issue. Also keep in mind that this is NOT a situation for database.yml as I won't know the DB info in advance.
That said, I need a solution for DYNAMICALLY connecting to multiple databases in a Rails app. My situation is that I have multiple data logging sites, all with a simple data table (EVENTS, TIMESTAMP, VALUE). These sites need to (and will) remain as they are due to local applications that need to use them.
What I need is to create an application that maintains a table of "MYSQL_LOGINS" and uses each of those logins to connect to any one of various databases and provide charts/graphs for the data therein.
My "data" model will remain the same for all MySQL databases to which I connect, I simply need to be able to tell my MODEL to connect to a different DB on the fly. I'm hoping this is amazingly simple, but we'll see.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要 ActiveRecord::Base#assessment_connection。 您可能希望在抽象子类中使用它,以便使用手动连接的模型不会干扰应用程序其余部分的连接使用:
如果您只有一个进行手动连接的模型,则抽象类是不必要。
您知道您可能连接到的全套数据库吗? 如果是这样,database.yml 可能对您仍然有用。 您可以将所有信息作为一个目录放入其中,为每个数据库指定一个名称,然后使用这些名称动态选择要在应用程序中建立连接的数据库:
建立连接可能非常繁重,如果站点之间的间隔很宽,则情况会加倍。区域网络。 我会考虑使用提取-转换-加载方法并使用每小时/每天的 cron 作业将数据复制到集中式报告站点是否更有意义。 这是数据仓库最基本的形式,也是一种非常常用的方法。 如果您使用 MySql,Maatkit Sync 是一个简洁的小工具,就像表的 rsync 一样。 或者,您可能使用支持主从复制的数据库,尽管维护起来可能要复杂得多。
You want ActiveRecord::Base#establish_connection. You'll probably want to use it in an abstract subclass so that models using the manual connections don't disturb the connection uses by the rest of your application:
If you'll only ever have one model that makes manual connections the abstract class is unnecessary.
Do you know the full set of databases that you might connect to? If so, database.yml may still be useful to you. You can put all the information in there as a directory, giving each database a name, then dynamically chose which one to establish_connection with in the application using those names:
Setting up connections can be pretty heavyweight, doubly so if the sites are separated by wide area network. I'd consider whether it makes more sense to use an extract-transform-load approach and use a hourly/daily cron job to copy the data to a centralized reporting site. This is the most basic form of a data warehouse, and is a very commonly used approach. If you're using MySql, Maatkit Sync is a neat little tool, like rsync for tables. Alternately you probably use a database that supports master-slave replication, though that can be considerably more complicated to maintain.
Rails 还不支持开箱即用的多个数据库(也许永远不会,谁知道呢)。 您必须以某种方式劫持该模型的 ActiveRecord 中的数据库连接。
这里是这方面的活动记录文档,这里是特定文档。
这是真正很好地解释了这个过程的东西。
还有一些半生产就绪项目正在攻击此问题。
Rails doesn't support multiple databases out of the box yet (may never will, who knows). You'll have to somehow hijack the database connection in ActiveRecord for that model.
Here is the active record documentation for this, and here is the specific documentation you'll have to override.
This is something that really explains the process well.
There are also some semi-production-ready projects attacking this issue.