0

I am trying to understand which user is called when I execute a MySQL procedure because my procedure tries to create a file but the file can not be created because of a permission problem.

Here is my procedure:

CREATE PROCEDURE `EXPORT_TEST`()
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY INVOKER
COMMENT ''
BEGIN

DECLARE VAR_INS INT(10);
DECLARE VAR_DT_DEBUT DATETIME;


SET VAR_DT_DEBUT = CURRENT_TIMESTAMP;

INSERT INTO BC_ALIM (NOM_TABLE, DATE_DEBUT)
  VALUES('EXPORT_TEST', VAR_DT_DEBUT);

BEGIN

SELECT * INTO OUTFILE '/home/bureau/IMPORTS/fichierSortie3.csv'
FIELDS TERMINATED BY ';'
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
FROM db_baby_concept.BC_TYPE_LIVRAISON;
END;
END

Thank you for help.

Chris99391
  • 551
  • 1
  • 6
  • 18

1 Answers1

0

MySQL stored procedures have a SQL SECURITY characteristic, which specifies security contact in which the procedure code runs. This can be either DEFINER or INVOKER, and the default is DEFINER. So when you run your stored procedure, it executes with the permissions of the user who defined the procedure; if they don't have the FILE privilege, it won't be able to create the file.

You can set the procedure's security to INVOKER instead. Then it will use your permissions rather than the creator's.

CREATE PROCEDURE procName()
SQL SECURITY INVOKER
BEGIN 
    ...
END;

Also, the file is written by the MySQL server process, so the Linux userid used for the server needs to have write permission to the directory containing the file. So you probably need to make your IMPORTS directory world-writable. And if the secure_file_priv system variable is set, you can only write to the directory named there.

Barmar
  • 741,623
  • 53
  • 500
  • 612