How can i get current timezone in mysql .
May be i'm don't undestand question but...
http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
It's the first (and only) column of the first (and only) row of the result of this SELECT query:
SELECT ##global.time_zone;
If you want the session time zone you can use:
SELECT ##session.time_zone;
The result is like this:
mysql> SELECT ##global.time_zone;
+--------------------+
| ##global.time_zone |
+--------------------+
| Europe/Paris |
+--------------------+
1 row in set (0.00 sec)
Related
My connection string for MySQL is:
"Server=localhost;User ID=root;Password=123;pooling=yes;charset=utf8;DataBase=.;"
My questions are :
What query should I write to get database names that exist?
What query should I write to get server version?
I have error because of my connection string ends with DataBase=.
What should I write instead of the dot?
SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA
SELECT VARIABLE_NAME, VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_VARIABLES WHERE VARIABLE_NAME = 'VERSION'
Use INFORMATION_SCHEMA as the database.
To get the list of databases, you can use SHOW DATABASES:
SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.01 sec)
To get the version number of your MySQL Server, you can use SELECT VERSION():
SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.1.45 |
+-----------+
1 row in set (0.01 sec)
As for the question about the connection string, you'd want to put a database name instead of the dot, such as Database=test.
show Databases;
Will return you all the registered databases.
And
show variables;
will return a bunch of name value pairs, one of which is the version number.
I have a fairly large and complex mysql query to blackbox test and this query is time sensitive, it has a lot of conditions based on current_timestamp.
My goal is to make some tests so it always passes or fails. I'm thinking of mocking the value of current_timestamp temporarily to a fixed date before running the query and set it back to original value after the query.
Is it something thats doable?
I cannot modify the query itself (i.e.: find replace current_timestamp to something else)
Thanks
By setting system variable 'timestamp'. To restore it to current timestamp again, set it to DEFAULT.
mysql> SET TIMESTAMP = UNIX_TIMESTAMP('2015-01-01');
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2015-01-01 00:00:00 |
+---------------------+
1 row in set (0.00 sec)
mysql> SET TIMESTAMP = DEFAULT;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2016-03-08 09:50:16 |
+---------------------+
1 row in set (0.00 sec)
mysql> SELECT title FROM pages WHERE id=111;
+------------+
| title |
+------------+
| 'Theology' |
+------------+
1 row in set (0.00 sec)
mysql> SELECT id FROM pages WHERE title='Theology';
Empty set (0.00 sec)
The results conflicted. I can't understand that.
Change
'Theology'
to
'\'Theology\''
Seems that the data stored is 'Theology' instead of Theology. Thanks to Abhik Chakraborty.
Use query like this . You need to escape the '
1st Way
SELECT id FROM pages WHERE title = '\'Theology\''
2nd Way
SELECT id FROM pages WHERE title = "'Theology'"
3rd Way
SELECT id FROM pages WHERE title='''Theology''';
I am trying to move our encryption from Code to the database to speed things up. When I attempt to decrypt the information using this select statement I get an incorrect parameter count error.
SELECT AES_DECRYPT(u.strFirstName,'usa2010') FROM EncryptingTest.tblUser u;
I've looked at the documentation and this should work. Can someone tell me what I am doing wrong??
EDIT
I have tried Restarting the MySQL Server to no avail. The server Version is 5.6.22
From the example you provided it seems ok. Can you see about casting it first?
SELECT CAST(AES_DECRYPT(u.strFirstName,'usa2010') AS CHAR(50)) FROM EncryptingTest.tblUser u;
Please check this link it has some great resources on the issue your facing...
http://mysqlblog.fivefarmers.com/2014/03/27/mysql-5-6-17-now-with-better-encryption/
EDIT - The actual fix
Using AES_ENCRYPT() or AES_DECRYPT() with block_encryption_mode set to a block cipher other than ECB will produce an error if the IV is not provided:
mysql> SET ##session.block_encryption_mode = 'aes-256-cbc';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT HEX(AES_ENCRYPT('test', 'key'));
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'aes_encrypt'
mysql> SELECT HEX(AES_ENCRYPT('test', 'key', RANDOM_BYTES(16)));
+---------------------------------------------------+
| HEX(AES_ENCRYPT('test', 'key', RANDOM_BYTES(16))) |
+---------------------------------------------------+
| 2EFBA8708925C1DF8B661E57938FAE5E |
+---------------------------------------------------+
1 row in set (0.00 sec)
Note that the IV isn’t itself stored in the resulting encrypted output – it’s an artifact that you’ll have to track separately to get the decrypted values back:
mysql> SET #iv = RANDOM_BYTES(16);
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT HEX(AES_ENCRYPT('test', 'key', #iv));
+--------------------------------------+
| HEX(AES_ENCRYPT('test', 'key', #iv)) |
+--------------------------------------+
| 650CE9E699ECA922E09E80CEBE51BFC7 |
+--------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT AES_DECRYPT(UNHEX('650CE9E699ECA922E09E80CEBE51BFC7'), 'key', #iv);
+--------------------------------------------------------------------+
AES_DECRYPT(UNHEX('650CE9E699ECA922E09E80CEBE51BFC7'), 'key', #iv) |
+--------------------------------------------------------------------+
| test |
+--------------------------------------------------------------------+
1 row in set (0.00 sec)
What is the behavior of the TIMESTAMP field type in relation to timezones?
Is any timestamp value inserted to that field inserted as is?
Or does it assume that the timezone of a timestamp value that is inserted is in server local time and converts it UTC?
EDIT:
Here is my test
I ran both PHP date() and MySQL's SELECT NOW() and they are outputting roughly equal timestamps. The results of both is not in UTC time.
I tried inserting to a test table with the value for the TIMESTAMP field by gotten from PHP date()
The value from PHP date() SHOULD have been converted to UTC. However, what I see in the database is not UTC. The value for the TIMESTAMP field is inserted as is.
TIMESTAMP value is always saved as UTC.
MySQL converts TIMESTAMP from current timezone to UTC for storage and back from UTC to the current time zone for retrieval.
The default timezone will be the server timezone and can be set on a connection. See this.
For more details see MySQL Doc
I can explain this through an example. Please execute the queries in mysql console:
mysql> CREATE TABLE `testtable` (
`date_timestamp` TIMESTAMP NOT NULL,
`date_datetime` DATETIME NOT NULL
)
ENGINE = InnoDB;
Query OK, 0 rows affected (0.06 sec)
mysql> SET time_zone = '+00:00';
Query OK, 0 rows affected (0.00 sec)
mysql> insert into testtable values(now(),now());
Query OK, 1 row affected (0.03 sec)
mysql> select * from testtable;
+---------------------+---------------------+
| date_timestamp | date_datetime |
+---------------------+---------------------+
| 2012-10-19 05:01:38 | 2012-10-19 05:01:38 |
+---------------------+---------------------+
1 row in set (0.00 sec)
mysql> SET time_zone = '+05:30';
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> insert into testtable values(now(),now());
Query OK, 1 row affected (0.03 sec)
mysql> select * from testtable;
+---------------------+---------------------+
| date_timestamp | date_datetime |
+---------------------+---------------------+
| 2012-10-19 10:31:38 | 2012-10-19 05:01:38 |
| 2012-10-19 10:31:47 | 2012-10-19 10:31:47 |
+---------------------+---------------------+
2 rows in set (0.00 sec)
The TIMESTAMPT value is inserted AS IS.