简体   繁体   English

使用node.js,如何写入一个MySQL DB并从另一个读取?

[英]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.

相关问题 从MySQL服务器获取到Node.js程序后,如何通过response.write方法写入数据? - How can I write data by method response.write after got from MySQL server to a Node.js program? 使用Node.js编写MySQL一对多关系 - Using Node.js to write MySQL One-to-Many relationship 无法从 node.js 将数据写入 mySql - Can not write data into mySql from node.js 在 node.js 项目中实现数据库查询以读取和写入 MySQL 副本的正确方法? - Proper way of implementing the DB queries to read and write MySQL replicas in the node.js project? 如何使用反引号在 node.js 中编写多行 MySQL 查询? - How to write multiline MySQL queries in node.js using backticks? 我如何在 node.js 和 mysql 中对 map 一个查询到另一个 - How do I map one query to another in node.js and mysql 我可以在使用 html、mysql 和 NODE.js 的一个按钮上使用 2 个 POST 请求吗? - Can I use 2 POST requests on click of one button using html, mysql and NODE.js? 您如何使用node.js从mySQL读取数据并将其发布回HTML? - How do you read data from mySQL using node.js and post back to HTML? 如何编写一个mysql查询来读取一个表中的数据并写入另一个表? - How do I write a mysql query that will read data from one table and write to another? 如何在 Node.JS 的一个函数中使用来自 MySQL 的响应? - How to use responses from MySQL in one function with Node.JS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM