I use SQLAlchemy with pymysql driver to connect to MySQL instance. This MySQL instance have its timezone configured as UTC.
Now, I want to execute a long SQL query (it is a hard coded SQL script...) which make heavy use of date functions against timestamp columns. I want this SQL to be executed under my country's local timezone (namely, it is +09:00).
Question
When you are making engine with pymysql driver, is it possible to set time_zone other than the server timezone?
Or at least, on certain connection?
See how to pass Custom DBAPI connect() arguments for example of how to get past the SQLAlchemy part of what you are trying to do.
There is an open feature request in pymysql repo for what you want: Allow specifiying a timezone for connections.
The pymysql.connections.Connection object accepts a parameter called init_command for which the docs state:
Initial SQL statement to run when connection is established.
A contributor provides the following example in the discussion of that issue:
init_command="SET SESSION time_zone='+00:00'"
So your create_engine might look something like this:
engine = create_engine(..., connect_args={"init_command": "SET SESSION time_zone='+09:00'"})
Related
I brought new Managed MYSQL Database from DigitalOCean. Now I am unable to change Global timeZone. When I am trying to change it error Occurred and it says there isn't Privileges.
*I'm working with Spring Boot Project.
Is there any solution to resolve that?
Still, there is no way to change it from SQL or digitalocean's dashboard.
there is a way when you create the connection. the main user also hasn't root privileges to change the global variable. so we have one only option. only we can play with the session.
When you create the connection you cant set SESSION timezone.
If you are using Spring boot and Hikari (pool), I'll put the configuration.
Todo this we can use connection-init-sql
spring.datasource.hikari.connection-init-sql=SET SESSION time_zone='Asia/Colombo'
now your session timezone will be as you want.
or you can pass the timezone [serverTimezone] with the connection URL like below,
jdbc:mysql://localhost:3006?serverTimezone=Asia/Colombo
I want to automate the installation and configuration of a mysql server using azure cli.
The installation works well using azure mysql server create, however the configuration using azure mysql server configuration set -n time_zone --value Europe/Paris fails due to the following error:
Deployment failed. Correlation ID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. The value 'Europe/Paris' for configuration 'time_zone' is not valid. The allowed values are '[+|-][0]{0,1}[0-9]:[0-5][0-9]|[+|-][1][0-2]:[0-5][0-9]|SYSTEM'.
As I read in the mysql docs I could enable named time zones executing the following sql SET GLOBAL time_zone = timezone;, but unfortunately my user would need super privilege for this to succeed and this is impossible in azure.
The other approach would be to run mysql_tzinfo_to_sql but this is not available using azure cli.
Is there any other way to activate named time zones?
From the Azure DB for MySQL documentation:
Populating the time zone tables
The time zone tables on your server can be populated by calling the mysql.az_load_timezone stored procedure from a tool like the MySQL command line or MySQL Workbench.
CALL mysql.az_load_timezone();
Also, in this doc (you linked to in your question):
Upon initial deployment, an Azure for MySQL server includes systems tables for time zone information, but these tables are not populated. The time zone tables can be populated by calling the mysql.az_load_timezone stored procedure from a tool like the MySQL command line or MySQL Workbench.
According to Error message the format should be one of these three:
[+|-][0]{0,1}[0-9]:[0-5][0-9]
eg. -04:30
or
[+|-][1][0-2]:[0-5][0-9]
e.g -12:00
or
SYSTEM
So have you tried doing it with quotes?
azure mysql server configuration set -n time_zone --value
"Europe/Paris"
CALL mysql.az_load_timezone();
Call this stored procedure from your session on the server,
If you are running the mysql.az_load_timezone command from MySQL Workbench, you may need to turn off safe update mode first using SET SQL_SAFE_UPDATES=0 or in Preferences->SQL Editor->Safe Updates(), and back to connect to your mysql server.
I have a table contains a regular datetime typed column, and when selecting mysql command line returns the correct result:
but when it comes to JDBC, it'll return 2020-02-03 08:00:00. I have to use TimeZone.setDefault(TimeZone.getTimeZone("GMT-6:00") to regulate it.
On my server, the time zone related options are:
But seems the result JDBC driver returns is neither in my current time zone, nor the UTC. Is this because of something wrong on my MySQL server...?
Currently I'm using mysql:latest on Docker Windows and mysql:mysql-connector-java:8.0.19, the TZ environment of Docker container has been set to my local time zone.
Many thanks.
I am working on upgrading existing libraries in our code and mysql-connector-java is one of them. We currently use the version: 5.1.25, I am trying to upgrade it to the latest version which is 8.0.18. I got some errors regarding to package structure changes but I've corrected them.
When I run the application (directly goes go database for authorization) I get following error:
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.
After some research I've found this question and realized I have to specify server time zone. Our database both on development and production environment configured as CEST time zone but when I add "serverTimeZone = CEST" to connection string I get following error:
No timezone mapping entry for 'CEST'
If I specify like "serverTimeZone = UTC", it works but confuses me also.
Why should I say UTC even though it is CEST? Time zone in my local system is UTC, is it the reason that I have to force it to UTC? If so, we have users all around the world, so bunch of request from different time zones will come to the DB server, if I say serverTimeZone = UTC, isn't it going to be a problem for them?
I'm trying to use the MySqlHook.bulk_load() method docs in my Airflow task, and if my understanding is correct, I have to enable "local_infile" and "loose-local-infile" when I create the connection to my MySQL database in order for this to work.
Otherwise, it results in the following error:
"The used command is not allowed with this MySQL version."
If I create my connection from the Connections page on the webserver UI, and pass the following parameters as "extras", the method is successful and the DAG works as intended:
{"local_infile":"1","loose-local-infile":"1"}
image of connection settings on Airflow webserver UI
However, I am looking create my database connection by passing it as an environmental variable instead. To do so, requires me to pass my connection settings as a URI. see "Connections" in docs
ie: scheme://[user[:[password]]#]target[:port][/schema][?attribute1=value1&attribute2=value2...
docs
I have tried the following formats, but none have worked successfully for me:
mysql://user:password#hostname:port/schema?local_infile=1&loose-local-infile=1
mysql://user:password#hostname:port/schema?local_infile=1;loose-local-infile=1
I have also tried percent encoding my values like so:
mysql://user:password#hostname:port/schema?local_infile%3D1%26loose-local-infile%3D1
Any help would be GREATLY appreciated, thank you!