本地连接时,通过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 

本地连接时,通过localhost不能登陆到指定的端口相关推荐

  1. 解决vmware 本地连接时出错:地址仍未与网络终结点关联

    场景:使用vmware创建了server2008的虚拟机,使用的桥接模式,桥接模式是选择的自己的wifi网卡,虚拟机启动后发现无法联网 ipconfig查看(下图),发现ip地址段就不对,应该是192 ...

  2. Win10远程连接自己的电脑提示“登陆没有成功”的解决方案

    问题:提示登录没有成功 猜想: 1)要么是账号密码输入错误,必须是系统的用户名.密码 2)要么是配置问题,配置解决如下: 1.开启允许访问远程 找到此电脑-右键属性-高级系统设置-远程-勾选允许远程连 ...

  3. w7没有服务器认证消息,w7没有有效的ip配置怎么解决_win7本地连接没有有效的Ip配置处理方法...

    每当用户启动win7电脑之后,都需要进行本地连接操作才能够正常上网,但是在本地连接时偶尔也是会出现一些故障,例如有的用户在给win7系统进行本地连接的时候就有提示没有有效的Ip配置的情况,那么win7 ...

  4. win7设置多用户登陆和修改3389端口、win10设置多用户登陆和修改3389端口

    文章目录 设置多用户登陆 第一步:创建需要远程的两个用户账号 第二步:开启计算机远程 第三步:将新建用户添加到远程桌面组 第四步:选择组 第五步:防火墙设置 第六步:进入策略编辑器 第七步:策略修改 ...

  5. Centos下本地连接postgresql时出现认证错误的问题

    出现这个问题,主要原因在于postgresql的配置的认证权限不对. postgresql的配置权限的文件在 /var/lib/pgsql/data/pg_hba.conf 里面主要是关于认证权限的配 ...

  6. 虚拟机与本地连接不上时,该怎么办

    当虚拟机与本地数据库连接时,连接不上,该怎么办? 首先,我们要确保虚拟机上面的地址,与本地数据库的主机地址一致.如果还是无法连接,我们要查看我们系统内是否有虚拟机(操作步骤:1.点击控制面板,2.点击 ...

  7. 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器,sqlserver

    今早开机发现,打开SQL Server 2008 的 SQL Server Management Studio,输入sa的密码发现,无法登陆数据库?提示以下错误: "在与 SQL Serve ...

  8. mysql的连接名和用户名_MySQL登陆认证用户名先后顺序

    下面是从官档中摘抄的关于MySQL在登陆认证时,从user表选择用户验证的先后顺序. mysql> CREATE USER 'monty'@'localhost' IDENTIFIED BY ' ...

  9. linux ssh 设置的相关总结(ssh最大连接数、ssh连接时长、安全性配置等)

    以redhat6.3为例 ssh配置文件在: //ssh/sshd_config 可以打开查看相应配置,默认情况下只开放了几个选项,其余全部#屏蔽掉了. 英文手册参考:http://www.openb ...

最新文章

  1. iOS经典面试题总结--内存管理
  2. Java传xml时字符串乱码_php调用java传递xml字符串乱码怎么办
  3. 使用Json让Java和C#沟通的方法
  4. lightoj 1031 区间dp
  5. 面试中有哪些经典的数据库问题?
  6. java单元测试算初级_Java____Eclipse下JUnit单元测试(初级)
  7. 邱勇校长在2018级研究生开学典礼上的讲话 | 融合成就人生新的高度
  8. matlab数字信号处理 王彬,MATLAB数字信号处理
  9. java笔试题型_JAVA笔试必备题
  10. [USB].USB总线详解
  11. 搭建PHP官方框架zend framework 2(LINUX)
  12. zabbix 自动发现
  13. 在Bash中重定向stderr和stdout
  14. 【转】深度学习最全优化方法总结比较(SGD,Adagrad,Adadelta,Adam,Adamax,Nadam)
  15. lib文件夹 springboot_我把 Spring Boot 项目从 18.18M 瘦身到 0.18M,部署起来真省事!...
  16. 什么是PHP,如何快速运行PHP文件
  17. wsdl文件 服务器地址,webservice 的wsdl文件详解
  18. 虚幻引擎中蓝图间的几种通信方式
  19. Connect Four四子棋c++程序 - 显示窗口(0)
  20. iOS 提交app错误 90096解决办法

热门文章

  1. 机器学习基础-逻辑回归-09
  2. linux安装多版本php_Linux系统下为Nginx安装多版本PHP
  3. html number方法,JavaScript Number 对象
  4. 济南女子学院计算机科学与技术,山东女子学院计算机科学与技术专业2016年在山东理科高考录取最低分数线...
  5. Redis(七)Redis的噩梦:阻塞
  6. AT89C52编程开发源代码
  7. 杭电1018 Big Number
  8. 【转】2010全球最值得模仿的230个网站
  9. Docker 环境的快速搭建
  10. _Ansible批量管理与维护