set timezone mysql - mysql

I want to set timezone in mysql from default timezone ('los angles') to my own timezone ('asia/jakart'). I have been change the timezone using query ->
SET GLOBAL TIME_ZONE = 'ASIA/JAKARTA'
The timezone was change if i using the query above. but if i restart mysql, the timezone is back to default timezone(los angels).
How to make the timezone changed permanent to asia/jakarta?

Put the following in your mysql server configuration (e.g. my.cnf)
default-time-zone=Asia/Jakarta
Please check the doc here

In /etc/mysql/my.cnf
[mysqld_safe]
socket=/var/run/mysqld/mysqld.sock
default_time_zone=Asia/Jakarta
Remember to use underscores _

Related

How to set mysql default Time zone when server restart?

I am using AWS linux instance.
How can i set default time zone '+05:30'?
I tired editing my.cnf file and added
default-time-zone='+05:30'
but it doesn't effect.
I also tried to set through command like
set global time-zone = '+05:30'
and it was working but when i restart the server it set to default time zone "SYSTEM" (00:00).
Can anyone help?

How to set up group_concat_max_length permanently in Mysql?

How to set up group concat max length permanently in Mysql?
While setting group concat max length:
SET group_concat_max_len=102400
At the time it is set, but after restarting the server, it shows its default value 1024 again.
Edit my.cnf file and add group_concat_max_len=102400 under [mysqld] section. Then restart mysql service and that's all.
You can pass it to MySQL deamon via param:
--group_concat_max_len=xxx

The date and time takes the timezone of the server where it is hosted rather then where the server is currently used in particular location

Suppose I am located in India and my Mysql database server is stored in the Oregon server.So when i am trying to track datetime (i.e. now()) whenever i perform Insert or update operation of the data in the table .It always take the time zone of the Oregon server
Can i use the system timezone where my Mysql server is being hosted?
Presently I am using a function whenever i use to display datetime on front-end
CREATE DEFINER=`root`#`localhost` FUNCTION `FnTimeZone`(P_date datetime) RETURNS varchar(100) CHARSET latin1
BEGIN
declare returnDate varchar(100);
Select convert_tz(P_date,'+00:00','+00:00') into returnDate;
RETURN returnDate;
END
Can you please help me find better solution?
Thanks
There are a number of ways you can configure the timezone mysql uses. Mysql documentation on MySQL Server Time Zone Support lists all these options for you:
The system time zone. When the server starts, it attempts to determine the time zone of the host machine and uses it to set the
system_time_zone system variable. The value does not change
thereafter.
You can set the system time zone for MySQL Server at startup with the --timezone=timezone_name option to mysqld_safe. You can also set
it by setting the TZ environment variable before you start mysqld. The
permissible values for --timezone or TZ are system dependent. Consult
your operating system documentation to see what values are acceptable.
The server's current time zone. The global time_zone system variable indicates the time zone the server currently is operating in.
The initial value for time_zone is 'SYSTEM', which indicates that the
server time zone is the same as the system time zone.
The initial global server time zone value can be specified explicitly at startup with the --default-time-zone=timezone option on
the command line, or you can use the following line in an option file:
default-time-zone='timezone'
If you have the SUPER privilege, you can set the global server time zone value at runtime with this statement:
mysql> SET GLOBAL time_zone = timezone;
Per-connection time zones. Each client that connects has its own time zone setting, given by the session time_zone variable. Initially,
the session variable takes its value from the global time_zone
variable, but the client can change its own time zone with this
statement:
mysql> SET time_zone = timezone;

MySQL Time Zones

Is there an exhaustive list of MySQL Time Zones?
It seems that the valid values for time_zone in MySQL settings are dependent on the host Operating System but I have been unable to find a list of possible values.
I need the time to show Calgary local time.
By default, (at least on Debian-based installations) no time zone data is loaded into MySQL. If you want to test if they are loaded, try executing:
SELECT CONVERT_TZ('2012-06-07 12:00:00', 'GMT', 'America/New_York');
If it returns a DATETIME (in this case 2012-06-07 08:00:00), you have time zones loaded. If it returns NULL, they aren't. When not loaded, you are limited to converting using offsets (e.g. +10:00 or -6:00).
This should work fine in many cases, but there are times when it is better to use named time zones, like for not worrying about daylight savings time. Executing the following command loads the time zone data from the system (Unix-only. I'm not sure what the equivalent Windows command would be):
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
If you need to continually rely on MySQL time zones, the above command should be executed every time the system time zone is updated. You could also just add it to a weekly or monthly cron job to do it for you automatically.
Then, to view a list of time zones, just do the following:
USE mysql;
SELECT * FROM `time_zone_name`;
Note, the time zone info takes up about 5 MB in MySQL. If you ever want to un-load the timezone info, just execute the following and restart MySQL:
TRUNCATE `time_zone` ;
TRUNCATE `time_zone_leap_second` ;
TRUNCATE `time_zone_name` ;
TRUNCATE `time_zone_transition` ;
TRUNCATE `time_zone_transition_type` ;
Do not DROP these tables or bad things will happen.
Edit:
Based on a user comment below, if you want to have the timezones automatically updated when you update the system, you first need to allow root to log in without being prompted for a password.
MySQL >= 5.6.6
Execute the following [source]:
mysql_config_editor set --login-path=client --host=localhost --user=root --password
MySQL < 5.6.6
Create a ~/.my.cnf file (if it doesn't exist yet) and add the following:
[client]
user=root
password=yourMysqlRootPW
Then execute chmod 600 ~/.my.cnf to make sure nobody else can read it.
Update script
Add the following script to crontab to be executed once per day:
#!/bin/bash
# Find if there are any timezone files that have been modified in the last 24
# hours and do not have ".tab" in the name (since these are not timezone files)
if [ `find /usr/share/zoneinfo -mtime -1 | grep -v '\.tab' | wc -l` -gt 0 ]; then
echo "Updating MySQL timezone info"
# Note, suppressing STDERR here because of the .tab files above
# that cause warnings.
mysql_tzinfo_to_sql /usr/share/zoneinfo 2>/dev/null | mysql -u root mysql
echo "Done!\n"
fi
Remove the echo lines if you don't want any output.
Note: This is (mostly) untested. Let me know if you have any issues and I'll update this answer.
From the MySQL 5.7 documentation (emphasis mine):
timezone values can be given in several formats, none of which are
case sensitive:
The value 'SYSTEM' indicates that the time zone should be the same as
the system time zone.
The value can be given as a string indicating an offset from UTC, such
as '+10:00' or '-6:00'.
The value can be given as a named time zone, such as
'Europe/Helsinki', 'US/Eastern', or 'MET'. Named time zones can be
used only if the time zone information tables in the mysql database
have been created and populated.
It should be noted that the MySQL timezone variable's default setting is SYSTEM at MySQL startup. The SYSTEM value is obtained from an operating system setting (e.g. from the file that is referenced by the symlink /etc/localtime)
MySQL's default timezone variable can be initialised to a different value at start-up by providing the following command line option:
--default-time-zone=timezone
Alternatively, if you are supplying the value in an options file, you should use the following syntax to set the variable:
--default-time-zone='timezone'
If you are a MySQL SUPER user, you can set the SYSTEM time_zone variable at runtime from the MYSQL> prompt using the following syntax:
SET GLOBAL time_zone=timezone;
MySQL also supports individual SESSION timezone values which defaults to the GLOBAL time_zone environment variable value. To change the session timezone value during a SESSION, use the following syntax:
SET time_zone=timezone;
In order to interrogate the existing MYSQL timezone setting values, you can execute the following SQL to obtain these values:
SELECT ##global.time_zone, ##session.time_zone;
For what it's worth, I simply googled mysql time_zone configuration valid values and looked at the first result.
You can run this query to get a complete list of the timezones MySQL supports:
SELECT Name
FROM mysql.time_zone_name
ORDER BY Name
(Found on this page: https://www.plumislandmedia.net/mysql/time-zones-mysql/)
An exhaustive list of timezones can be downloaded from MySQL's website as database tables that get imported into MySQL server.
For Calgary time you can specify UTC offsets as
set time_zone = '-6:00';
It should be noted that the MySQL timezone variable's default setting is SYSTEM at MySQL startup. The SYSTEM value is obtained from the operating system's GLOBAL time_zone environment variable.
MySQL's default timezone variable can be initialised to a different value at start-up by providing the following command line option:
--default-time-zone=timezone
Alternatively, if you are supplying the value in an options file, you should use the following syntax to set the variable:
--default-time-zone='timezone'
If you are a MySQL SUPER user, you can set the SYSTEM time_zone variable at runtime from the MYSQL> prompt using the following syntax:
SET GLOBAL time_zone=timezone;
MySQL also supports individual SESSION timezone values which defaults to the GLOBAL time_zone environment variable value. To change the session timezone value during a SESSION, use the following syntax:
SET time_zone=timezone;
In order to interrogate the existing MYSQL timezone setting values, you can execute the following SQL to obtain these values:
SELECT ##global.time_zone, ##session.time_zone;

MySQL timezone change?

How do I change my timezone which is currently in UTC to GMT +1, what is the correct line and do i just enter it in phpMyAdmin SQL execution?
My host just gave me this link http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html and went off so I'm kinda lost thanks
The easiest way to do this, as noted by Umar is, for example
mysql> SET GLOBAL time_zone = 'America/New_York';
Using the named timezone is important for timezone that has a daylights saving adjustment. However, for some linux builds you may get the following response:
#1298 - Unknown or incorrect time zone
If you're seeing this, you may need to run a tzinfo_to_sql translation... it's easy to do, but not obvious. From the linux command line type in:
mysql_tzinfo_to_sql /usr/share/zoneinfo/|mysql -u root mysql -p
Provide your root password (MySQL root, not Linux root) and it will load any definitions in your zoneinfo into mysql. You can then go back and run your
mysql> SET GLOBAL time_zone = timezone;
issue the command:
SET time_zone = 'America/New_York';
(Or whatever time zone GMT+1 is.: http://www.php.net/manual/en/timezones.php)
This is the command to set the MySQL timezone for an individual client, assuming that your clients are spread accross multiple time zones.
This command should be executed before every SQL command involving dates. If your queries go thru a class, then this is easy to implement.
While Bryon's answer is helpful, I'd just add that his link is for PHP timezone names, which are not the same as MySQL timezone names.
If you want to set your timezone for an individual session to GMT+1 (UTC+1 to be precise) just use the string '+01:00' in that command. I.e.:
SET time_zone = '+01:00';
To see what timezone your MySQL session is using, just execute this:
SELECT ##global.time_zone, ##session.time_zone;
This is a great reference with more details: MySQL 5.5 Reference on Time Zones
Here is how to synchronize PHP (>=5.3) and MySQL timezones per session and user settings. Put this where it runs when you need set and synchronized timezones.
date_default_timezone_set($my_timezone);
$n = new \DateTime();
$h = $n->getOffset()/3600;
$i = 60*($h-floor($h));
$offset = sprintf('%+d:%02d', $h, $i);
$this->db->query("SET time_zone='$offset'");
Where $my_timezone is one in the list of PHP timezones: http://www.php.net/manual/en/timezones.php
The PHP timezone has to be converted into the hour and minute offset for MySQL. That's what lines 1-4 do.
If you have the SUPER privilege, you can set the global server time zone value at runtime with this statement:
mysql> SET GLOBAL time_zone = timezone;
If SET time_zone or SET GLOBAL time_zone does not work, you can change as below:
Change timezone system, example: ubuntu... $ sudo dpkg-reconfigure
tzdata
Restart the server or you can restart apache2 and mysql
(/etc/init.d/mysql restart)
This works fine
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
$con->query("SET GLOBAL time_zone = 'Asia/Calcutta'");
$con->query("SET time_zone = '+05:30'");
$con->query("SET ##session.time_zone = '+05:30'");
?>