Trying to set useLegacyDatetimeCode to false in MySQL fails - mysql

When I set useLegacyDatetimeCode=false in my JDBC connection properties, I get this error message:
java.sql.SQLException: The server timezone value 'CET' represents more
than one timezone. You must configure either the server or JDBC driver
(via the serverTimezone configuration property) to use a more specifc
timezone value if you want to utilize timezone support. The timezones
that 'CET' maps to are: Europe/Tirane, Europe/Andorra, Europe/Vienna,
Europe/Minsk, Europe/Brussels, Europe/Sofia, Europe/Prague,
Europe/Copenhagen, Europe/Tallinn, Europe/Berlin, Europe/Gibraltar,
Europe/Athens, Europe/Budapest, Europe/Rome, Europe/Riga,
Europe/Vaduz, Europe/Vilnius, Europe/Luxembourg, Europe/Malta,
Europe/Chisinau, Europe/Tiraspol, Europe/Monaco, Europe/Amsterdam,
Europe/Oslo, Europe/Warsaw, Europe/Lisbon, Europe/Kaliningrad,
Europe/Madrid, Europe/Stockholm, Europe/Zurich, Europe/Kiev,
Europe/Uzhgorod, Europe/Zaporozhye, Europe/Simferopol,
Europe/Belgrade, Africa/Algiers, Africa/Tripoli, Africa/Casablanca,
Africa/Tunis, Africa/Ceuta.
I understand what the message is telling me, but I'm not sure what to do about it. I don't have any way of knowing what time zone the MySQL server is running in (this is software that my customers install, and the servers are not managed by me).
I need to set this property to false to fix MySQL time zone bugs.

You need to pass an additional parameter like this:
db=jdbc:mysql://localhost/db?user=me&pass=secret&useLegacyDatetimeCode=false&serverTimezone=Europe/Vienna

Related

Lumen show different time than what is saved is MYSQL DB

my current timezone is Asia/Karachi and when i retrieve table data from mysql it gives me (actual time - 5 hours)
for eg:
mysql column value: '2021-04-21 01:34:57'
and when i retrieve from laravel DB::table('table_name')->get()->toArray();
it gives following :2021-04-20 20:34:57
and changing my timezone doesn't change anything either.
so is there something else i'm missing ?
btw i created following route for checking my current timezone
$app->get('/timezone', function () {
return date_default_timezone_get();
});
and it gives same what is saved in my env i.e(Asia/Karachi) but this doesn't change the result i get from mysql even if i change it to some other timezone like Asia/Kolkata.
I tried researching on this but didn't get any suitable answer.
i tried alot of things to solve this issue like
adding env variables APP_TIMEZONE="Asia/Karachi" (did not work)
also i tried adding DB_TIMEZONE="+05:00" (this worked in my local but not on stagging)
finally i saw somewhere that someone else had exact same issue resolving this and he did it by adding the hours using carbon which also worked for me
'posted_at' => Carbon::parse($record->posted_at)->addHours(5)->toDateTimeString(),
i know this is not the optimal solution but this was the only solution that worked so i had to go with it.
https://dev.mysql.com/doc/refman/8.0/en/datetime.html
If you store a TIMESTAMP value, and then change the time zone and retrieve the value, the retrieved value is different from the value you stored. This occurs because the same time zone was not used for conversion in both directions. The current time zone is available as the value of the time_zone system variable.
As far as I know, you have a couple things to consider:
The timezone of your Laravel/Lumen app
This can be found found in config/app.php -> timezone.
The timezone of your MySQL server
You can retrieve this using:
mysql> SELECT ##global.time_zone, ##session.time_zone;
The timezone of the server itself
If the mySQL query above returns SYSTEM, it means it uses the system timezone setting, which for Debian/Ubuntu etc you can check using:
cat /etc/timezone
From my experience, you can most often leave the system/mySQL timezones intact and only set the correct timezone in your Laravel config. I know it caused me a headache the first time I had to figure out how this actually worked.

How to get max_allocated_packets from JDBC connection from a Slick DB connection to MySQL

Is there a way via Scala with Slick database query and access library (or using other tricks - dare I say mocks?) to get max_allocated_packets from JDBC-read connection properties from a Slick-style DB connection to MySQL?
As I suspect, the code makes several touch type actions at deeper levels and this connection property is then populated.
Ex: Once a connection is made in com.mysql.cj.jdbc.ConnectionImpl ... using Scala with the Slick library... the value for the JDBC connect property of max_allocated_packets is within the object (debugged in IntelliJ). How can I extract this value or obtain it in higher level code as asked above?
Of course I can query the DB directly to get that value, but I am hoping I can extract this property after the setup phase.
If the value is publically available on a connection, you could try to use SimpleDBIO action to access the JDBC-level values.
See: https://scala-slick.org/doc/3.2.0/dbio.html#jdbc-interoperability
It would be of the form:
val getMaxPacketAction = SimpleDBIO[Int] { database =>
// make use of database.connection here
}
However, this is still going to the database for a connection, so it may be just easier to query for the value you want.

MySQL/Hibernate: getting value +1H on insert type Time

I'm working on a java web project that uses:
Hibernate 5.2.2/JPA 2.0+ MySQL5InnoDBDialect
MySQL 5.6.15-innoDB (on EasyPHP/PHPMyAdmin) + JDBC connector 6.0.4
Joda time API 2.9.4 + Fasterxml jackson API 2.8.3
I'm facing a problem on inserting Time data on database. everytime i put a row, i get a +1H value on time column!
Attribute on Java:
#JsonFormat(shape=JsonFormat.Shape.STRING, pattern="HH:mm")
#Column(name = "RES_DUREE", nullable = false)
#Temporal(TemporalType.TIME) private Date resDuree;
Attribute on SQL:
RES_DUREE TIME NOT NULL;
EDIT (After Adrian Shum's Comment):
Connection line:
jdbc.url =
jdbc:mysql://localhost:3306/mydb?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
I do use UTC but it still 1H+ .
Any suggestion will help, thanks.
Normally it is caused by server and DB time zone mismatch.
In brief, java.util.Date does not contain Timezone information. Conceptually you can treat it as simply representing Date + Time (similar to what JODA/JSR310 LocalDateTime is doing). Therefore if your app server is UTC+10, and your DB is UTC+2, when you save a date of "2016-10-13 10:11:12", although your app server is treating it as "2016-10-13 10:11:12 +10:00", it is simply passing "2016-10-13 10:11:12" to DB. Given DB is UTC+2, it is thinking the time actually means "2016-10-13 10:11:12 +02:00". Situation become more messy if your JDBC connection is claimed to be "UTC+10", most DB is going to "smartly" translate "2016-10-13 10:11:12 +02:00" to "2016-10-13 18:11:12 +10:00" which caused weird time stored and retrieved.
You may diagnose by tracing the SQL (and the actual value used) for corresponding inserts and select. You should see discrepancies between the values, vs the value stored in table. Such tracing can be done by misc way, e.g.
Older version of Hibernate can show the parameter used in prepared statement by turning on proper logger
You may use tools like JdbcDsLog (Disclaimer: I am maintainer for a fork of JbdcDsLog at http://github.com/adrianshum/jdbcdslog)
There is probably tools in DBMS side to trace incoming queries.
Best way to solve is to make sure everything is in the same timezone, and the most rational choice for timezone is UTC.

Am getting timezone error in cakephp

am getting continue this error in cakephp even i ave set timezone in php.ini but it gives 500 internal server error
Warning: strtotime(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home1/allinal6/public_html/telecom/lib/Cake/Cache/CacheEngine.php on line 61
Thanks in advance
That warning is unrelated to your 500 internal server error. However, in order to remove that warning, as seen on How do I set default timezone in cakephp? go into /app/config/core.php and find the line that calls date_default_timezone_set(); (normally commented out when you first download CakePHP.) Uncomment it and add your desired timezone.

Mysql v5.1.50 retuns string instead of decimal on DIFFERENT computers

I have two PC's both of them with Win2008 R2 set it up exactly the same.
On one PC the sum(FIELD_NAME) returns a decimal number (CORRECTY) on the other
the same sum(FIELD_NAME) returns the same number as STRING !!!
I have set the System_Time_Zone to be the same on both PC (GTB Standard Time) but the problem still exist. I had uninstall and install back again MySQL on the second PC but still no luck.
I had try tzutil (O/S command) and set the time zone to the above desired but still have the same problem.
With show variables like "%time_zone%" the correct pc shows
system_time_zone : GTB Standard Time
time_zone : System
the "wrong" pc shows
system_time_zone : AN EMPTY SPACE
time_zone : System
my final solution is to reinstal Windows again but.... :o((((
any suggestions ?????
I suggest you confirm the version of the MySQL Connector you are using (not clear whether you are using the Connector/ODBC, Connector/Net or Connector/J.
What MySQL returns is interpreted/translated by the Connector. I think the issue is likely a differnce in the connector being used, or the verison of the connector, or possibly just a difference in the configuration of the Connector (for example, the DFLT_BIGINT_BIND_STR parameter in Connector/ODBC.)
(Less likely, the behavior you observe could be due to a difference in the characterset encodings.)
The details for configuration, debugging and tracing are different for each Connector.
http://dev.mysql.com/doc/index-connectors.html