Reason why secure-file-priv does not recognize files in its assigned folder?

Viewed 104

I am trying to load a .csv file through mysql.connector in python. Here is the relevant portion of the script:

import os

print(os.listdir(r'C:\ProgramData\MySQL\MySQL Server 8.0\Uploads')) #returns test.csv

import_file = 'C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Uploads\\test.csv'

cnx = mysql.connector.connect(user=usr, 
                              password=passwd,
                              host='127.0.0.1',
                              database='ctr')
cursor = cnx.cursor()

load_data = (
    "LOAD DATA "
    "INFILE "
    "'{}' "
    "INTO TABLE import "
    "FIELDS TERMINATED BY ',' "
    """OPTIONALLY ENCLOSED BY '"' """
    "LINES TERMINATED BY '\\n' "
    "IGNORE 1 LINES".format(import_file))

securefile_query = ("SHOW VARIABLES LIKE 'secure_file_priv'")
cursor.execute(securefile_query)
directory = cursor.fetchall()
print(directory) #returns C:\ProgramData\MySQL\MySQL Server 8.0\Uploads
print(import_file)
print(load_data)
cursor.execute(load_data)

Traceback of the error that follows points to cursor.execute(load_data) in my script, and the last line of the traceback is "mysql.connector.errors.DatabaseError: 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement".

According to the secure_file_priv documentation:

  • 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 does not create it.
  • If set to NULL, the server disables import and export operations.

I have confirmed multiple times that the file is in the correct directory. I have also confirmed the value of secure_file_priv using "SHOW VARIABLES". Doesn't this mean MySQL ought to be able to read test.csv from the Uploads folder? How can I read the file without disabling secure_file_priv?

Edit 17Jan2022: Since I'm running the server locally and have the files locally, I ended up using LOAD DATA LOCAL INFILE as a workaround by adding "allow_local_infile=True" as a parameter inside cnx and then moving the source file to the same directory as the script. Setting secure-file-priv to an empty string (C:\ProgramData\MySQL\MySQL Server 8.0\my.ini) also worked. I still don't know why I was receiving "server is running with the --secure-file-priv option" given the previous location of my files.

0 Answers
Related