Mysql: How to diagnosis and correct wrong timezone - mysql

In my server, I have the following from the command line:
]$ date
Fri Sep 16 13:47:02 JST 2011
Which is correct.
in mysql, I have the following:
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2011-09-16 04:50:21 |
+---------------------+
1 row in set (0.00 sec)
mysql> SELECT ##global.time_zone, ##session.time_zone;
+--------------------+---------------------+
| ##global.time_zone | ##session.time_zone |
+--------------------+---------------------+
| SYSTEM | SYSTEM |
+--------------------+---------------------+
1 row in set (0.00 sec)
So, it is set to the system time, but then why is not showing the same time?

You just need to restart mysqld after altering timezone of System..
The Global time zone of MySQL takes timezone of System. When you change any such attribute of system, you just need a restart of Mysqld.
That's it.

Your queries are returning the values in UTC but from the command line output your system is in JST. Since the variables are returning SYSTEM I would suggest that perhaps your configuration has identified system as UTC. Check out your my.cnf as suggested here How do I make MySQL's NOW() and CURDATE() functions use UTC?.

Related

converting datetime mysql with timezone from same table

I have a table with the following columns (simplified): openingtime | timezone
This tables holds opening times. The datetime is in the timezone my server is in, timezone is the timezone of the location the opening time is for. I would like to query the datetime column ending up with the local time.
Example data:
2022-07-08 11:00:00 | Europe/London
My server is in Europe/Paris so the expected output would be 2022-07-08 10:00:00
The following only gives my NULL as results:
SELECT CONVERT_TZ(openingtime, 'Europe/Paris', timezone)
FROM openingtimes
To use CONVERT_TZ() you need to install the time-zone tables otherwise MySQL returns NULL.
To load time zone:
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
After time zone loaded:
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.25 |
+-----------+
1 row in set (0.00 sec)
mysql> SELECT CONVERT_TZ('2022-07-08 11:00:00', 'Europe/London','GMT') ;
+----------------------------------------------------------+
| CONVERT_TZ('2022-07-08 11:00:00', 'Europe/London','GMT') |
+----------------------------------------------------------+
| 2022-07-08 10:00:00 |
+----------------------------------------------------------+
1 row in set (0.00 sec)

How to change the system_time_zone in mysql?

I verified that my global timezone is set to "+05:30" but the system_time_zone is showing UTC. I want to set system_time_zone to IST or "+05:30". I searched over the internet but couldn't find the solution.
mysql> SELECT ##global.time_zone;
+--------------------+
| ##global.time_zone |
+--------------------+
| +05:30 |
+--------------------+
1 row in set (0.00 sec)
mysql> SELECT ##system_time_zone;
+--------------------+
| ##system_time_zone |
+--------------------+
| UTC |
+--------------------+
1 row in set (0.00 sec)
I just want to set system_time_zone to IST. Can someone please help me?
The system timezone is not related to the database but to the OS. Check the documentation of your OS or share with us which OS you use.
Anyway, I would recommend always using UTC for the OS, the database and in your app. Then you can format the date/time in the right timezone at display time, based on the user's preference.

Time zone in Linux and in MySQL 8.0

I am trying to set the time zone in MySQL so that it matches the Ubuntu 20.04 time zone. When I check the error.log for MySQL I see a different set of data for the time zone.
Here is what I have from the system, MySQL 8.0.23 and the error.log:
sudo nano /etc/mysql/my.cnf
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
[mysqld]
default-time-zone = "SYSTEM"
Console commands:
date
Sat 01 Jan 2022 10:01:29 PM EST
var/log/mysql/error.log
2022-01-02T02:31:15.751387Z 0 [System] [MY-010931] [Server]
mysql
mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2022-01-01 22:04:43 |
+---------------------+
1 row in set (0.00 sec)
mysql> SELECT ##global.time_zone;
+--------------------+
| ##global.time_zone |
+--------------------+
| SYSTEM |
+--------------------+
1 row in set (0.00 sec)
After further research I found what I was looking for in the post at stackoverflow.com/questions/35123049/… this change did the trick:
[mysqld]
log_timestamps = SYSTEM

How to change mysql timezone running on inuxmint in virtual box?

I want to change timezone in mysql from EDT to GMT ,
SELECT ##system_time_zone;
+--------------------+
| ##system_time_zone |
+--------------------+
| EDT |
+--------------------+
1 row in set (0.00 sec)
on researching google I am hitting its not possible ! I am pretty sure it should be.
For the current session, just:
SET ##session.time_zone='+00:00'; -- UTC
Or:
SET time_zone = '+00:00';
To make it permanent, you can change mysqld conf file (requires a server restart):
default_time_zone='+00:00'
Demo on DB Fiddle:
SELECT ##global.time_zone, ##session.time_zone;
##global.time_zone | ##session.time_zone
:----------------- | :------------------
SYSTEM | SYSTEM
SET ##session.time_zone='+00:00';
SELECT ##global.time_zone, ##session.time_zone;
##global.time_zone | ##session.time_zone
:----------------- | :------------------
SYSTEM | +00:00

What is the impact of system time zone of RHEL6 server in MySQL replication?

We have a master server in EST time zone while slave is configured to follow IST time zone but mysql on slave is configured with default_time_zone=EST5EDT just to make both DB instances follow same timezone , but If I execute
mysql > Select now();
on both servers I am getting same value , in contrast while I insert values in master instance using now() it insert values in EST on master and in IST on slave which is causing data discrepancy in master and slave as far for data columns.
Below is the test I performed
Master : (Having EST as system timezone)
mysql> insert into test_timezone values (1,now());
Query OK, 1 row affected (0.00 sec)
mysql> insert into test_timezone values (2,now());
Query OK, 1 row affected (0.00 sec)
mysql> insert into test_timezone values (3,now());
Query OK, 1 row affected (0.00 sec)
mysql> select * from test_timezone;
+------+---------------------+
| id | date_tz |
+------+---------------------+
| 1 | 2015-02-02 02:53:46 |
| 2 | 2015-02-02 02:53:50 |
| 3 | 2015-02-02 02:53:54 |
+------+---------------------+
Slave : (Having IST as system timezone but mysql default timezone is EST5EDT)
mysql> select * from test_timezone;
+------+---------------------+
| id | date_tz |
+------+---------------------+
| 1 | 2015-02-02 13:23:46 |
| 2 | 2015-02-02 13:23:50 |
| 3 | 2015-02-02 13:23:54 |
+------+---------------------+
3 rows in set (0.00 sec)
So what needs to be done to get same time on master and slave while inserting/updating data ?
MySQL is dependent on system time zone.
default-time-zone='TIMEZONE'
If this variable in not configured in my.cnf then MySQL will use system time zone to save and display data. If you want to have same time values in master and slave then ideal way is to configure
default-time-zone='TIMEZONE'
Above variable in master and slave this will have precedence over system time zone and you will have same time values in master and slaves.
If you don't find your required time zone in MySQL, please follow below link to populate time zones in MySQL database
https://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html