• Uncategorized

About mysql : How-to-populate-zone-tables-in-mysql-database-within-ubuntu-with-xampp

Question Detail

I am trying to import time zones according to this document: http://dev.mysql.com/doc/refman/5.7/en/mysql-tzinfo-to-sql.html.

When I try hitting even first command through terminal i.e.

mysql_tzinfo_to_sql tz_dir

it says

There were fatal errors during processing of zoneinfo directory 'tz_dir'

When I run:

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql

then it returns

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

Operating System: Ubuntu

Server: XAMPP (having mariadb)

So, I am not able to import timezone data into the zone tables of mysql database.

Question Answer

I also had this issue this morning while trying to populate the timezone tables on our production server (using CentOS).

I worked around this problem by exporting the table creation script from my development computer (populating the tables on Windows simply requires to download the tables and copy them in the proper directory).

I tweaked the creation script a little bit and it is now working for me:

I cannot post it in my answers since the script is too long.

https://drive.google.com/file/d/0B7iwL5F-hwr_YkItRXk2Z1VZdlE/view?usp=sharing

Here’s a version with the comment (but it doesn’t seems to work, so use the version without comments).

https://drive.google.com/file/d/0B7iwL5F-hwr_dWdjTDREcXNHQmM/view?usp=sharing

The script should take no more than a few second to run. You’ll probably need to use the root user to be able to run it.

You can use this query in order to validate the installation:

SELECT CONVERT_TZ(CURRENT_DATE(),'UTC','America/Montreal');

If it returns NULL instead of a datetime, it means the script failed.

Good success

The

There were fatal errors during processing of zoneinfo directory 'tz_dir'

error message means that the directory cannot be read (not enough access rights or not even exists).

Knowing that the mysql_tzinfo_to_sql program is just a tool that converts a bunch of timezone files into an SQL script that you can use to install the time zones for mysql, your task is the following:

  • obtain the timezone files from somewhere
  • execute mysql_tzinfo_to_sql to create an SQL script from those files
  • execute that SQL script in your mysql database.

These steps can be performed on different computers if you transfer the files between them. For example, I installed the timezones on a machine where the mysql installation was not complete, that is, mysql_tzinfo_to_sql was not available and I wasn’t able to install it either.

In such a case you can combine the following steps:

  • if mysql_tzinfo_to_sql is not available on the computer where your mysql database resides then find a computer where mysql_tzinfo_to_sql is already installed
  • make the zoneinfo folder available on that computer. It is just a bunch of files in different folders so you can transport them in a gzip file from one computer to the other. In a normal mysql installation this folder should exist but maybe your installation is not complete, su just get it from anywhere.
  • execute the mysql_tzinfo_to_sql command to create an SQL script like this:

    mysql_tzinfo_to_sql path-to-your-zoneinfo-folder >install_mysql_zoneinfo.sql

  • move the created SQL script to the computer where your mysql database resides *

  • execute the script like this:

    mysql --user=root --password=abc123 mysql <install_mysql_zoneinfo.sql

Adjust the username and password if needed and your script will be executed. This will fill up the timezone-related tables with the appropriate values and you will be able to use them:

SELECT convert_tz(NOW(),'UTC','Australia/Melbourne'); 
  • if you can reach the mysql database from the computer where the SQL script was generated then it’s enough to add the -h <hostname> command line argument to the subsequent script-executing program and you will not have to copy the SQL script to the target machine.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.