Changing the connection timezone in MySQL - mysql

My server is running in MDT
So I used following one to convert EST
SET time_zone = '-5:00'
But, how can I switch it to EDT because
While `EST` is only valid in winter, while in summer how to change it to `EDT`
I just try to used named time zone. But I am getting following error
#1298 - Unknown or incorrect time zone: 'America/Toronto'
I don't understand how to solve this problem
How can i switch
UTC -05 TO UTC-04

For Linux, BSD, and Mac OS X if you need to load the timezone table do this:
$ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -uroot mysql

Since you're using Windows, your going to have to import the time zone description tables to your MySQL server.
http://dev.mysql.com/downloads/timezones.html

For me on Windows using WampServer:
download the POSIX version from https://dev.mysql.com/downloads/timezones.html
unzip it
put all files in wamp mysql data: C:\wamp\bin\mysql\mysql5.7.11\data\mysql
restart wamp
More Info:
“To use a time zone package that contains .frm, .MYD, and .MYI files for the MyISAM time zone tables, download and unpack it. These table files are part of the mysql database, so you should place the files in the mysqlsubdirectory of your MySQL server's data directory. Stop the server before doing this and restart it afterward”
http://dev.mysql.com/doc/refman/5.7/en/time-zone-support.html#time-zone-installation

In case of Mysql8 and want to set EDT timezone 'America/New_York' then follow the below steps:
Go to the /etc/my.cnf
Add this under [mysqld]
default-time-zone='America/New_York'
systemctl restart mysqld
If you face the issue realted fatal error like this:
Fatal error: Illegal or unknown default time zone
Then do the following steps:
first remove the entry from /etc/my.cnf file for default-time-zone='America/New_York'
Go to shell(exit from mysql) and run the command
$ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
Add the line again
Restart mysql

Have you tried using a zoneinfo name such as "America/New_York" instead of the abbreviation (which is only valid for half the time)?
Ideally, you shouldn't use the server time zone at all of course - store dates in UTC and then convert it to the appropriate time zone in the client code. (This does depend on what you're storing, mind you. Future date/time values specified by a user with a time zone should be stored that way, not converted to any other time zone, in case the rules change between now and the future date/time. But for instants in time recorded by machines, e.g. for logging, transactions etc, I'd definitely store UTC. I would never suggest storing "just the local time and assume that the server and the client have the same rules".)

Just for the sake of completeness, Mac users can find mysql_tzinfo_to_sql utility in /usr/local/mysql/bin directory.
Hence the complete command will be /usr/local/mysql/bin/mysql_tzinfo_to_sql /usr/share/zoneinfo | ./mysql -p -u root mysql
I spent hours to find the utility, the above path may save yours!

Related

MySQLdump backup script no longer works, getting "mysqldump: unknown variable 'local-infile=0'"

I've recently upgrade a server to Debian 9 and MySQL to the latest version. I have a simple backup script that I run before performing any work on a production site but this time, when running my script, I encounter the following:
mysqldump: unknown variable 'local-infile=0'
Here is my script. What's going on?
#!/bin/bash
# [skipping commentary]
SITE=prod
# Set the directory that the Drupal root is IN, no trailing slashes
DROOT=[website_root]
# Set the directory for storing backups, no trailing slashes
BUD=/$DROOT/notes/backups
# Don't edit; End of defining variables
echo Doing a full back up...
echo Prepare to enter MySQL password...
# tar -czf $BUD/$SITE-files-$(date +'%Y%m%d%H%M%S').tgz $DROOT/docroot
mysqldump -u mysql_user -p drupal > $BUD/$SITE-drupal-$(date +'%Y%m%d%H%M%S').sql
mysqldump -u mysql_user -p civicrm > $BUD/$SITE-civicrm-$(date +'%Y%m%d%H%M%S').sql
ls -lh $BUD
pwd
echo Finished with backups...
MySQL version 10.1.37-MariaDB-0+deb9u1 Debian 9.6
Edit: When I ssh and run mysqldump with correct permissions I get the same issue. Weirdest thing, cron that runs similar process is backing up my databases as ordered.
The best way to solve this is simply to rename the variable to:
loose-local-infile=1
This will allow mysqldump to merely throw a warning, rather than a fatal error.
The suggestion to comment out the variable is not an option if you want LOAD DATA INFILE functionality out of the box, and MySQL 8+ for security reasons requires you to set this variable for both server (mysqld) and client. It is the [client] variable grouping in your config that chokes mysqldump if you don't add the "loose-" prefix to local-infile.
Seems like the new version you install is compiled without support of local-infile parameter. And because package management system (usually) keep your current configuration file you can try to find this parameter in my.ini file and comment it.
This parameter manage LOAD DATA LOCAL functionality. But seems like this have some potential security issues (more here)

The server time zone value 'CEST' is unrecognized

I am using hibernate (Hibernate Maven 5.2.15.Final, Mysql-connector Maven 8.0.9-rc) whith mysql 5.7 on lampp environment on linux so.
I am in Italy (Central European Summer Time) and once March 25, occurs follow error on connection db:
The server time zone value 'CEST' is unrecognized or represents more
than one time zone. You must configure either the server or JDBC
driver (via the serverTimezone configuration property) to use a more
specifc time zone value if you want to utilize time zone support.
On mysql console, I ran:
SHOW GLOBAL VARIABLES LIKE 'time_zone';
SET GLOBAL time_zone='Europe/Rome';
but that did not persist.
Then I added to my.cnf file (in /etc/mysql):
[mysqld]
default-time-zone = 'Europe/Rome'
and also:
default_time_zone = 'Europe/Rome'
but the db server did not start still...
Why does the error occur?
Could someone help me?
Thank you!
#aiman's answer is not correct since in your case the effective server timezone is not UTC.
You'll find on the net some solutions including additional parameters on the jdbc connection string, but there are cases where you cannot change this string.
Here's how I fixed it:
First import the system timezones in mysql:
$ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
Then set your default mysql server timezone in the [mysqld] section of /etc/mysql/my.cnf (or of /etc/mysql/mysql.conf.d/mysqld.cnf on recent Debian/Ubuntu distros) to your actual server timezone, for instance:
default_time_zone = Europe/Paris
and don't forget to restart mysql
$ sudo service mysql restart
(or the appropriate command depending on your distro).
If the problem is when connecting to the db, I found the solution in the NOTE of this answer.
In your connection url use the following:
String url = "jdbc:mysql://localhost/mydb?serverTimezone=Europe/Rome";
This worked for me to connect with flyway and timezone Europe/Amsterdam.
First see your mysql server timezone:
mysql -e "SELECT ##global.time_zone;" -u <mysqluser> -p.
Most probably it should be SYSTEM.
Find your system timezone: date +”%Z.
See if its CEST.
You need to change your system timezone:
#cd /usr/share/zoneinfo
#ls -l
#rm /etc/localtime
#ln -s /usr/share/zoneinfo/UTC /etc/localtime
Then restart your mysql server: /etc/init.d/mysqld restart.
Enjoy
This cropped up out of the blue on my dev machine - restarting MySQL fixed for me

Database returned an invalid value in QuerySet.dates()

I get this error on my Ubuntu 12.04 machine with mysql 5.5 after I imported some Wordpress content to Mezzanine's blog_blogpost.
ValueError at /admin/blog/blogpost/
Database returned an invalid value in QuerySet.dates(). Are time zone definitions and pytz installed?
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/blog/blogpost/
Django Version: 1.6.1
Exception Type: ValueError
Exception Value:
Database returned an invalid value in QuerySet.dates(). Are time zone definitions and pytz installed?
Exception Location: /home/me/.mezenv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py in results_iter, line 1107
Python Executable: /home/me/.mezenv/bin/python
Python Version: 2.7.3
Python Path:
[u'/home/me',
'/home/me/sai',
'/home/me/.mezenv/local/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg',
'/home/me/.mezenv/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg',
'/home/me/.mezenv/lib/python2.7',
'/home/me/.mezenv/lib/python2.7/plat-linux2',
'/home/me/.mezenv/lib/python2.7/lib-tk',
'/home/me/.mezenv/lib/python2.7/lib-old',
'/home/me/.mezenv/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk',
'/home/me/.mezenv/local/lib/python2.7/site-packages']
Server time: Sat, 25 Jan 2014 13:44:11 +0100
Error during template rendering
In template /home/me/.mezenv/local/lib/python2.7/site-packages/grappelli_safe/templates/admin/change_list.html, error at line 140
I added to my local_settings.py
import pytz
from pytz import *
and also defined timezone in my.cnf [mysqld]
default-time-zone = "+01:00"
but I still get the error unitl I get the line in template:
the tag is:
140 {% block date_hierarchy %}{% date_hierarchy cl %}{% endblock %}
Appreciate your help to resolve it.
Looks like error caused Django's 1.6 timezone functionality changes. The docs now mention this error specifically (bug report, link to docs).
You have to load timezone tables into mysql (http://dev.mysql.com/doc/refman/5.6/en/mysql-tzinfo-to-sql.html).
Try execute on your database server:
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -D mysql -u root -p
And then run "flush tables" or "flush query cache", otherwise the problem may not disappear even though you've loaded the correct timezone data:
mysql -u root -p -e "flush tables;" mysql
updated by #qris
What worked for me:
1. Populate the timezone definitions in the 'mysql' table
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
2. Flush tables
mysql -u root -p -e "flush tables;" mysql
3. Restart mysql
sudo service mysql restart
For MacOS users I found solution here (in comment):
mysql_tzinfo_to_sql /usr/share/zoneinfo | sed -e "s/Local time zone must be set--see zic manual page/local/" | mysql -u root mysql
Because on MacOS we have error looks like this:
$ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u roomysql
Warning: Unable to load '/usr/share/zoneinfo/+VERSION' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/Asia/Riyadh87' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/Asia/Riyadh88' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/Asia/Riyadh89' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/Mideast/Riyadh87' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/Mideast/Riyadh88' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/Mideast/Riyadh89' as time zone. Skipping it.
ERROR 1406 (22001) at line 38981: Data too long for column 'Abbreviation' at row 1
For Windows, take the following steps:
stop the service
download the files provided by MySQL but note they go in C:\ProgramData\MySQL\MySQL Server VERSION\data\mysql not in C:\Program Files\MySQL\MySQL Server VERSION\data at least on Windows 7 as of 2014.
I also modified C:\Program Files\MySQL\MySQL Server VERSION\my.ini to add default-time-zone = 'UTC' per the tips here. Then restart the service.
For anyone else passing through here with the same issue on Mavericks I was getting the error mentioned by Anton whereby I kept getting;
ERROR 1406 (22001) at line 38981: Data too long for column 'Abbreviation' at row 1
So I used mysql_tzinfo_to_sql tz_file tz_name from the MySQL docs to load the specific time zones that I wanted;
mysql_tzinfo_to_sql /usr/share/zoneinfo/GMT GMT | mysql -u root -p mysql
mysql_tzinfo_to_sql /usr/share/zoneinfo/UTC UTC | mysql -u root -p mysql
And now Django isn't returning errors so I'm happy :D
The Django docs actually talk about this problem:
I get an error “Are time zone definitions for your database and pytz installed?” pytz is installed, so I guess the problem is my database?
If you are using MySQL, see the Time zone definitions section of the MySQL notes for instructions on loading time zone definitions.
If you follow that link, you get this advice:
If you plan on using Django’s timezone support, use mysql_tzinfo_to_sql to load time zone tables into the MySQL database. This needs to be done just once for your MySQL server, not per database.
Here's the command:
mysql_tzinfo_to_sql /usr/share/zoneinfo/ | mysql mysql
Loading Time Zone for Mysql solved the problem for me.
If you are on windows, follow these instructions :
Step #1: Download the package that contains the data files of pre-built time zone tables.
http://dev.mysql.com/downloads/timezones.html
Step #2: Unzip the downloaded ZIP file to a folder on your Desktop.
Step #3: Stop MySQL Server.
For Xampp and Wamp use their GUI or stop the mysql service from the Task Manager.
Step #4: Open mysql subdirectory of your MySQL server's data directory.
For me it is C:\xampp\mysql\data\mysql
Step #5: Overwrite time zone data files with the downloaded version.
Copy all 15 data files from your unzipped folder and paste into data folder for the mysql system database. Overwrite all these .frm, .MYD, and .MYI files.
Step #6: Restart MySQL server.
And the job is done :-)
source : http://www.geeksengine.com/article/populate-time-zone-data-for-mysql.html

how to restore mysql db a day before

can anyone please tell me how i can restore the mysql db to one day earlier because some mistake happened in the script and one of all the colums of particular parameter got affected please help ,Server version: 5.0.85-log
MySQL client version: 5.2.10-MariaDB
PHP extension: mysqli
Go back to your previous MariaDB backup, and reload from the binary log until the point where the mistake happened. If the binary log is not active, or you do not have a backup at all, then you are on your own!
First, reload from your latest dump file:
mysql -u root -p < dump_file
Next, look in the dump file to find the binary log co-ordinates, and re-execute the events until the point you need.
Then, look through the binary log (with mysqlbinlog) to see the point where the mistake happened. This is the the point in which to stop.
For example, if the co-ordinates in the dump file are as follows:
-- CHANGE MASTER TO MASTER_LOG_FILE='binlog.002003', MASTER_LOG_POS=96456;
and the point to stop is at 104342 (this is the first invalid command)
Then you will need to run the following command to restore:
mysqlbinlog --start-position=96456 --stop-position=104342 binlog.002003 | mysql -u root -p
You may also need to run over more than one binlog - see https://mariadb.com/kb/en/mysqlbinlog/ for more mysqlbinlog usage and syntax details.

convert_tz returns null

I know this sounds stupid, but when I use
SELECT CONVERT_TZ('2004-01-01 12:00:00','UTC','Asia/Jakarta') AS time
it outputs NULL. I'm using MySQL Workbench in Ubuntu 12.04 64 bit, and it works in my other laptop/os (also using MySQL Workbench).
This will happen if you haven't loaded the time zone table into mysql.
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
mysql is the name of the built-in database that holds MySQL-specific configuration data.
I found this thread after spending some time trying to figure out why after running the command in the accepted answer (which is the same on MySQL's dev site) the command was unable to convert between timezones such as
SELECT CONVERT_TZ('2004-01-01 12:00:00','UTC','MET') AS time
It turns out that on OS X there are two files that cause problems: /usr/share/zoneinfo/Factory and /usr/share/zoneinfo/+VERSION.
The fix... temporarily moving these files to a different location such as /usr/share/zoneinfo/.bak/ allows for the command
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
to fully populate all of the expected timezone information.
This may or may not be a bug in my installed version of MySQL:
$ mysql --version
mysql Ver 14.14 Distrib 5.6.11, for osx10.6 (x86_64) using EditLine wrapper
I am also operating in STRICT_MODE.
In any case, I hope this saves a few headaches for anyone searching for the fix.
Apart from Windows environment, You can set Time Zone by
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
In Windows environment,
1. download Time zone description tables from http://dev.mysql.com/downloads/timezones.html
2. Stop MySQL server
3. Put then inside Mysql installation package (ie. C:\Program Files\MySQL\data\mysql)`
4. Start MySQL server
..Your work is finished..
If still you are getting NULL for CONVERT_TZ
Download these database tables and insert it into mysql database http://www.4shared.com/folder/Toba2qu-/Mysql_timezone.html
Now you problem will be solved.. :)
MAMP PRO
Open Terminal
cd /usr/share/zoneinfo/
sudo mv +VERSION ~/Desktop
cd /applications/MAMP/Library/bin
sudo ./mysql_tzinfo_to_sql /usr/share/zoneinfo | ./mysql -p -u root mysql
sudo mv ~/Desktop/+VERSION /usr/share/zoneinfo/
1) In Windows, there isn't any data folder now in C:\Program Files\MySQL\ as in other answers.
2) In that case, look for C:\ProgramData\MySQL\MySQL Server 5.x\Data\mysql. Generally this folder hidden and you will not see C:\ProgramData\ some times.
3) Change the Settings in View tab to see Hidden files and Folders as explained here https://irch.info/index.php?pg=kb.page&id=133
4) Stop the MySQL service by searching for "services" in Windows Start button.
5) Then unzip the timezone_2017c_posix.zip and then copy the files in it (copy the files directly, don't copy the whole folder itself), and paste in
C:\ProgramData\MySQL\MySQLServer5.x\Data\mysql\
6) For MySQL 5.7, timezone_2017c_posix.zip will just give a .sql file after unzipping and it may not solve the issue. So go ahead and download the zip file for 5.6 even if you are running MySQL 5.7 and copy those files to C:\ProgramData\MySQL\MySQL Server 5.x\Data\mysql\
7) Restart the MySQL server. To check if the CONVERT_TZ () is working, run this sql query.
SELECT CONVERT_TZ('2004-01-01 12:00:00','UTC','Asia/Jakarta');
and check for non-null output.
These are the steps to make it work if you're in windows and using MySQL 5.7.
Right click on My Computer/Computer/This PC or whatever the name in your OS and choose Properties.
Choose "Advanced system settings" from the left panel.
Choose "Environmental Variables", enter the complete path name of your MySQL bin directory (generally it will be in, C:\Program Files\MySQL\MySQL Server 5.7\bin).
Open cmd prompt, enter into mysql using mysql -u root -p password.
Enter use mysql to select the MySQL DB.
Download the file "timezone_YYYYc_posix_sql.zip" (In the place of YYYY, substitute the maximum year available in that page like 2017 or 2018) from https://dev.mysql.com/downloads/timezones.html.
Extract it and open the file in text editor.
Copy the contents and execute in the cmd prompt.
On successful completion, you should be able to use CONVERT_TZ and other timezone functions.
If you are using MySql on Windows you have to load the timezone data into the mysql schema. Here is a good HOWTO: http://www.geeksengine.com/article/populate-time-zone-data-for-mysql.html
If you don't do this, the function CONVERT_TZ won't recognize your input timezone (i.e. your examples: 'UTC','Asia/Jakarta'), and will simply return NULL.
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
if you get the error data too long for column 'abbreviation' at row 1
then see: https://bugs.mysql.com/bug.php?id=68861
the fix would be to run the following
this will add a line to disable the mysql mode and allow mysql to insert truncated data
this was because of a mysql bug where mysql would add a null character at the end (according to the above link)
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
(if the above gives error "data too long for column 'abbreviation' at row 1")
mysql_tzinfo_to_sql /usr/share/zoneinfo > /tmp/zut.sql
echo "SET SESSION SQL_MODE = '';" > /tmp/mysql_tzinfo_to.sql
cat /tmp/zut.sql >> /tmp/mysql_tzinfo_to.sql
mysql --defaults-file=/etc/mysql/my.cnf --user=verifiedscratch -p mysql < /tmp/mysql_tzinfo_to.sql
On Mac OS Catalina when using XAMPP,
Go to /Applications/XAMPP/xamppfiles/bin folder in Terminal then run following.
./mysql_tzinfo_to_sql /usr/share/zoneinfo | sed -e "s/Local time zone must be set--see zic manual page/local/" | ./mysql -u root mysql
This worked for me.