mysql-clustered
[![NPM Version][mysql-url]]
Table of Contents
News
WARNING!!! This package is in HEAVY DEVELOPMENT and is NOT READY FOR PRODUCTION USE!!!!
Introduction
Welcome to the MySQL Clustered package. This package will allow you to hook up multiple Mysql hosts with minimal configuration on your part. It will handle all the complication associated with clustering large MySQL farms.
This package relies HEAVILY on the mysql node package. It has been modified to handle mysql replication and clustering. I did not write any of that code. Please refer to https://www.npmjs.com/package/mysql for licensing.
Install
Run this on every application instance that is going to use the cluster
$ npm install mysql-clustered
After following the steps below, all you have to do is the following
cluster.query
Setting Up Databases
(This assumes AWS ubuntu servers, adjust accordingly)
http://dev.mysql.com/doc/refman/5.7/en/replication-howto.html
Setting up binlogs
On master server
$ sudo su$ emacs /etc/mysql/my.cnf
Set each master's server_id variable with a unique id. I like to set them up as 100's so for example I would set the first master server up as 100 and then the second one as 200 Here's an example of what I do on our production servers but you can feel free to change it up as needed
server-id = 100log_bin = /supersecret/path/to/binlogsexpire_log_days = 1max_binlog_size = 100Mbinlog_do_db = supersecretnameofdatabaseinnodb_flush_log_at_trx_commit = 1sync_bin_log = 1
uneeded but I suggest you at least increase these values somewhat
wait_timeout = 28800innodb_lock_wait_timeout = 28800connect_timeout = 28800net_read_timeout = 28800net_write_timeout = 28800slave_net_timeout = 28800max_connections = 100000max_connect_errors = 1000000
save,exit,restart mysql
$ service mysql restart
On slave
$ sudo su$ emacs /etc/mysql/my.cnf
server-id = 101
save,exit,restart mysql
$ service mysql restart
Create a user inside mysql to query off of and one to replicate off of
CREATE USER 'someuser'@'172.%' IDENTIFIED BY 'terriblepassword';GRANT ALL ON *.* TO 'someuser'@'172.%';CREATE USER 'repl'@'172.%' IDENTIFIED BY 'terriblepassword';GRANT REPLICATION SLAVE ON *.* TO 'repl'@'172.%';FLUSH PRIVILEGES;
Lock the tables on the master databases
Dump the databases and load the script into the slaves
Unlock the tables and start slaves
On the master server
FLUSH TABLES WITH READ LOCK;SHOW MASTER STATUS;
write down/memorize/or keep this open in a terminal somewhere
Use whatever tool you want to get the scripts from the master database to all other databases (I just use MySQL workbench)
On the slave
CHANGE MASTER TOMASTER_HOST='master_host_name',MASTER_USER='replication_user_name',MASTER_PASSWORD='replication_password',MASTER_LOG_FILE='recorded_log_file_name',MASTER_LOG_POS=recorded_log_position;START SLAVE;SHOW SLAVE STATUS; // Use this to make sure the slave is activily changing based on the master
On the master server
UNLOCK TABLES;
I would suggest at this point to go change something on the master and check the slave to make sure it migrated to the slave.
If everything went well you now have a clustered system!
Basic Usage
You don't have to use async but be aware that if you don't it's possible for query to be called before the system is fully initialized.
The configuration variables are the same as node-mysql createPool(). https://www.npmjs.com/package/mysql#pool-options
var async = require,cluster =async.series
MySQL Load File
If you are interested in storing file into the database, you need to follow these instructions.
MySQL's LOAD_FILE will only look for the file locally. We solve this problem by storing the file
locally on the MySQL master server and load it after it's been posted.