Monday 8 April 2013

How to display memcache statistics at bash shell

To display memcache statistics at command prompt, you can enter below command. This command will display memcache statistics like "top", updating the statistic every 2s.

watch "echo stats|nc 127.0.0.1 11211"

How to display memcache statistics with php

To display memcache statistics, you can create a php script as shown below. This scripts uses "getStats" command to display the statistics. You can also format it into a html table.

<?php
$memcache_host="127.0.0.1";
$memcache_port="11211";
$memcache = new Memcache;
$memcache->connect($memcache_host,$memcache_port) or die ("Cannot connect");
$return=($memcache->getStats());
foreach ($return as $key => $value) {
    echo $key." : ".$value."<br>";   
}
?>

How to use memcache with php


Previously I posted an article on how to install Memcache. This time I will show how to use Memcache with PHP. This program will start off with a SQL query statement, hashing the statement and check if the data is stored in memcache.If found, the data will be retrieve from memcache. If it is not found in memcache, then it will query from MySQL database.


<?php
require('connect_db.php'); 
$memcache_host="127.0.0.1";
$memcache_port="11211";
$memcache = new Memcache;
$memcache->connect($memcache_host,$memcache_port) or die ("Cannot connect");

$version = $memcache->getVersion();
echo "Version: ".$version;

$query="select * from exampleDB limit 1;";
$key = md5($query); 
$get_result = $memcache->get($key);

if ($get_result) {   
    print_r($get_result);
    echo "Data retrieved from memcache";
}
else {    
    $result = mysql_query($query);
    if (!$result) {
        die('Invalid query: ' . mysql_error()) 
      }
    $row = mysql_fetch_array($result);
    print_r($row);  
    $memcache->set($key, $row, MEMCACHE_COMPRESSED, 10); 
    echo "Data retrieved from database";
}  
?>

Thursday 4 April 2013

Enable EPEL and IUS repository for CENTOS6

What is EPEL? Extra Packages for Enterprise Linux (or EPEL) is a Fedora Special Interest Group that creates, maintains, and manages a high quality set of additional packages for Enterprise Linux Server, including, but not limited to, Red Hat Enterprise Linux (RHEL), CentOS and Scientific Linux (SL). First step, download repository rpm from website:
wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

Install the rpm:
rpm -Uvh epel-release-6-8.noarch.rpm
What is IUS? The IUS Community Project is an effort to package rpms of the latest stable versions of the most commonly requested software on Red Hat Enterprise Linux and CentOS. IUS provides a better way to upgrade PHP/MySQL/Python/Etc on RHEL or CentOS. The project is run by professional Linux Engineers that are primarily focused on RPM Development in the web hosting industry. Download repository rpm from website:
wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/x86_64/ius-release-1.0-11.ius.el6.noarch.rpm 

Install the rpm:
rpm -Uvh ius-release-1.0-11.ius.el6.noarch.rpm 

Wednesday 3 April 2013

Install memcache on CENTOS6


This guide will explain how to install memcache.

To install the server:

yum search memcached
yum install memcached php-pear
chkconfig memcached on
service memcached start

To install php,pecl,memcache extension:

pecl search memcache

The latest stable version is memcache 2.2.7.

You need to install dependencies before installing memcache:
yum install php-devel gcc zlib zlib-devel
To install php extension:
pecl install memcache

Insert the extension in php.ini:
vi /etc/php.ini

Insert below line to php.ini:
extension=memcache.so


To create php test script:

vi /var/www/html/memcache.php

<?php

$memcache = new Memcache;
$memcache->;connect('127.0.0.1', 11211) or die ("Cannot connect");

$version = $memcache->getVersion();
echo "Version: ".$version."
\n"; $tmp_object = new stdClass; $tmp_object->test1 = 'value1'; $tmp_object->test2 = 472; $memcache->set('key', $tmp_object, false, 20) or die ("Cannot save data"); echo "Stored data in the memcache for 20 seconds
\n"; $get_result = $memcache->get('key'); echo "Data from the cache:
\n"; var_dump($get_result); ?>

Tuesday 2 April 2013

Install the latest mysql server on CENTOS6

How to install the latest mysql server on CENTOS6? CENTOS6 shipped with mysql server 5.1.67 version. To install latest mysql server 5.6.10, you can download the rpm from mysql official download site, and install the rpm individually.

Firstly, navigate to "http://www.mysql.com/downloads/mysql/#downloads" , choose "Oracle and Red Hat Linux 6". Download the rpms as shown below:

wget http://www.mysql.com/get/Downloads/MySQL-5.6/MySQL-client-5.6.10-1.el6.x86_64.rpm/from/http://cdn.mysql.com/
wget http://www.mysql.com/get/Downloads/MySQL-5.6/MySQL-shared-5.6.10-1.el6.x86_64.rpm/from/http://cdn.mysql.com/
wget http://www.mysql.com/get/Downloads/MySQL-5.6/MySQL-server-5.6.10-1.el6.x86_64.rpm/from/http://cdn.mysql.com/
wget http://www.mysql.com/get/Downloads/MySQL-5.6/MySQL-shared-compat-5.6.10-1.el6.x86_64.rpm/from/http://cdn.mysql.com/


Then use md5sum, to check md5 hash on the download site:

md5sum MySQL-shared-compat-5.6.10-1.el6.x86_64.rpm
md5sum MySQL-server-5.6.10-1.el6.x86_64.rpm
md5sum MySQL-shared-5.6.10-1.el6.x86_64.rpm
md5sum MySQL-client-5.6.10-1.el6.x86_64.rpm

If the md5 hash is ok, we can install the rpm in following sequence:

rpm -Uvh MySQL-shared-compat-5.6.10-1.el6.x86_64.rpm
rpm -ivh MySQL-shared-5.6.10-1.el6.x86_64.rpm
rpm -ivh MySQL-server-5.6.10-1.el6.x86_64.rpm
rpm -ivh MySQL-client-5.6.10-1.el6.x86_64.rpm

To turn mysql on when boot:
chkconfig mysql on


To start the mysql service:
service mysql start


To obtain the temporary password:
cat /root/.mysql_secret


To change the mysql root password:
mysqladmin -uroot -p password


Login to the newly installed server using its client:
mysql -uroot -p
These are the steps to install latest mysql on CENTOS. Cheers!

Benchmark PHP mysql vs PHP mysqli

In PHP coding, many of my php scripts are still using php mysql extension. Before taking the time to migrate to php mysqli extension, I have decided to run a simple benchmark between the two. I have written 2 scripts and time it. The first script is "test_mysql.php", the second script is "test_mysqli.php". The result is, the first script took an average of 1.01 sec, the second script took 0.86 sec. "test_mysqli.php" is 17.87% faster than "test_mysql.php".

The scripts were tested on Linux 3.8.4, PHP 5.4.12, MySQL 5.5.30.

First script, test_mysql.php

<?php
//-----Start Timer-------------------------------------------------//       
    $time = microtime();
    $time = explode(" ", $time);
    $time = $time[1] + $time[0];
    $top = $time;
//-----Start Timer-------------------------------------------------//   
for($j=0;$j<1000;$j++)
{  
    $link = mysql_connect($host,$user,$password) or die('Error in Server information');   
    mysql_select_db($db,$link) or die('Can not Select Databasse');   
    $query="select * from com_list where field1='value1';";
    $res = mysql_query($query);  //1 is ok
    if (!$res) {
        die('Invalid query: ' . mysql_error()); }

    while($row = mysql_fetch_array($res)){
    $i++;
    }
    mysql_free_result($res);
    mysql_close($link);
}
    echo "Total lines :".$i;
//-----End Timer-------------------------------------------------//   
    $time = microtime();
    $time = explode(" ", $time);
    $time = $time[1] + $time[0];
    $bottom = $time;
//-----End Timer-------------------------------------------------//
    $loadtime = ($bottom-$top);
    echo ("<br> <br>This page generated in $loadtime seconds");  
?>
Second script, test_mysqli.php
<?php
//-----Start Timer-------------------------------------------------//       
    $time = microtime();
    $time = explode(" ", $time);
    $time = $time[1] + $time[0];
    $top = $time;
//-----Start Timer-------------------------------------------------//       
for($j=0;$j<1000;$j++)
{  
    $link = mysqli_connect($host,$user,$password,$db) or die('Error in Server information');
    $query="select * from com_list where field1='value1';";
    $res = mysqli_query($link,$query);  //1 is ok
    if (!$res) {
        die('Invalid query: ' . mysql_error()); }
    while($row = mysqli_fetch_array($res)){        
    $i++;
    }   
    mysqli_free_result($res);
    mysqli_close($link);
}
    echo "Total lines :".$i;
//-----End Timer-------------------------------------------------//       
    $time = microtime();
    $time = explode(" ", $time);
    $time = $time[1] + $time[0];
    $bottom = $time;
//-----End Timer-------------------------------------------------//           
    $loadtime = ($bottom-$top);
    echo ("<br> <br>This page generated in $loadtime seconds");  
?>



search iomeweekly