How to change the system_time_zone in mysql? - 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.

Related

MySQL 8.0 "show columns from ..." query returns 0 rows

I am running MySQL 8.0.22 on Mac OS 11.7. Before I upgraded MySQL from 5.6, the following query worked fine.
show columns from Viewing where field="type";
But now, after upgrading to MySQL 8.0.22, I get the expected 1 row returned when I'm logged into MySQL as root:
mysql> show columns from Viewing where field="type";
+-------+------------------------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------------------------------------+------+-----+---------+-------+
| type | enum('edits','other','page','preview','whole') | YES | | NULL | |
+-------+------------------------------------------------+------+-----+---------+-------+
1 row in set (0.01 sec)
But when I log in as the user I've created for the webpage that issues this query, 0 rows are returned. This user is view_pjn#localhost, and its grants are:
mysql> show grants for "view_pjn"#"localhost";
+--------------------------------------------------------------------+
| Grants for view_pjn#localhost |
+--------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `view_pjn`#`localhost` |
| GRANT SELECT, INSERT ON `blog`.`viewing` TO `view_pjn`#`localhost` |
+--------------------------------------------------------------------+
2 rows in set (0.00 sec)
But when I log into MySQL 8.0.22 as view_pjn, I get the following when I issue this query:
mysql> show columns from Viewing where field="type";
Empty set (0.00 sec)
I have searched MySQL 8.0 Manual and it would seem that as view_pjn#localhost has SELECT, INSERT privileges on blog.viewing, it ought to be able to issue a show columns from Blog.Viewing query.
If anyone can tell me why view_pjn is getting 0 rows returned, I'd really appreciate it.

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 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

Mysql: How to diagnosis and correct wrong timezone

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?.