简体   繁体   中英

Is it possible to set MySQL session variables using mysqlimport?

I have an import job that uses unix's mysqlimport, and the source data is undependable, so I need to set sql_mode="NO_ENGINE_SUBSTITUTION" so that the job doesn't fail if there is an error. I'd prefer to do this just for the individual job, but I don't know if it's possible. Does anyone know?

Current command:

mysqlimport --compress --fields-terminated-by="," --fields-optionally-enclosed-by='"' --lines-terminated-by="\\n" --replace --local --user=username --password=password -h localhost dbname company_data.txt

I'm not familiar with mysqlimport, but the manual doesn't list an option for that.

Since mysqlimport is just a command-line interface to the LOAD DATA INFILE SQL statement, a workaround would be this:

mysql -uuser -ppassword dbname -e "set session sql_mode='NO_ENGINE_SUBSTITUTION'; LOAD DATA INFILE 'company_data.txt' INTO ..." /*you can figure out the rest*/

Here's the manual entry for LOAD DATA INFILE .

You can use LOAD DATA INFILE statement instead of the mysqlimport utility, which will then allow you to set the sql_mode prior to the DML statement.

However , setting the SQL mode will have no effect when you load a local file (Ie LOAD DATA LOCAL INFILE ...).

This is what the MySQL documentation has to say about this.

With LOCAL or IGNORE, warnings occur rather than errors, even with a restrictive sql_mode value, and the row is inserted using the same closest-value behavior used for nonrestrictive SQL modes. This occurs because the server has no way to stop transmission of the file in the middle of the operation.

So unless you can get your file onto the same server as MySQL, you cannot ensure data integrity when loading that file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM