Skip to content

Commit 52c3e2f

Browse files
committed
Implemented Read/Write Mysql Split (Master for writes, Slave for reads)
1 parent 064ab5a commit 52c3e2f

File tree

4 files changed

+103
-4
lines changed

4 files changed

+103
-4
lines changed

include/autoloader.inc.php

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
if ($config['mysql_filter']) {
1010
require_once(CLASS_DIR . '/strict.class.php');
1111
}
12+
require_once(INCLUDE_DIR . '/classes/mysqlims.class.php');
1213
require_once(INCLUDE_DIR . '/database.inc.php');
1314
require_once(INCLUDE_DIR . '/config/memcache_keys.inc.php');
1415
require_once(INCLUDE_DIR . '/config/error_codes.inc.php');

include/classes/mysqlims.class.php

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/*
3+
* This class will run queries on master/slave servers depending on the query itself.
4+
*/
5+
class mysqlims extends mysqli
6+
{
7+
private $mysqliW;
8+
private $mysqliR = null;
9+
10+
/*
11+
* Pass main and slave connection arrays to the constructor, and strict as true/false
12+
*
13+
* @param array $main
14+
* @param array $slave
15+
* @param boolean $strict
16+
*
17+
* @return void
18+
*/
19+
public function __construct($main, $slave = false, $strict = false)
20+
{
21+
var_dump($main);
22+
var_dump($slave);
23+
if ($strict) {
24+
$this->mysqliW = new mysqli_strict($main['host'],
25+
$main['user'], $main['pass'],
26+
$main['name'], $main['port']);
27+
if ($slave && is_array($slave) && isset($slave['enabled']) && $slave['enabled']
28+
=== true) {
29+
$this->mysqliR = new mysqli_strict($slave['host'],
30+
$slave['user'], $slave['pass'],
31+
$slave['name'], $slave['port']);
32+
}
33+
} else {
34+
$this->mysqliW = new mysqli($main['host'],
35+
$main['user'], $main['pass'],
36+
$main['name'], $main['port']);
37+
if ($slave && is_array($slave) && isset($slave['enabled']) && $slave['enabled']
38+
=== true) {
39+
$this->mysqliR = new mysqli($slave['host'],
40+
$slave['user'], $slave['pass'],
41+
$slave['name'], $slave['port']);
42+
}
43+
}
44+
45+
if ($this->mysqliW->connect_errno) {
46+
throw new Exception("Failed to connect to MySQL: (".$this->mysqliW->connect_errno.") ".$this->mysqliW->connect_error);
47+
}
48+
49+
if ($this->mysqliR->connect_errno) {
50+
throw new Exception("Failed to connect to MySQL: (".$this->mysqliR->connect_errno.") ".$this->mysqliR->connect_error);
51+
}
52+
}
53+
54+
/*
55+
* Override standard mysqli_prepare to select master/slave server
56+
* @param $string query
57+
*
58+
* @return mysqli_stmt
59+
*/
60+
public function prepare($query)
61+
{
62+
if (stripos($query, "SELECT") && stripos($query, "FOR UPDATE") === false && $this->mysqliR !== null) {
63+
return $this->mysqliR->prepare($query);
64+
} else {
65+
return $this->mysqliW->prepare($query);
66+
}
67+
}
68+
69+
/*
70+
* Override standard mysqli_query to select master/slave server
71+
* @param string $query
72+
* @param int $resultmode
73+
*
74+
* @return boolean
75+
* @return mixed
76+
*/
77+
public function query($query, $resultmode = MYSQLI_STORE_RESULT)
78+
{
79+
if (stripos($query, "SELECT") && stripos($query, "FOR UPDATE") === false && $this->mysqliR !== null) {/* Use readonly server */
80+
return $this->mysqliR->query($query, $resultmode);
81+
} else {
82+
return $this->mysqliW->query($query, $resultmode);
83+
}
84+
}
85+
}

include/config/global.inc.dist.php

+12
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@
6060
// $config['db']['shared']['workers'] = $config['db']['name'];
6161
// $config['db']['shared']['news'] = $config['db']['name'];
6262

63+
64+
/**
65+
* Setup read-only/slave database server for selects (read queries)
66+
**/
67+
$config['db-ro']['enabled'] = false;
68+
$config['db-ro']['host'] = 'localhost';
69+
$config['db-ro']['user'] = 'someuser';
70+
$config['db-ro']['pass'] = 'somepass';
71+
$config['db-ro']['port'] = 3306;
72+
$config['db-ro']['name'] = 'mpos';
73+
74+
6375
/**
6476
* Local wallet RPC
6577
* RPC configuration for your daemon/wallet

include/database.inc.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
// Instantiate class, we are using mysqlng
55
if ($config['mysql_filter']) {
6-
$mysqli = new mysqli_strict($config['db']['host'], $config['db']['user'], $config['db']['pass'], $config['db']['name'], $config['db']['port']);
6+
$mysqli = new mysqlims($config['db'],$config['db-ro'], true);
77
} else {
8-
$mysqli = new mysqli($config['db']['host'], $config['db']['user'], $config['db']['pass'], $config['db']['name'], $config['db']['port']);
8+
$mysqli = new mysqlims($config['db'],$config['db-ro'], false);
99
}
1010

11-
// Check if read-only and quit if it is on
12-
if ($mysqli->query('/* MYSQLND_MS_MASTER_SWITCH */SELECT @@global.read_only AS read_only')->fetch_object()->read_only == 1) {
11+
// Check if read-only and quit if it is on, disregard if slave is enabled
12+
13+
if ($mysqli->query('/* MYSQLND_MS_MASTER_SWITCH */SELECT @@global.read_only AS read_only')->fetch_object()->read_only == 1 && $config['db-ro']['enabled'] === false ) {
1314
die('Database is in READ-ONLY mode');
1415
}
1516

0 commit comments

Comments
 (0)