Monday, October 7, 2013

How to set "auto_increment_increment" and "auto_increment_offset" - MySQL

Referred web sites:

What is "auto_increment_increment" and "auto_increment_offset" ?

Both "auto_increment_increment" and "auto_increment_offset" are system variables which required in MySQL Master-Master replication. By changing above variables you will be able to manage auto increment columns and avoid from data conflict issues in your database.

What does it do ?

  • "auto_increment_increment" controls the interval between successive column values
  • auto_increment_offset determines the starting point for the AUTO_INCREMENT column value

How to access MySQL monitor (Ubuntu 12.04)

  • Open Terminal window using "Dash Home".
  • Log into MySQL Monitor (You may need a valid username and password)
  • mysql
    or
    mysql -uroot
    or
    mysql -uroot -p


  • View existing(default) system variables ("auto_increment_increment" and "auto_increment_offset")
  • mysql> SHOW VARIABLES LIKE 'auto_inc%';
     


  • Create a Database (You will be able to test this using your existing Database)
  • mysql> CREATE DATABASE TEST_DB;
  • Select created Database to use
  • mysql> USE TEST_DB;
  • Create a table
  • mysql> CREATE TABLE TestTable
        -> (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
  • Insert sample records
  • mysql> INSERT INTO TestTable VALUES (NULL), (NULL), (NULL), (NULL);
  • View inserted records
  • mysql> SELECT * FROM TestTable;


  • Set "auto_increment_increment" to 5 and insert another sample records
  • mysql> SET @@auto_increment_increment=5;
    mysql> INSERT INTO TestTable VALUES (NULL), (NULL), (NULL), (NULL);
    mysql> SELECT * FROM TestTable;


  • Now you know how it works. You can create another table to test "auto_increment_offset"
    • You can drop your "TestTable" and follow above instructions. But make sure to keep the table without any data.
    • Also do not forget to set "auto_increment_increment"
  • Now you can set "auto_increment_offset"
  • mysql> SET @@auto_increment_offset=5;
  • Then insert few sample records
  • mysql> INSERT INTO TestTable VALUES (NULL), (NULL), (NULL), (NULL);



  • This is how "auto_increment_increment" and "auto_increment_offset" works.

Something to remember.

  • You will be able to set "auto_increment_offset" greater then (>) "auto_increment_increment" and test again. But the value of "auto_increment_offset" will be ignore by the system.


  • Because if the value of "auto_increment_offset" is greater than that of "auto_increment_increment", the value of auto_increment_offset is ignored.

Hardcode variables in my.cnf:

Also you can hardcode followings settings in your my.cnf file (/etc/my.cnf)
# The MySQL server
[mysqld]

auto-increment-increment = 2 # No of replication servers
auto-increment-offset = 1 # For first replication server

Tuesday, October 1, 2013

How to fix "It is not safe to rely on the system's timezone settings"

If you see one of following warnings when you run a PHP script:


  • PHP Warning:  mktime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Calcutta' for 'IST/5.0/no DST' instead in /opt/websites/www/sample.php on line 10
  • PHP Warning:  date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Calcutta' for 'IST/5.0/no DST' instead in  /opt/websites/www/sample.php on line 11
PHP Warning:  mktime(): It is not safe to rely on the system's timezone settings.
PHP Warning:  date(): It is not safe to rely on the system's timezone settings.

You can try following solutions:

Solution 01

  1. Find your timezone settings

    (For Ubuntu)
    cat /etc/timezone 

    (For Fedora/CentOS/RedHat)
    cat /etc/sysconfig/clock 

     

    Figure:  Ubuntu 12.04 Terminal

    or simply select your timezone from http://php.net/manual/en/timezones.php
  2. Find your php.ini file to edit

    (For Ubuntu)
    cat /etc/php5/apache2/php.ini

    (For Fedora/CentOS/RedHat)
    cat /etc/php.ini

    or You can locate all php.ini files using
    locate php.ini

  3. Edit php.ini

    Now insert following date-time settings into php.ini (replace "Asia/Colombo" using your timezone settings)
    [Date]
    ; Defines the default timezone used by the date functions
    ; http://php.net/date.timezone
    date.timezone = Asia/Colombo
  4. Restart Apache

    (For Ubuntu)
    /etc/init.d/apache2 restart
    or
    service apache2 restart
    (For Fedora/CentOS/RedHat)
    /etc/init.d/httpd restart
    or
    service httpd restart

Solution 02

  1. Edit your php script

    if you are unable to access your php.ini, can set the default timezone at the beginning of your PHP scripts.
    <?php
    date_default_timezone_set("Asia/Colombo");

Friday, September 6, 2013

Alternative PHP Cache (APC) - Installation guide.

Referred web sites:

  • http://www.php.net/manual/en/book.apc.php
  • http://php.find-info.ru/php/016/ch20lev1sec1.html
  • http://linuxaria.com/howto/everything-you-need-to-know-about-apc-alternate-php-cache?lang=en
  • http://lampzone.wordpress.com/2010/03/26/how-does-apc-work/

What is APC ?

  • APC is stands for Alternative PHP Cache
  • APC is a free and open opcode cache for PHP
  • Its goal is to provide a free, open, and robust framework for caching and optimizing PHP intermediate code.

Who need APC ?

  • System Administrator who need increase the performance of a PHP web site.
  • Web designers/developers or programmers who develop web applications using Yii,WordPress,Drupal or any framework.

How can I find more information about APC ?

  • You can read about apc on http://www.php.net/manual/en/book.apc.php

How APC works ?

  • First of all, You need to know how a PHP application run by the Zend Engine (Opcodes and Op Arrays)
    1. The first step is reading the PHP code from the filesystem and put it into memory.
    2. Lexing : The php code inside is converted into tokens or Lexicons.
    3. Parsing : During this stage, tokens are processed to derive at meaningful expressions.
    4. Compiling : The derived expressions are compiled into opcodes.
    5. Executing : Opcodes are executed to get the final result.
  • The goal of APC is bypass the steps from 1 to 4, caching in a shared memory segment the opcodes generated and then copies them into The execution process so Zend can actually execute the opcodes.

How to install APC ?

  • There are 2 ways to install APC
    1. Install APC is with PECL
      • With this method you’ll download the last source and compile it for you computer.
      • For Debian and Ubuntu
      • sudo apt-get install php-pear php5-dev apache2-threaded-dev build-essential
        sudo pecl install apc
    2. Install APC from a repository of your distribution.
      • On Debian and Ubuntu APC is available on the main repository with this name:
      • sudo apt-cache search apc php
        php-apc - APC (Alternative PHP Cache) module for PHP 5
        
        sudo aptitude install php-apc
        
      • On Red Hat Enterprise, Centos or Fedora you can use this command:
      • sudo yum install php-pecl-apc

APC configurations

  • Once installed APC must be enabled in the configuration file of PHP: php.ini, if you have installed APC from a package this is already done and you just have to restart Apache (or FastCGI daemon like FPM) to see a new APC section in phpinfo() which will confirm it’s enabled.
  • Otherwise you have to include in your php.ini this line:
  • extension=apc.so

How to set APC runtime configuration ?