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 BY
the LINES TERMINATED BY
sum matches the actual delimiter in the data file. IGNORE 1 LINES
Used 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 INFILE
a 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-priv
the 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_priv
the settings of the options:
SHOW VARIABLES LIKE 'secure_file_priv';
If the result shows secure_file_priv
a value of NULL
, it means the option is not configured.
Configure the secure_file_priv option
If secure_file_priv
not configured, follow the steps below to configure it:
-
Locate the configuration file for the MySQL server (
my.cnf
ormy.ini
). -
Open the configuration file and add
secure_file_priv
options.Add the following line to
[mysqld]
the section to setsecure_file_priv
the 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.
-
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 INFILE
the command to import the data file again.
By following the steps above, you should be able to successfully execute LOAD DATA INFILE
the 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.