本地连接时,通过localhost不能登陆到指定的端口
2016-12-22 16:19
405 查看
本地连接时,通过localhost不能登陆到指定的端口
朋友说他的一台服务器上,装了多个mysql,用了不同的端口,通过localhost的方式指定端口时,连上的还是3306的端口
mysql -uroot -hlocalhost -P3307 -p
这样连的是 3306的库。
但是通过
mysql -uroot -h127.0.0.1 -P3307 -p
连到的是 3307的库。
这是为什么
我在我本地一台服务器上建了两个实例, 3306和3308
mysql -uroot -h127.0.0.1 -P3308
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.24 Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select user,host from mysql.user;
+------+------------+
| user | host |
+------+------------+
| root | 10-4-1-104 |
| root | 127.0.0.1 |
| root | ::1 |
| root | localhost |
+------+------------+
mysql -uroot --host=localhost -P3308
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
这种方式,直接就连不上。
查看了文档 5.6 p264
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from
what you expect compared to other network-based programs. For connections to localhost, MySQL
programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port
or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the
local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of
the local server. You can also specify the connection protocol explicitly, even for localhost, by using the
--protocol=TCP option.
当使用localhost的时候,会试着用socket文件,虽然你指定了端口。
指定--protocol=TCP 可以避免这种情况。
在我的环境中:
mysql -uroot -hlocalhost -P3308 --protocol=TCP
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.24 Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show variables like '%port%'
-> ;
+---------------------+-------+
| Variable_name | Value |
+---------------------+-------+
| innodb_support_xa | ON |
| large_files_support | ON |
| port | 3308 |
| report_host | |
| report_password | |
| report_port | 3308 |
| report_user | |
+---------------------+-------+
告诉朋友,让他加上 --protocol=TCP 之后,也可以正常的连接到想连接的端口
总结:在本地使用localhost登陆库时,如果需要连接到别的端口需要 加上--protocol=TCP
朋友说他的一台服务器上,装了多个mysql,用了不同的端口,通过localhost的方式指定端口时,连上的还是3306的端口
mysql -uroot -hlocalhost -P3307 -p
这样连的是 3306的库。
但是通过
mysql -uroot -h127.0.0.1 -P3307 -p
连到的是 3307的库。
这是为什么
我在我本地一台服务器上建了两个实例, 3306和3308
mysql -uroot -h127.0.0.1 -P3308
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.24 Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select user,host from mysql.user;
+------+------------+
| user | host |
+------+------------+
| root | 10-4-1-104 |
| root | 127.0.0.1 |
| root | ::1 |
| root | localhost |
+------+------------+
mysql -uroot --host=localhost -P3308
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
这种方式,直接就连不上。
查看了文档 5.6 p264
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from
what you expect compared to other network-based programs. For connections to localhost, MySQL
programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port
or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the
local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of
the local server. You can also specify the connection protocol explicitly, even for localhost, by using the
--protocol=TCP option.
当使用localhost的时候,会试着用socket文件,虽然你指定了端口。
指定--protocol=TCP 可以避免这种情况。
在我的环境中:
mysql -uroot -hlocalhost -P3308 --protocol=TCP
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.24 Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show variables like '%port%'
-> ;
+---------------------+-------+
| Variable_name | Value |
+---------------------+-------+
| innodb_support_xa | ON |
| large_files_support | ON |
| port | 3308 |
| report_host | |
| report_password | |
| report_port | 3308 |
| report_user | |
+---------------------+-------+
告诉朋友,让他加上 --protocol=TCP 之后,也可以正常的连接到想连接的端口
总结:在本地使用localhost登陆库时,如果需要连接到别的端口需要 加上--protocol=TCP
相关文章推荐
- com.microsoft.sqlserver.jdbc.SQLServerException: 通过端口 1433 连接到主机 localhost 的 TCP/IP 连接
- 关于“telnet localhost:1433 不能打开到主机的连接, 在端口 23: 连接失败”问题
- myeclipse连接只能通过ip不能通过localhost连接问题
- Java JDBC连接SQL Server2005错误:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败
- Java JDBC连接SQL Server2005错误:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败
- Java JDBC连接SQL Server2005错误:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败
- Java JDBC连接SQL Server2008错误:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。。。
- 通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。错误:“Connection refused: connect。
- 解决tomcat服务器下,只能通过localhost,而不能通过127.0.0.1或者本地ip地址访问的问题
- 通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败
- 通过指定的端口进行远程连接
- 通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。错误:“Connection refused: connect。
- sql2005 无法连接服务器 1433端口没有监听,但是本地帐户可以登陆
- SQLserver 通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。错误:“Connecti...
- 正在连接到localhost...不能打开到主机的连接, 在端口 1433: 连接失败
- 通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。错误:“Connecti...
- Java JDBC连接SQL Server2005错误:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。。。
- SQL Server 2005 连接本地端口1433开启远程连接/登陆18456错误的解决方法
- 【JAVA】通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败
- ERROR:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。错误:“Connection refused: connect。请验证连接属性,并检查 SQL Server 的实例正在主机上运行,且在此端口接受 TCP/IP 连接,还要确保防火墙没有阻止到此端口的 TCP