When I attempt to access mysql over an SSH tunnel, I get the error:
ERROR 2005 (HY000): Unknown MySQL server host '[serverhost]' (0)
When I make the attempt through MySQL Workbench or over an ordinary SSH CLI, it works fine, though. What am I doing wrong? (I have scanned multiple related threads on this site, but none that explained this behaviour to me.)
TMI:
The host I have is a name, not an IP.
I open the tunnel thus:
plink.exe [user]@[remote-ip] -P [ssh-port] -pw [pw] -L [local-listen-port]:localhost:[remote-mysql-port]
I call mysql on my command line (through tunnel) thus:
mysql -h [serverhost] -u [user] -p[pw] --port [local-listen-port]
I call mysql over an SSH command line (no tunnel) thus:
mysql -h [serverhost] -u [user] -p[pw] --port [remote-mysql-port]
Your existing plink command is wrong, as its specifying localhost as the destination address (which is the localhost on the other side of the SSH session, so [remote-ip], by your reckoning, which is why you are getting an error) - it should be [serverhost].
Then, you are then mistakenly attempting to connect mysql directly to [serverhost], but the tunnel does not affect routing, so that is incorrect, you should be connecting to localhost.
Your existing example command works when connected to an ssh shell session on [remote-ip], because you are connecting from [remote-ip], which has access to [serverhost] presumably.
Assuming [serverhost] is the remote server, your commands should be:
plink.exe [user]@[remote-ip] -P [ssh-port] -pw [pw] -L [local-listen-port]:[serverhost]:[remote-mysql-port]
Then
mysql -h localhost -u [user] -p[pw] --port [local-listen-port]
EDIT: Let me try and diagram it for you, since you are obviously misunderstanding the nature of the tunnel:
The SSH connection
localhost[?] <--------> remote-ip[22]
The tunnel
localhost[local-port] <---- SSH ---> remote-ip[22] <--- TCP ---> serverhost[remote-port]
Therefore, SSH/plink has bound a port on localhost, using the [local-port] number you have specified, that, when receiving a connection request, establishes a connection from [remote-ip] to [serverhost:remote-port], then shuffles the sends/receives back and forth between them, using its own SSH connection for the hop from [localhost] to [remote-ip].
So - once the tunnel has been set up, to access [serverhost:remote-port], you actually point your tools at [localhost:local-port], and the tunnel routes the traffic to the appropriate place.