PHP – Exporting database records directly into CSV file

I am trying to export a dataset of rows into a csv/txt file with a single query as explained here or here, by executing a Laravel PHP script on a WSL environment with no root user.

The issues I found are the same as mentioned in those posts, but I can’t get my mind into solving it.

When first executing a similar SQL query into MySQL like:

SELECT * INTO OUTFILE "/tmp/activities_export.csv" FIELDS TERMINATED BY "," LINES TERMINATED BY "n" FROM posts;

I receive

Error Code: 1. Can’t create/write to file ‘tmpposts.csv’ (Errcode: 2 – No such file or directory)

Whatever the directory is (I’ve tried with plenty of them) except for the MySQL default directory, which I won’t have access to once deployed.

The file exists and is empty, so I guess it’s the same permissions issue, so far I check:

  • show grants shows GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION
  • SHOW VARIABLES LIKE "secure_file_priv"; which returns null
  • GRANT FILE ON *.* TO 'root'@'localhost'; (As a test) and results are the same, no changes.
  • I tried executing the PHP script with root user, same results.

Any ideas what is missing or what steps are to be done?