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

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!

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 |
+-----------------------------------------------------------------------------------------------------------------------+

utf8mb4 setting for talend - not working

I am migrating the data from sql server to mysql. I am using the tool Talend(ETL) for the same.
The problem comes when I have emojis in the source (sql server) , it does not get inserted to the table in mysql. So, I know I must use utf8mb4 on mysql side.
The client settings character encoding has to be set, for the smileys to get inserted. The database, tables and the server are all on utf8mb4
But, the client i.e., talend is not utf8mb4. So where do I set this?
I tried with 'set names utf8mb4' in additional parameters of tmysqloutput. But this does not work
I have been stuck on this for days, any help on this would be greatly appreciated
Update :
The job looks like this now. But, the smileys are still getting exported as '?'
Thanks
Rathi
First, make sur that your server is properly configured to use utf8mb4.
Following this tutorial, you need to add the following to your my.cnf (or my.ini if you're on Windows):
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
That tells MySQL server to use utf8mb4 and ignore any encoding set by client.
After that, I didn't need to do set any additional properties on the MySQL connection in Talend. I've executed this query in Talend to check the encoding set by it :
SHOW VARIABLES
WHERE Variable_name LIKE 'character\\_set\\_%' OR Variable_name LIKE 'collation%'
And it returned:
|=-----------------------+-----------------=|
|Variable_Name |Value |
|=-----------------------+-----------------=|
|character_set_client |utf8mb4 |
|character_set_connection|utf8mb4 |
|character_set_database |utf8mb4 |
|character_set_filesystem|binary |
|character_set_results | |
|character_set_server |utf8mb4 |
|character_set_system |utf8 |
|collation_connection |utf8mb4_unicode_ci|
|collation_database |utf8mb4_unicode_ci|
|collation_server |utf8mb4_unicode_ci|
'------------------------+------------------'
The following test to insert a pile of poop works:
Update
Using native MySQL components in Talend 6.3.1, you get mysql-connector-java-5.1.30-bin.jar, which is supposed to automatically detect the utf8mb4 used by the server, but for some reason (bug?) it isn't doing that.
I switched to using JDBC components, and downloaded the latest mysql connector (mysql-connector-java-5.1.45-bin.jar), I got it working by setting these additional parameters on the tJDBCConnection component :
useUnicode=true&characterEncoding=utf-8
(even if I'm specifying utf-8, the doc says it will treat it as utf8mb4)
Here's what my job looks like now :

How to enable explicit_defaults_for_timestamp?

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..

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