Mysql: SET time_zone = "+00:00" and pool

Created on 13 Oct 2014  路  5Comments  路  Source: mysqljs/mysql

Hi.

I need UTC in my TIME queries, but I don't have permission on target machine to do global modification on time settings in mysql.ini

Trouble can me solved with SET time_zone = "+00:00", but only if I use mysql.createConnection() and exec SET query at beginning of all rest operations.

In my application I prefer use mysql.createPool and I don't known where I need to exec SET time_zone...

As I understand pool can recreate connection if it is was broken or timed out. And we need apply SET time_zone on every reconnect (connection)...

Please help me.

question

Most helpful comment

Hi @vinnitu , this is what you want to do for the pool:

var mysql = require('mysql');

var pool = mysql.createPool({
  host: 'localhost',
  timezone: '+00:00'
});

pool.on('connection', function onConnection(connection) {
  connection.query('SET time_zone = ?', '+00:00');
});

// Now just use the pool normally and every single connection
// you ever get from the pool will have SET time_zone ran on it

All 5 comments

Doesn't this work for you?

var db = mysql.createPool("mysql://username:password@hostname/database?tz=UTC");

no :(

Hi @vinnitu , this is what you want to do for the pool:

var mysql = require('mysql');

var pool = mysql.createPool({
  host: 'localhost',
  timezone: '+00:00'
});

pool.on('connection', function onConnection(connection) {
  connection.query('SET time_zone = ?', '+00:00');
});

// Now just use the pool normally and every single connection
// you ever get from the pool will have SET time_zone ran on it

It works!

pool.on('connection', function onConnection(connection) {
  connection.query('SET time_zone = ?', '+00:00');
});

Thank You!

yay, you're welcome! :tada:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Axxxx0n picture Axxxx0n  路  3Comments

Rhapsody-Sky picture Rhapsody-Sky  路  3Comments

abou7mied picture abou7mied  路  4Comments

nanom1t picture nanom1t  路  3Comments

DmitryEfimenko picture DmitryEfimenko  路  4Comments