Linux 为什么 mySQL 在任何/所有端口连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4395923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why is mySQL connecting at any/all ports
提问by Girish Dusane
I'm running Linux Mint and trying to connect to mySQL this way
我正在运行 Linux Mint 并尝试以这种方式连接到 mySQL
mysql --port=3306 -u root -p
Then it prompts me for my password. This is all fine. Why is it that when I type something like this it still works....
然后它提示我输入密码。这一切都很好。为什么当我输入这样的东西时它仍然有效......
mysql --port=1234 -u root -p
Should that not fail since there is no mySQL server running on port 1234?
由于没有在端口 1234 上运行的 mySQL 服务器,这应该不会失败吗?
The reason I am asking this is because I want to create a SSH tunnel to connect to a database on another server. Let's say the SSH tunnel will forward all my traffic from localhost:3308 to myremoteserver:3306. Since my local mySQL server is accepting my connections on all ports, I cannot actually connect to port 3308 and hit the remote server. I am still hitting my local server....
我问这个的原因是因为我想创建一个 SSH 隧道来连接到另一台服务器上的数据库。假设 SSH 隧道会将我所有的流量从 localhost:3308 转发到 myremoteserver:3306。由于我的本地 mySQL 服务器在所有端口上都接受我的连接,因此我实际上无法连接到端口 3308 并访问远程服务器。我还在打我的本地服务器....
Even if my SSH tunnel options might have been wrong, I was wondering if anyone knew why I can connect to port 1234 and it still hit my local mySQL server running on 3306?
即使我的 SSH 隧道选项可能是错误的,我想知道是否有人知道为什么我可以连接到端口 1234 并且它仍然访问我在 3306 上运行的本地 mySQL 服务器?
采纳答案by titanoboa
IIRC mysql connects you to a Unix socket if you are connecting to localhost. Since it does not connect you via TCP in this case, there is no port involved and the port number you give does not matter.
如果您连接到本地主机,IIRC mysql 会将您连接到 Unix 套接字。由于在这种情况下它不会通过 TCP 连接您,因此不涉及任何端口,您提供的端口号也无关紧要。
Edit: Not sure if this is true on all systems, but If I use 127.0.0.1 or the hostname instead of localhost, mysql connects via TCP and the port number does matter - I can connect with the correct port number only.
编辑:不确定这是否适用于所有系统,但如果我使用 127.0.0.1 或主机名而不是 localhost,mysql 通过 TCP 连接并且端口号很重要 - 我只能使用正确的端口号连接。
回答by Lee
It will ask you for your password beforeit tries to connect. If you enter your password (or anything else for that matter), and let it proceed, it will respond with something like:
它会在尝试连接之前询问您的密码。如果您输入您的密码(或其他任何与此相关的内容),并让它继续进行,它将响应如下:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/local/var/run/mysql5/mysqld.sock'
回答by P0P3Y
To force a TCP connection use --protocol=TCP
.
要强制 TCP 连接,请使用--protocol=TCP
.
Example:
例子:
First the SSH tunnel
首先是SSH隧道
ssh -L 4000:localhost:3306 server.ch
and then connect to the remote mysql server with
然后连接到远程mysql服务器
mysql -h localhost --port=4000 --protocol=TCP -u root -p
回答by nolazybits
@titanoboa, thx for this! I was having the same issue. Just to add you can actually force TCP connection even for localhost using the following
@titanoboa,谢谢!我遇到了同样的问题。只是添加您实际上可以使用以下命令强制 TCP 连接,即使对于本地主机
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
protocol = TCP
Cheers
干杯