mysqljs / mysql

A pure node.js JavaScript Client implementing the MySQL protocol.
MIT License
18.26k stars 2.52k forks source link

clusterPool failover #1871

Open terencechow opened 6 years ago

terencechow commented 6 years ago

I'm trying to understand the poolCluster as I am in the process of setting up mysql services for production. I use Google Cloud SQL and they provide an automatic failover option. The automatic failover should be read-only not write.

My code currently uses pools like so:

const pool = mysql.createPool(config.mysql)
export default pool

elsewhere: used like pool.query ...

If I use poolCluster do I have to get a pool each time?

const poolCluster = mysql.createPoolCluster()
poolCluster.add(masterConfig)
poolCluster.add(failoverConfig)

const pool = poolCluster.of('*')
export default pool

Does poolCluster.of('*') select a new pool each time a query is actually called? Or does the above implicitly grab only 1 pool since I've only called poolCluster.of('*') once and exported it? (Implicitly this means I'll need to add a lot of poolCluster.of('*') everywhere in my code and I'm hoping that isn't the case.

Also if I use poolCluster.of('*') and proceed to make a write query, will it implicitly select the pool that can take writes? Or do we need to specifcy the pool that can take writes?

dougwilson commented 6 years ago

Hi @terencechow sorry you haven't gotten a response yet. I am not very familiar with the pool cluster implementation, and the docs are quick lacking (partly because I don't really know enough about it to write docs and none have been contributed yet). If I find some time, I will try to research your issue is no one else responds.