ssh 隧道 mysql,通过SSH隧道的MySQL连接

I have set up a SSH tunnel between two servers A and B. B has MySQL server, and this works:

mysql -h localhost -P 3306 -u user -p

While this doesn't:

mysql -h 127.0.0.1 -P 3306 -u user -p

Although my.cnf has these lines:

bind-address = 127.0.0.1

# Next addr differs slightly, but anyway

bind-address = 99.99.99.99

Now about the tunnel. It connects the following:(A) localhost(9989) -> (B) localhost(3306)

But when (on A, with ports forwarded) I do

mysql -v -h 127.0.0.1 -P 9989 -u user userdb -p

I get ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0

And when I do

mysql -v -h localhost -P 9989 -u user userdb -p

I get ERROR 1045 (28000): Access denied for user 'user'@'localhost' (using password: YES)

What might be the reason? What am I doing wrong?

解决方案

There are three issues here.

1 - Forget about the SSH tunnel for now

You cannot bind MySQL to more than one specific IP.

The first bind-address clause is overridden (therefore, ignored) by the second one. Your server only listens to 99.99.99.99.

The reason why you can connect with -h localhost but not with -h 127.0.0.1 is that in the first form, you do not actually connect through TCP/IP, but through a local socket.

Look in your my.cnf for a socket clause.

Remove one redundant bind-address clause. You may want to use bind-address=0.0.0.0, which instructs MySQL daemon to listen to all network interfaces.

2 - Let's setup your SSH tunnel

The reason for you error ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0 is not obvious to me. I suspect SSH tunnel is actually established only when it receives a connection request (in your case, when you run the mysql client). Since your server does not listen to 127.0.0.1 (see previous paragraph), the SSH tunnel cannot be established, connection fails, and your client interprets it as a network failure.

3 - Why mysql -v -h localhost -P 9989 -u user userdb -p fails

Please post the output of

[edit : just added ...OR host LIKE 'localhost' below, as this might be relevant for troubleshooting purposes]

mysql > SELECT user, host FROM mysql.user WHERE user LIKE 'user' OR host LIKE 'localhost';

(replace 'user', after the LIKE clause, with the actual user name if necessary)

MySQL access control checks both the username/password (user) and the origin of the connection (host) to identify a user. You probably did not create a user 'user'@'localhost'.

N.B.: mysql.com being unreachable from my location at this time, I cannot link to the relevant manual pages.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值