PHP Classes

PHP MySQL Cluster Connection: Access MySQL clusters using separate connections

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 69%Total: 404 All time: 6,533 This week: 46Up
Version License PHP version Categories
mysqli-cluster 1.0GNU General Publi...5.6.16PHP 5, Databases, Performance and opt...
Description 

Author

This class can access MySQL clusters using separate connections.

It takes an array with parameters to connect to different servers of a MySQL cluster with the distinction of being for read or write query servers.

The class checks the type of query being executed and route them to a read server if it is of the SELECT type or to a write server to INSERT, UPDATE and DELETE queries.

The class can also reconect to another MySQL available server if the current server that it attempted to access is down.

If any errors happen, the class sends an email message to an address configured for the respective cluster server.

Innovation Award
PHP Programming Innovation award nominee
February 2016
Number 9


Prize: SourceGuarding PHP encoder tool
Database clusters are used to distribute the load of servers when they need to execute many queries at the same time.

Usually there are server machines in the cluster that perform only write operations and others perform only read operations.

This class can connect to different servers of a MySQL database cluster. It sends queries to be executed to the right type of server depending on whether those are read or write queries.

Manuel Lemos
Picture of Mihails Atamanskis
  Performance   Level  
Name: Mihails Atamanskis <contact>
Classes: 1 package by
Country: Latvia Latvia
Age: 40
All time rank: 354010 in Latvia Latvia
Week rank: 192 Up2 in Latvia Latvia Up
Innovation award
Innovation award
Nominee: 1x

Example

<?php

$databases
= array();

$i=0;
// Primary database

$databases[$i]['number'] = $i; //server in cluster unique number
$databases[$i]['role'] = 'read'; //server role: read/write
$databases[$i]['db_host'] = '10.0.0.1'; //hostname (ip or domain)
$databases[$i]['db_name'] = 'database'; //database name
$databases[$i]['db_user'] = 'user'; //database user
$databases[$i]['db_pass'] = 'password'; //database password
$databases[$i]['error_email'] = 'my@email.com'; //database error report email

//Second database
$i++;
$databases[$i]['number'] = $i; //server in cluster unique number
$databases[$i]['role'] = 'write'; //server role: read/write . If only one server in cluster then it use for read & write. if
$databases[$i]['db_host'] = '10.0.0.2'; //hostname (ip or domain)
$databases[$i]['db_name'] = 'database'; //database name
$databases[$i]['db_user'] = 'user'; //database user
$databases[$i]['db_pass'] = 'password'; //database password
$databases[$i]['error_email'] = 'my@email.com'; //database error report email

require_once 'mysql_cluster.php';

$db = new db_cluster($databases);

# Uncoment if need test shutdown one of server
//echo "sleep start\n";
//sleep(70); #mysql timeout set to 60 seconds, we must get error and select other mysql server
//echo "sleep stop\n";

//Example 1
$sql_results = $db->query("Select * from users LIMIT 10");
var_dump($db->get_row($sql_results));

//Multy rows example
$sql_results = $db->query("Select * from users LIMIT 10");
if (
$db->num_rows($sql_results) > 0) {
    while (
$row = $db->get_row($sql_results)){
       
//Some your code
   
}
}


  Files folder image Files (3)  
File Role Description
Accessible without login Plain text file example.php Example Simple example
Plain text file mysql.php Class Single server working class
Plain text file mysql_cluster.php Class Cluster server working class

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:404
This week:0
All time:6,533
This week:46Up
User Ratings User Comments (4)
 All time
Utility:100%StarStarStarStarStarStar
Consistency:95%StarStarStarStarStar
Documentation:-
Examples:100%StarStarStarStarStarStar
Tests:-
Videos:-
Overall:69%StarStarStarStar
Rank:328
 
Thats a very good class ;-)
8 years ago (José Filipe Lopes Santos)
70%StarStarStarStar
Thats a very good class ;-)
8 years ago (José Filipe Lopes Santos)
70%StarStarStarStar
Thats a very good class ;-)
8 years ago (José Filipe Lopes Santos)
70%StarStarStarStar
Thats a very good class ;-)
8 years ago (José Filipe Lopes Santos)
70%StarStarStarStar