Problems
Redirecting stdout, so the file is created by the right user (like in this other answer) is a decent idea, but I assume you need TERMINATED BY, ESCAPED BY and LINES TERMINATED BY. These need INTO OUTFILE. A straightforward solution that redirects stdout just omits INTO OUTFILE, so it cannot use TERMINATED BY etc.
An attempt that uses INTO OUTFILE to write to stdout doesn't work:
SELECT some, fields INTO OUTFILE '/proc/self/fd/1' FROM mytable FIELDS TERMINATED BY ';' ESCAPED BY '' LINES TERMINATED BY '\n';
It doesn't work because the file must not exist before; MySQL must create it by itself. There are valid security reasons for this (example). The special file /proc/self/fd/1 is provided by the kernel so MySQL rejects it.
The path in question is /tmp/mysql_export.csv. When you try to delete mysql_export.csv from /tmp/, the permissions you have for the directory matter. This is how /tmp/ usually looks like:
$ ls -ld /tmp/
drwxrwxrwt 1 root root 1772 Nov 1 19:39 /tmp/
$
The lowercase t in drwxrwxrwt denotes execute permission for "others" and the sticky bit set. About the sticky bit:
The most common use of the sticky bit is on directories residing within filesystems for Unix-like operating systems. When a directory's sticky bit is set, the filesystem treats the files in such directories in a special way so only the file's owner, the directory's owner, or root can rename or delete the file. Without the sticky bit set, any user with write and execute permissions for the directory can rename or delete contained files, regardless of the file's owner.
(source: Wikipedia)
You cannot delete the file because you're neither root nor the owner of the file.
Solution
The above citation just gave us a clue. If you're neither the owner of the file nor root then you need to be:
- (with the sticky bit on the directory) the owner of the directory, or
- (without the sticky bit) any user with write and execute permissions for the directory.
So create a directory somewhere, allow the other user to create files in it (chmod, setfacl) and adjust your procedure to create mysql_export.csv there. After the file appears you will be able to delete it even if it belongs to another user. Use rm -f. Without -f you will probably be prompted because the file does not belong to you.
Notes
- The solution does not go beyond what regular users can do. You don't need
sudo, you don't need to set any additional group in the system.
- Allowing other user(s) to create files in your directory may not end well. You will be able to delete "foreign" non-directories and empty subdirectories. But to delete a non-empty subdirectory you need to delete everything in it first. In general it's not up to you if you can delete files from somebody else's directory, even if it's a subdirectory of your directory. In your case this is not a concern if the other user is just a formal user under your control.