Quick question - I run two linux boxes, one my own desktop and the other my VPS. For security reasons on the VPS end I opted for socket connections to MySQL (/var/run/mysqld/mysql.sock). I know I can tunnel like this: ssh -L 3307:127.0.0.1:3306 user@site.com if I set up the remote sql server to listen on some port, but what I want to know is can I do something like: ssh -L /path/to/myremotesqlserver.sock:/var/run/mysqld/mysql.sock thereby tunnelling two sockets, as opposed to two ports?

A perfectly acceptable solution would also be to forward a local port to the remote socket file, but where possible I'm trying not to have tcp servers running on the remote box.

(and yes, I know tcp would be easier).

Thanks all,

Nf.

link|improve this question
If the reason you don't want to use TCP on the mySQL box is because of security concerns (i.e. remote attacks, etc) you can ofcourse either firewall it, and if that is not good enough, make mySQL only listen to 127.0.0.1 for its TCP connections, then you can tunnel through SSH easily. If not, I support the socat solution below. – Mattias Ahnberg Feb 22 at 23:30
feedback

3 Answers

up vote 1 down vote accepted

No, you cannot. At least, I cannot plausibly think of a method to use the MySQL socket file remotely. Your only options involve TCP/IP sockets or running a client local to the server either via SSH or something like phpMyAdmin.

link|improve this answer
Hmmm, what a shame. Ok thanks. I'll leave this open for the moment to see if anyone does know of a way. +1. – Ninefingers Mar 30 '10 at 18:32
feedback

i haven't done this, but i would try with socat. maybe something like:

ssh xxx@yyy.zzz -L 9999:localhost:9999 "socat TCP-LISTEN:localhost:9999 UNIX-CONNECT:/var/run/mysqld/mysql.sock"
socat UNIX-LISTEN:/path/to/local/socket TCP:localhost:9999

again, i have never done anything like this.

link|improve this answer
I'll give it a go and let you know how it goes. I've been using the tcp-based implementation. – Ninefingers Mar 30 '10 at 20:18
I can't get it to work at the moment but +1 anyway for the idea, I like it. I'll let you know if I fix it. – Ninefingers Mar 30 '10 at 20:32
+1 looks like a useful utility. – Warner Mar 30 '10 at 20:43
feedback

Forward a local socket on demand

  • Setup SSH public key authentication
  • Install socat at both ends
  • create a directory locally for your sockets, inaccessible to other users.
export SOCKET_DIR=~/.remote-sockets
socat "UNIX-LISTEN:$SOCKET_DIR/mysqld.sock,reuseaddr,fork" \
EXEC:'ssh user@server socat STDIO UNIX-CONNECT\:/var/run/mysqld/mysqld.sock'

then

mysql -S $SOCKET_DIR/mysqld.sock -u mysqluser -p

stolen from forwarding unix domain sockets with ssh and socat

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.