I'm performing a query in laravel that selects all products and puts the result in a csv file. This all works fine on windows, but on linux, file-permissions are preventing the action.
My query:
$query =
<<<END
select * from products
INTO OUTFILE '/var/www/html/storage/feeds/products.csv'
fields TERMINATED BY '~' optionally ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
END
DB::statement($query);
My error:
SQLSTATE[HY000]: General error: 1 Can't create/write to file '/var/www/html/storage/feeds/products.csv'
I added the www-data
user and group to the feeds
folder, the folder is currently empty.
Note:
- I've got a copy of this project running on the same server as the db is hosted, here I could just add the permissions of the
mysql
user to thefeeds
folder to fix this. - I'm reaching the db trough an ssh tunnel, so I do not have a
mysql
user on this system to assign access to.
A complete list of users on my system:
root:x:0:0:root:/root:/bin/ash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/mail:/sbin/nologin
news:x:9:13:news:/usr/lib/news:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucppublic:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
man:x:13:15:man:/usr/man:/sbin/nologin
postmaster:x:14:12:postmaster:/var/mail:/sbin/nologin
cron:x:16:16:cron:/var/spool/cron:/sbin/nologin
ftp:x:21:21::/var/lib/ftp:/sbin/nologin
sshd:x:22:22:sshd:/dev/null:/sbin/nologin
at:x:25:25:at:/var/spool/cron/atjobs:/sbin/nologin
squid:x:31:31:Squid:/var/cache/squid:/sbin/nologin
xfs:x:33:33:X Font Server:/etc/X11/fs:/sbin/nologin
games:x:35:35:games:/usr/games:/sbin/nologin
cyrus:x:85:12::/usr/cyrus:/sbin/nologin
vpopmail:x:89:89::/var/vpopmail:/sbin/nologin
ntp:x:123:123:NTP:/var/empty:/sbin/nologin
smmsp:x:209:209:smmsp:/var/spool/mqueue:/sbin/nologin
guest:x:405:100:guest:/dev/null:/sbin/nologin
nobody:x:65534:65534:nobody:/:/sbin/nologin
www-data:x:82:82:Linux User,,,:/home/www-data:/sbin/nologin
application:x:1000:1000:Linux User,,,:/home/application:/bin/bash
nginx:x:100:101:nginx:/var/lib/nginx:/sbin/nologin
EDIT:
I've tried installing mysql and mysql-client on this machine so I have a mysql
user and group to assign to the desitnation folder, but that did not work.
EDIT2: As suggested by @LawrenceCherone, I tried running the query by putting the file into a tmp location. This time the query did not crash, but I could not locate the file. After retrying the query, it crashed, saying the file already exists. I started looking on the mysql server and found it. Tho not a solution, it shines some light on what's happening: the file is not created on the server where I need it, but on the mysql server.