[英]Using node.js, how can I write to one MySQL DB and read from another?
Specifically, I'm talking about MySQL Master-Slave(s) Replication
. 具体来说,我在谈论MySQL Master-Slave(s) Replication
。 As I understand it, all of my reads should happen from a slave (or multiple slaves hid behind a load balancer) and all of my writes directly to the master. 根据我的理解,我的所有读取都应该从一个从站(或多个从站隐藏在负载均衡器后面)和我所有的写入直接发送到主站。
How can I do this with node.js
? 我怎么能用node.js
做到这一点? I'm using the MySQL Active Record package for my database queries and I don't think that supports it natively. 我正在使用MySQL Active Record包进行数据库查询,我认为它本身不支持它。 I supposed I can hack away at the package and have a if is write-query, then use db1
type of situation, but I wonder if there's an easier way. 我想我可以破解包并有一个if is write-query, then use db1
类型的情况,但我想知道是否有更简单的方法。
You can just create two adapters like this: 您可以像这样创建两个适配器:
var activeRecord = require('mysql-activerecord');
var readDB = new activeRecord.Adapter({
server: 'slave.db.com',
username: 'user',
password: 'pass',
database: 'db'
});
var writeDB = new activeRecord.Adapter({
server: 'master.db.com',
username: 'user',
password: 'pass',
database: 'db'
});
When you go to do an update
or other writing query, use writeDB
; 当您进行update
或其他写入查询时,请使用writeDB
; when you go to do a select
use readDB
. 当你去select
使用readDB
。
Another option would be to switch to another ORM that supports this out of the box. 另一种选择是切换到支持这种开箱即用的另一个ORM。 I know this isn't as nice of a solution though. 我知道这不是一个很好的解决方案。 Sequelize for example is widely used and supports this out of the box. 例如, Sequelize被广泛使用并支持开箱即用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.