How to enable explicit_defaults_for_timestamp? - mysql

When I try to start my mySQL server I get message:
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated.
Please use --explicit_defaults_for_timestamp server option (see
documentation for more details).
I find answer on:
http://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_explicit_defaults_for_timestamp
But how to enable it? Where?

First you don't need to change anything yet.
Those nonstandard behaviors remain the default for TIMESTAMP but as of
MySQL 5.6.6 are deprecated and this warning appears at startup
Now if you want to move to new behaviors you have to add this line in your my.cnf in the [mysqld] section.
explicit_defaults_for_timestamp = 1
The location of my.cnf (or other config files) vary from one system to another. If you can't find it refer to https://dev.mysql.com/doc/refman/5.7/en/option-files.html

In your mysql command line do the following:
mysql> SHOW GLOBAL VARIABLES LIKE '%timestamp%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| explicit_defaults_for_timestamp | OFF |
| log_timestamps | UTC |
+---------------------------------+-------+
2 rows in set (0.01 sec)
mysql> SET GLOBAL explicit_defaults_for_timestamp = 1;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW GLOBAL VARIABLES LIKE '%timestamp%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| explicit_defaults_for_timestamp | ON |
| log_timestamps | UTC |
+---------------------------------+-------+
2 rows in set (0.00 sec)

On a Windows platform,
Find your my.ini configuration file.
In my.ini go to the [mysqld] section.
Add explicit_defaults_for_timestamp=true without quotes and save the change.
Start mysqld
This worked for me (windows 7 Ultimate 32bit)

For me it worked to add the phrase "explicit_defaults_for_timestamp = ON" without quotes into the config file my.ini.
Make sure you add this phrase right underneath the [mysqld] statement in the config file.
You will find my.ini under C:\ProgramData\MySQL\MySQL Server 5.7 if you had conducted the default installation of MySQL.

On Windows you can run server with option key, no need to change ini files.
"C:\mysql\bin\mysqld.exe" --explicit_defaults_for_timestamp=1

In your mysql command line: SET explicit_defaults_for_timestamp=1

On my system (Windows 8.1), the problem was with the server configuration. The server worked for the first time when I installed it. However, I forgot to check the "run as a service" option and this caused all the problem. I tried all possible solutions available on SO but nothing worked. So, I decided to reinstall MySQL Workbench. On executing the same msi file that I earlier used to install MySQL workbench, I reconfigured the server and allowed to run the server as a service.

On Windows -- open my.ini file, present at "C:\ProgramData\MySQL\MySQL Server 5.6", find "[mysqld]" (without quotes) in next line add explicit_defaults_for_timestamp and then save the changes.

I'm Using Windows 8.1 and I use this command
c:\wamp\bin\mysql\mysql5.6.12\bin\mysql.exe
instead of
c:\wamp\bin\mysql\mysql5.6.12\bin\mysqld
and it works fine..

Related

Mysql 8 select performance degraded for few of the queries [duplicate]

I wanted to disable the ONLY_FULL_GROUP_BY value of sql-mode permanently even restart the MySQL server. following things I have tried to do but which are not working. that set to the default value when restarting the MySQL.
Persisted settings are permanent. They apply across server restarts.
set PERSIST sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
have you tried this?
Using SET GLOBAL to change a variable only changes it for the current MySQL Server process. If the server restarts, changes are reverted. Global variable settings are read from the MySQL Server options file upon startup.
In MySQL 8.0, they added a SET PERSIST command so you can change a global variable and add the setting to the options file at once. Read https://dev.mysql.com/doc/refman/8.0/en/persisted-system-variables.html
If you use an older version of MySQL, you'll have to edit the options file. Editing the options file alone does not change the option in the running MySQL Server instance. You would need to restart the MySQL Server to get it to re-read the options file.
My usual habit is to do both — edit the options file and then also run SET GLOBAL to change it to the same value. That ensures it will be the same after a restart, but it allows me to make the change immediately without restarting.
That said, I recommend you should not disable ONLY_FULL_GROUP_BY. That mode protects you from writing invalid SQL queries.
You can use "SET PERSIST" since MySQL 8.0 to persist "sql_mode" global system variable without ONLY_FULL_GROUP_BY to "mysqld-auto.cnf" as shown below so that "sql_mode" global system variable is not reset after restarting MySQL:
SET PERSIST sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
In addition, with the command below, you can check all persisted system variables in "mysqld-auto.cnf":
mysql> SELECT * FROM performance_schema.persisted_variables;
+-----------------+----------------------------------------------------------------------------------------------------+
| VARIABLE_NAME | VARIABLE_VALUE |
+-----------------+----------------------------------------------------------------------------------------------------+
| sql_mode | STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
| max_connections | 500 |
+-----------------+----------------------------------------------------------------------------------------------------+
And, with the command below, you can remove all persisted system variables from "mysqld-auto.cnf":
RESET PERSIST;
And, with the command below, you can remove only "sql_mode" persisted system variable from "mysqld-auto.cnf":
RESET PERSIST sql_mode;
And, with the command below, you can reset "sql_mode" global system variable:
SET ##GLOBAL.sql_mode = DEFAULT;
Now, "sql_mode" global system variable has the default values with ONLY_FULL_GROUP_BY as shown below:
mysql> SELECT ##GLOBAL.sql_mode;
+-----------------------------------------------------------------------------------------------------------------------+
| ##GLOBAL.sql_mode |
+-----------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+-----------------------------------------------------------------------------------------------------------------------+

How to disable global system variables to reset after restarting MySQL?

I wanted to disable the ONLY_FULL_GROUP_BY value of sql-mode permanently even restart the MySQL server. following things I have tried to do but which are not working. that set to the default value when restarting the MySQL.
Persisted settings are permanent. They apply across server restarts.
set PERSIST sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
have you tried this?
Using SET GLOBAL to change a variable only changes it for the current MySQL Server process. If the server restarts, changes are reverted. Global variable settings are read from the MySQL Server options file upon startup.
In MySQL 8.0, they added a SET PERSIST command so you can change a global variable and add the setting to the options file at once. Read https://dev.mysql.com/doc/refman/8.0/en/persisted-system-variables.html
If you use an older version of MySQL, you'll have to edit the options file. Editing the options file alone does not change the option in the running MySQL Server instance. You would need to restart the MySQL Server to get it to re-read the options file.
My usual habit is to do both — edit the options file and then also run SET GLOBAL to change it to the same value. That ensures it will be the same after a restart, but it allows me to make the change immediately without restarting.
That said, I recommend you should not disable ONLY_FULL_GROUP_BY. That mode protects you from writing invalid SQL queries.
You can use "SET PERSIST" since MySQL 8.0 to persist "sql_mode" global system variable without ONLY_FULL_GROUP_BY to "mysqld-auto.cnf" as shown below so that "sql_mode" global system variable is not reset after restarting MySQL:
SET PERSIST sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
In addition, with the command below, you can check all persisted system variables in "mysqld-auto.cnf":
mysql> SELECT * FROM performance_schema.persisted_variables;
+-----------------+----------------------------------------------------------------------------------------------------+
| VARIABLE_NAME | VARIABLE_VALUE |
+-----------------+----------------------------------------------------------------------------------------------------+
| sql_mode | STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
| max_connections | 500 |
+-----------------+----------------------------------------------------------------------------------------------------+
And, with the command below, you can remove all persisted system variables from "mysqld-auto.cnf":
RESET PERSIST;
And, with the command below, you can remove only "sql_mode" persisted system variable from "mysqld-auto.cnf":
RESET PERSIST sql_mode;
And, with the command below, you can reset "sql_mode" global system variable:
SET ##GLOBAL.sql_mode = DEFAULT;
Now, "sql_mode" global system variable has the default values with ONLY_FULL_GROUP_BY as shown below:
mysql> SELECT ##GLOBAL.sql_mode;
+-----------------------------------------------------------------------------------------------------------------------+
| ##GLOBAL.sql_mode |
+-----------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+-----------------------------------------------------------------------------------------------------------------------+

MySQL:how to export data from mysql to a txt?

i want to export data from mysql to a txt.
i use
select * from users2 into outfile 'C:\ProgramData\MySQL\MySQL Server 5.7\Uploads\1.txt'
but there is an error:
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
i try to solve with:
show variables like '%secure%';
the result is that:
+--------------------------+------------------------------------------------+
| Variable_name | Value |
+--------------------------+------------------------------------------------+
| require_secure_transport | OFF |
| secure_auth | ON |
| secure_file_priv | C:\ProgramData\MySQL\MySQL Server 5.7\Uploads\ |
+--------------------------+------------------------------------------------+
the path is that i use in the above.
so i don`t know how to solve it.
You might want to go through the MySQL Documentation regarding this, here:
Documentation quotes the following points:
secure_file_priv may be set as follows:
If empty, the variable has no effect. This is not a secure setting.
If set to the name of a directory, the server limits import and export operations to work only with files in that directory. The directory must exist; the server will not create it.
If set to NULL, the server disables import and export operations. This value is permitted as of MySQL 5.7.6.
Based on the above, you have to explicitly specify this in your my.cnf as the following:
A "" value can be set with secure_file_priv=""
A NULL value can be set with secure_file_priv=NULL
A specific folder value can be with secure_file_priv=My_Specific_Output_Folder
By this, you might want to decide upon the options available and make the necessary change in your my.cnf file and RESTART your MySQL Server service. Once the service is restarted, then you should be able to export data from your query.
Hope this helps!

mysql 5.7.12 - Disabling only_full_group_by sql_mode is not fully successful

I'm using mysql version 5.7.12 on Ubuntu 16.04. I noticed the following exception while running my web application:
SQL Exception : Expression #x of SELECT list is not in GROUP BY clause and contains nonaggregated column 'something.something' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
It used to work perfectly on mysql 5.5. I looked for solutions online. I can understand why they are enforcing only_full_group_by mysql_mode by default in version 5.7. However, I cannot afford at this stage to go back to the code and keep correcting the queries. I chose to disable this sql_mode by editing /etc/mysql/my.cnf file as suggested here.
By appending the following lines to the end of my.cnf and restarted mysql.
[mysqld]
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
To verify that it works I tried the following commands in mysql console:
SELECT ##sql_mode;
SELECT ##GLOBAL.sql_mode;
SELECT ##SESSION.sql_mode;
All of these returned the same result as shown below:
+---------------------------------------------------------------------------------------------+
| ##GLOBAL.sql_mode |
+---------------------------------------------------------------------------------------------+
| STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+---------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
After starting my webapp, I found that it indeed worked for some queries but for others it still threw the same exception. I don't know what to do. Please help me resolve this issue.
Thanks
Please check for the multiple occurrences of my.cnf file in your file system, that could be overriding the sql_mode specified in the above.

MySQL: setting time_zone in my.cnf options file

In MySQL you can set a session variable called time_zone to change the timezone. This is useful e.g. when looking at timestamps from another country. Here is an example:
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2010-12-30 18:59:18 |
+---------------------+
1 row in set (0.00 sec)
mysql> set time_zone='Brazil/East';
Query OK, 0 rows affected (0.00 sec)
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2010-12-30 09:59:29 |
+---------------------+
1 row in set (0.00 sec)
Is it possible to put that in an option file e.g. .my.cnf ?
When I try, it doesn't work. All I get is:
mysql: unknown variable 'time_zone=Brazil/East'
it should be
default_time_zone=Brazil/East
details : http://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_time_zone
Option-File Format = default_time_zone
I'm not certain what has changed in Xampp, but this solution only works if you place this line in the proper place. Trust me I tried many times and had to do a pretty thorough search to find this solution.
default-time-zone = "+00:00"
Example:
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
default-time-zone = "+00:00" <--- Place here.
log_error = "mysql_error.log"
https://community.apachefriends.org/f/viewtopic.php?f=16&t=47656
Also, you'll want to be sure that you have your database populated with the proper time zone names if you are going to use "America/Los_Angeles". I'd recommend using the offset. I'd actually recommend using UTC as your base then converting your time from that point for users based on their timezone which will save you many headaches later and keep your database nice and uniform. Check out the guide I linked below it explained it very clearly for me and I utilized this system. There are many ways to code it but taking this approach will save you a lot of issues.
http://www.vertabelo.com/blog/technical-articles/the-proper-way-to-handle-multiple-time-zones-in-mysql
For MAMP, I added default_time_zone=-03:00 under [mysqld] in /Applications/MAMP/conf/my.cnf
I would get the following error for Brazil/East, probably because its deprecated(https://en.wikipedia.org/wiki/List_of_tz_database_time_zones):
[ERROR] Fatal error: Illegal or unknown default time zone 'Brazil/East'
In ~/.my.cnf:
[mysql]
init_command="SET time_zone='Brazil/East'"
A bit late, but this might be helpful nonetheless:
When explicitly setting the timezone, confirm that you are using the correct timezone name, keeping in mind the many have been deprecated. You can use https://en.wikipedia.org/wiki/List_of_tz_database_time_zones to confirm.
In my case, using MySQL 5.7, the deprecated timezone name was not working when adding it below the [mysqld] in my mysqld.cnf file. Using the new timezone name, and restarting the mysql service, worked.
So for user #kev here, using America/Sao_Paulo should work, instead of using Brazil/East.
Edit the following:
nano /etc/mysql/conf.d/mysql.cnf && systemctl restart mysql ; systemctl status mysql
MySQL.cnf:
[mysql]
default_time_zone=America/Vancouver
See here for current Time Zone formats --> https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
For Windows 10:
Go to C:\ProgramData\MySQL\MySQL Server 8.0 folder and open my.ini file as Administrator.
Scroll to the [mysqld]: line.
Add this lines after the [mysqld]:
#default time-zone
default-time-zone = '<your_time_zone>'
Save the file and restart the MySQL80 service from Services window.
You can control changes from
MySQL Workbench -> Local instance -> Server ->
Status and System Variables -> System Variables -> Filtered -> time_zone