0

I'm working with a remote server to which I access via ssh. At the server there's a mysql database from which I'm making query

SELECT * FROM my_table where date >  '2010-01-01'';

So far so good, the issue raises when I tray to create an output file:

SELECT * FROM my_table where date >  '2010-01-01'  INTO OUTFILE '/home/ubuntu/data/my_output.csv';

I getting a permission error:

ERROR 1 (HY000): Can't create/write to file '/home/ubuntu/data/my_output.csv' (Errcode: 13)

Is there a way sent the file directly to my local machine instead of writing it first on the server where I don't have permissions to write files from the database?

Luis Ramon Ramirez Rodriguez
  • 9,591
  • 27
  • 102
  • 181

1 Answers1

3

You can tunnel the mysql port (on the server) via ssh.

Example of an ssh connection tunneling a port of the server (here in a .ssh/config file on ubuntu):

Host my_mysql_server
Hostname mysql_host_server
Port ssh_port
LocalForward 3307 localhost:3306
User ssh_user

Terminal example:

ssh -L 3307:localhost:3306 ... (hostname, ...)

You can change the 3307 to whatever port you want (should be a port that is not used yet).

Then you can do an export on your local machine. Put your sql query in a sql file (example: my_query.sql) and export it via following command in terminal (ubuntu):

mysql -h localhost -P 3307 your_database_name -u mysql_user -p < my_query.sql > export.csv
Wouter
  • 1,293
  • 2
  • 16
  • 34