MySQL uses the LOAD DATA INFILE command to import data, configure secure_file_priv, and solve ERROR 1290 (HY000) errors

Table of contents

Preparation

Before performing the import operation, make sure the path and name of the data file are correct and accessible on the MySQL server.

Import data using the LOAD DATA INFILE command

In the MySQL command line terminal, execute the following command to import the data file into the target table:

LOAD DATA INFILE '/myswap/data.csv'
INTO TABLE products
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES;

Please replace it with the actual path and file name of the data file according to the actual situation /myswap/data.csv. Make sure FIELDS TERMINATED BYthe LINES TERMINATED BYsum matches the actual delimiter in the data file. IGNORE 1 LINESUsed to ignore the first line of the data file, usually the field name line.

Fix --secure-file-priv error

When an error is encountered when executing LOAD DATA INFILEa command ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement, it is necessary to solve --secure-file-privthe configuration problem of the option.

View the settings of the secure_file_priv option

Execute the following command in the MySQL command line terminal to view secure_file_privthe settings of the options:

SHOW VARIABLES LIKE 'secure_file_priv';

If the result shows secure_file_priva value of NULL, it means the option is not configured.

Configure the secure_file_priv option

If secure_file_privnot configured, follow the steps below to configure it:

  1. Locate the configuration file for the MySQL server ( my.cnfor my.ini).

  2. Open the configuration file and add secure_file_privoptions.

    Add the following line to [mysqld]the section to set secure_file_privthe option, replacing the directory path with a directory that allows the file to be loaded:

    secure_file_priv = /myswap
    

    Make sure the directory path is correct and that the MySQL server's operating system user has read permissions to the directory.

  3. Save and close the configuration file.

Restart the MySQL server

Execute the appropriate command to restart the MySQL server for the configuration changes to take effect.

  • On Ubuntu use the following command:

    sudo service mysql restart
    
  • On CentOS use the following command:

    sudo systemctl restart mysqld
    ``
    
    

`

  • Restart the service on Windows using the service manager or via the command line.

After reconfiguring the MySQL server, reconnect to the MySQL command line terminal and try executing LOAD DATA INFILEthe command to import the data file again.

By following the steps above, you should be able to successfully execute LOAD DATA INFILEthe command and import the data file into the target table in the MySQL database.

Hope this blog is helpful to you! If you have any other questions, please feel free to ask.

Guess you like

Origin blog.csdn.net/qq_39997939/article/details/131334048