nodejs的mysql模块学习,九连接池集群

连接池集群

连接池集群可以提供多个主机连接

创建连接池集群

//创建连接池集群
var poolCluster = mysql.createPoolCluster();
//添加配置 config是一个连接池配置
poolCluster.add(config);//使用自动名称添加配置
poolCluster.add('MASTER',masterConfig);//添加命名配置
poolCluster.add('SLAVE1',slave1config);
poolCluster.add('SLAVE2',slave2config);

//删除配置
poolCluster.remove('SLAVE1');//根据配置名字
poolCluster.remove('SLAVE*')//根据匹配到的

//获取连接 从所有的连接池里获得 默认选择器 
poolCluster.getConnectiuon(function(err,connection){});

//从 一个连接池里面获取连接
poolCluster.getConnectiuon('MASTER',function(err,connection){});
//从匹配到的连接池组里面获取连接 按照顺序
//如果SLAVE1出错 就从SLAVE2获得连接
poolCluster.getConnectiuon('SLAVE*','ORDER',function(err,connection){} );

//触发事件 当删除连接池时触发
poolCluster.on('remove',function(nodeId){
    console.log(nodeId);//被删除的连接池名字
});
//配置 选择器 从SLAVE1 SLAVE2 里面随机获得连接
var pool = poolCluster.of('SLAVE*','RANDOM');
pool.getConnectiuon(function(err,connection){});

//关闭连接池集群
poolCluster.end();