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");