npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mysql-master-slave

v0.0.7

Published

mysql master slave cluster util

Downloads

16

Readme

MySQL Master Slave

This module is depend on mysql module for build the master, slave database environment.

Installation

npm install mysql-master-slave

Usage Sampple:

var cluster = require('mysql-master-slave');

var opts = {
  connectionLimit : 10,
  user: 'your-user',
  password: 'your-password',
  database: 'sampledb'
}

cluster.addMaster('your-mysql-server', '3306', opts);
cluster.addMaster('your-mysql-server', '3306', opts);
cluster.addSlave('your-mysql-server', '3306', opts);
cluster.addSlave('your-mysql-server', '3306', opts);
cluster.addSlave('your-mysql-server', '3306', opts);

var sql = 'insert into test123 (create_date) values (?)';

for(var i = 0 ; i < 10 ; i++)
cluster.pool.query(sql, [new Date()], function(err, r, f){
  if(err) {
    console.log('Error:', err);
  }

  console.log('Result:', r);
  process.exit(0);
});

Execution result:

node test.js
[2015-04-06 09:31:26.424] [DEBUG] [default] - [index.js - query] using master pool...
[2015-04-06 09:31:26.431] [DEBUG] [default] - [index.js - query] using master pool...
[2015-04-06 09:31:26.432] [DEBUG] [default] - [index.js - query] using master pool...
[2015-04-06 09:31:26.433] [DEBUG] [default] - [index.js - query] using master pool...
[2015-04-06 09:31:26.434] [DEBUG] [default] - [index.js - query] using master pool...
[2015-04-06 09:31:26.436] [DEBUG] [default] - [index.js - query] using master pool...
[2015-04-06 09:31:26.437] [DEBUG] [default] - [index.js - query] using master pool...
[2015-04-06 09:31:26.437] [DEBUG] [default] - [index.js - query] using master pool...
[2015-04-06 09:31:26.438] [DEBUG] [default] - [index.js - query] using master pool...
[2015-04-06 09:31:26.439] [DEBUG] [default] - [index.js - query] using master pool...
Result: { fieldCount: 0,
  affectedRows: 1,
  insertId: 40,
  serverStatus: 2,
  warningCount: 0,
  message: '',
  protocol41: true,
  changedRows: 0 }

About Transaction

If you want to use transaction in your project. There is a quick way to implememt transaction use "cluster.beginTransaction()" and "tx.addJob()"...

var cluster = require('mysql-master-slave');

var opts = {
  connectionLimit : 10,
  user: 'root',
  password: '123456',
  database: 'mydb' 
}

cluster.setDbSelectStrategy(1);

cluster.addMaster('123.123.123.123', '3306', opts);
cluster.addMaster('124.124.124.124', '3306', opts);

var tx = cluster.beginTransaction();

tx.addJob('insert into test123 (id, create_date, data) values (?, ?, ?)', [50, new Date(), "test 中文"], function(){
  console.log('job1 done...');
})

tx.addJob('select * from test123 where id =?', [51, "test 中文"], function(err, result){
  console.log('verify job1 done...');
  if(err) {
    console.log('error:', err);
  }

  console.log('result:', result);
});

tx.exec(function(err, result){
  if(err) {
    console.log('err:', err);
  }
  console.log('>>>', result);
});

Others

Setup as sequence read from pool or random select.

cluster.setDbSelectStrategy(1);

Result:

[2015-04-06 09:37:23.767] [DEBUG] [default] - [index.js - query] using master pool...
[2015-04-06 09:37:23.778] [DEBUG] [default] - [index.js - query] using master pool...
[2015-04-06 09:37:23.779] [DEBUG] [default] - [index.js - query] using master pool...
[2015-04-06 09:37:23.779] [DEBUG] [default] - [index.js - query] using master pool...
[2015-04-06 09:37:23.780] [DEBUG] [default] - [index.js - query] using master pool...
[2015-04-06 09:37:23.781] [DEBUG] [default] - [index.js - query] using master pool...
[2015-04-06 09:37:23.782] [DEBUG] [default] - [index.js - query] using master pool...
[2015-04-06 09:37:23.782] [DEBUG] [default] - [index.js - query] using master pool...
[2015-04-06 09:37:23.784] [DEBUG] [default] - [index.js - query] using master pool...
[2015-04-06 09:37:23.785] [DEBUG] [default] - [index.js - query] using master pool...
Result: { fieldCount: 0,
  affectedRows: 1,
  insertId: 50,
  serverStatus: 2,
  warningCount: 0,
  message: '',
  protocol41: true,
  changedRows: 0 }