Implemented Read/Write Mysql Split (Master for writes, Slave for reads)
This commit is contained in:
parent
064ab5ac83
commit
52c3e2ff93
@ -9,6 +9,7 @@ require_once(CLASS_DIR . '/debug.class.php');
|
||||
if ($config['mysql_filter']) {
|
||||
require_once(CLASS_DIR . '/strict.class.php');
|
||||
}
|
||||
require_once(INCLUDE_DIR . '/classes/mysqlims.class.php');
|
||||
require_once(INCLUDE_DIR . '/database.inc.php');
|
||||
require_once(INCLUDE_DIR . '/config/memcache_keys.inc.php');
|
||||
require_once(INCLUDE_DIR . '/config/error_codes.inc.php');
|
||||
|
||||
85
include/classes/mysqlims.class.php
Normal file
85
include/classes/mysqlims.class.php
Normal file
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
/*
|
||||
* This class will run queries on master/slave servers depending on the query itself.
|
||||
*/
|
||||
class mysqlims extends mysqli
|
||||
{
|
||||
private $mysqliW;
|
||||
private $mysqliR = null;
|
||||
|
||||
/*
|
||||
* Pass main and slave connection arrays to the constructor, and strict as true/false
|
||||
*
|
||||
* @param array $main
|
||||
* @param array $slave
|
||||
* @param boolean $strict
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($main, $slave = false, $strict = false)
|
||||
{
|
||||
var_dump($main);
|
||||
var_dump($slave);
|
||||
if ($strict) {
|
||||
$this->mysqliW = new mysqli_strict($main['host'],
|
||||
$main['user'], $main['pass'],
|
||||
$main['name'], $main['port']);
|
||||
if ($slave && is_array($slave) && isset($slave['enabled']) && $slave['enabled']
|
||||
=== true) {
|
||||
$this->mysqliR = new mysqli_strict($slave['host'],
|
||||
$slave['user'], $slave['pass'],
|
||||
$slave['name'], $slave['port']);
|
||||
}
|
||||
} else {
|
||||
$this->mysqliW = new mysqli($main['host'],
|
||||
$main['user'], $main['pass'],
|
||||
$main['name'], $main['port']);
|
||||
if ($slave && is_array($slave) && isset($slave['enabled']) && $slave['enabled']
|
||||
=== true) {
|
||||
$this->mysqliR = new mysqli($slave['host'],
|
||||
$slave['user'], $slave['pass'],
|
||||
$slave['name'], $slave['port']);
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->mysqliW->connect_errno) {
|
||||
throw new Exception("Failed to connect to MySQL: (".$this->mysqliW->connect_errno.") ".$this->mysqliW->connect_error);
|
||||
}
|
||||
|
||||
if ($this->mysqliR->connect_errno) {
|
||||
throw new Exception("Failed to connect to MySQL: (".$this->mysqliR->connect_errno.") ".$this->mysqliR->connect_error);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Override standard mysqli_prepare to select master/slave server
|
||||
* @param $string query
|
||||
*
|
||||
* @return mysqli_stmt
|
||||
*/
|
||||
public function prepare($query)
|
||||
{
|
||||
if (stripos($query, "SELECT") && stripos($query, "FOR UPDATE") === false && $this->mysqliR !== null) {
|
||||
return $this->mysqliR->prepare($query);
|
||||
} else {
|
||||
return $this->mysqliW->prepare($query);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Override standard mysqli_query to select master/slave server
|
||||
* @param string $query
|
||||
* @param int $resultmode
|
||||
*
|
||||
* @return boolean
|
||||
* @return mixed
|
||||
*/
|
||||
public function query($query, $resultmode = MYSQLI_STORE_RESULT)
|
||||
{
|
||||
if (stripos($query, "SELECT") && stripos($query, "FOR UPDATE") === false && $this->mysqliR !== null) {/* Use readonly server */
|
||||
return $this->mysqliR->query($query, $resultmode);
|
||||
} else {
|
||||
return $this->mysqliW->query($query, $resultmode);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -60,6 +60,18 @@ $config['db']['name'] = 'mpos';
|
||||
// $config['db']['shared']['workers'] = $config['db']['name'];
|
||||
// $config['db']['shared']['news'] = $config['db']['name'];
|
||||
|
||||
|
||||
/**
|
||||
* Setup read-only/slave database server for selects (read queries)
|
||||
**/
|
||||
$config['db-ro']['enabled'] = false;
|
||||
$config['db-ro']['host'] = 'localhost';
|
||||
$config['db-ro']['user'] = 'someuser';
|
||||
$config['db-ro']['pass'] = 'somepass';
|
||||
$config['db-ro']['port'] = 3306;
|
||||
$config['db-ro']['name'] = 'mpos';
|
||||
|
||||
|
||||
/**
|
||||
* Local wallet RPC
|
||||
* RPC configuration for your daemon/wallet
|
||||
|
||||
@ -3,13 +3,14 @@ $defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
|
||||
|
||||
// Instantiate class, we are using mysqlng
|
||||
if ($config['mysql_filter']) {
|
||||
$mysqli = new mysqli_strict($config['db']['host'], $config['db']['user'], $config['db']['pass'], $config['db']['name'], $config['db']['port']);
|
||||
$mysqli = new mysqlims($config['db'],$config['db-ro'], true);
|
||||
} else {
|
||||
$mysqli = new mysqli($config['db']['host'], $config['db']['user'], $config['db']['pass'], $config['db']['name'], $config['db']['port']);
|
||||
$mysqli = new mysqlims($config['db'],$config['db-ro'], false);
|
||||
}
|
||||
|
||||
// Check if read-only and quit if it is on
|
||||
if ($mysqli->query('/* MYSQLND_MS_MASTER_SWITCH */SELECT @@global.read_only AS read_only')->fetch_object()->read_only == 1) {
|
||||
// Check if read-only and quit if it is on, disregard if slave is enabled
|
||||
|
||||
if ($mysqli->query('/* MYSQLND_MS_MASTER_SWITCH */SELECT @@global.read_only AS read_only')->fetch_object()->read_only == 1 && $config['db-ro']['enabled'] === false ) {
|
||||
die('Database is in READ-ONLY mode');
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user