Installing the PHP HandlerSocket Client

18 Dec

Download and Extract HandlerSocket Source Code

  • NOTE: If you are in a development environment where the Server-Side MySQL HandlerSocket service and the Apache/PHP service are in the
    same box, then you can skip this step.

Compile and Build HandlerSocket Client Code

  • cd /opt/mysqlcluster/src/handler_src
  • ./autogen.sh
  • ./configure –disable-handlersocket-server
  • make && make install

Install/Setup PHP HandlerSocket Client

  1. mkdir -p /opt/php_hs
  2. cd /opt/php_hs
  3. wget http://php-handlersocket.googlecode.com/files/php-handlersocket-0.3.0.tar.gz
  4. tar xvf php-handlersocket-0.3.0.tar.gz
  5. cd cd php-handlersocket
  6. Ensure the phpize command from /usr/bin is executed.
    1. export PATH=/usr/bin:$PATH
  7. make clean (If this is your first time building PHP HandlerSocket, this command will fail and that is ok)
  8. phpize
  9. ./configure
  10. make
  11. make install
    • This will copy handlersocket.so to /usr/lib64/php/modules on 64 bit OS (production systems)
    • Will copy handlersocket.so /usr/lib/php/modules on 32 bit OS (Dev systems)
  12. Specify handlersocket.so as a PHP extension
    1. echo “extension=handlersocket.so” > /etc/php.d/handlersocket.ini
  13. Restart Apache
    1. service httpd restart

Setup Sample Database

  • Setup a test database and innodb table:
    • mysql -h 127.0.0.1 -u myuser -p OR mysql -h 127.0.0.1 -u root depending on your environment.
    • mysql> create database hstestdb;
    • mysql> CREATE TABLE user (user_id INT UNSIGNED PRIMARY KEY,user_name VARCHAR(50),user_email VARCHAR(255),created Timestamp DEFAULT CURRENT_TIMESTAMP) ENGINE=InnoDB;
    • mysql> insert into user values (101, “Hulk Hogan”, “hulk@hogan.com”, current_timestamp());
    • mysql> insert into user values (102, “The Rock”, “the@rock.com”, current_timestamp());
    • mysql> SELECT user_name, user_email, created FROM user WHERE user_id=101;

Sample PHP Code to access the above Database/Table using HandlerSocket API:

Client API

SELECT example

$host = 'ec2-50-17-110-253.compute-1.amazonaws.com';
$port = 9998;
$port_wr = 9999;
$dbname = 'hstestdb';
$table = 'user';

// SELECT SINGLE AND SELECT MULTI $hs = new \HandlerSocket($host, $port);
if (!($hs->openIndex(1, $dbname, $table, \HandlerSocket::PRIMARY, 'user_name,user_email,created')))
{

    echo $hs->getError(), PHP_EOL; die();
}

$retval = $hs->executeSingle(1, '=', array('101'), 1, 0);

var_dump($retval);

$retval = $hs->executeMulti(
    array(array(1, '=', array('101'), 1, 0),
          array(1, '=', array('102'), 1, 0)));

var_dump($retval);

unset($hs);

UPDATE Example

$host = 'ec2-50-17-110-253.compute-1.amazonaws.com';
$port = 9998;
$port_wr = 9999;
$dbname = 'hstestdb';
$table = 'user';
// UPDATE $hs = new \HandlerSocket($host, $port_wr);
if (!($hs->openIndex(2, $dbname, $table, '', 'user_name')))
{
    echo $hs->getError(), PHP_EOL;
    die();
}

if ($hs->executeUpdate(2, '=', array('101'), array('Hulkier Hogan'), 1, 0) === false)
{
    echo $hs->getError(), PHP_EOL;
    die();
}

unset($hs);

INSERT EXAMPLE

$host = 'ec2-50-17-110-253.compute-1.amazonaws.com';
$port = 9998;
$port_wr = 9999;
$dbname = 'hstestdb';
$table = 'user';
// INSERT $hs = new \HandlerSocket($host, $port_wr);
if (!($hs->openIndex(3, $dbname, $table, '', 'user_id,user_name,user_email')))
{
    echo $hs->getError(), PHP_EOL;
    die();
}

if ($hs->executeInsert(3, array(103,'Mr. Perfect', 'mr@perfect.com')) === false)
{
    echo $hs->getError(), PHP_EOL;
}

unset($hs);

exit;

DELETE EXAMPLE

$host = 'ec2-50-17-110-253.compute-1.amazonaws.com';
$port = 9998;
$port_wr = 9999;
$dbname = 'hstestdb';
$table = 'user';
//DELETE $hs = new \HandlerSocket($host, $port_wr);
if (!($hs->openIndex(4, $dbname, $table, '', '')))
{
    echo $hs->getError(), PHP_EOL;
    die();
}

if ($hs->executeDelete(4, '=', array(103)) === false)
{
    echo $hs->getError(), PHP_EOL;
    die();
}

References

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: