MySQL主机127.0.0.1与localhost区别总结

2019-02-1714:01:58数据库教程Comments2,256 views字数 1772阅读模式

题不知道127.0.0.1与localhost有什么区别,但是有的时间会发现使用localhost连接不了,但是改成127.0.0.1即可了连接了,那么他们会有什么区别呢,下面我给大家介绍。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

  1. mysql -h 127.0.0.1 的时候,使用 TCP/IP 连接, mysql server 认为该连接来自于127.0.0.1或者是"localhost.localdomain"
  2. mysql -h localhost 的时候,是 不使用TCP/IP 连接的,而使用 Unix socket ;此时,mysql server则认为该client是来自"localhost"
  3. mysql权限管理中的"localhost"有特定含义:

注意:虽然两者连接方式有区别,但当localhost 为默认的127.0.0.1时,两种连接方式使用的权限记录都是以下的1.row的记录(因为记录在前,先被匹配)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

*************************** 1. row *************************** 
Host: localhost 
User: root 
...... 
*************************** 2. row *************************** 
Host: 127.0.0.1 
User: root

证明:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

shell> mysql -h 127.0.0.1 
mysql> status; 
Currentuser: root@localhost 
SSL: Notin use 
Current pager: stdout 
Using outfile: '' 
Using delimiter: ; 
Server version: 5.1.33-log Source distribution 
Protocol version: 10 
Connection: 127.0.0.1 via TCP/IP 
shell> mysql -h locahostmysql> status; 
Currentuser: root@localhost 
SSL: Notin use 
Current pager: stdout 
Using outfile: '' 
Using delimiter: ; 
Server version: 5.1.33-log Source distribution 
Protocol version: 10 
Connection: Localhost via UNIX socket

发现问题文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

昨天在帮同事编译安装Linux环境时,遇到一个问题:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

Web服务器是apache,数据库是MySQL文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

于是写了一个测试连接数据库的PHP页面:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

$mysql = mysql_connect('localhost','root','');

打开 http://localhost/test.php 测试文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

提示:Can't connect to local MySQL server through socket...文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

检查环境正常文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

以为是数据库没有启动,于是查看一下进程,MySQL在进程里,重启了一下MySQL.文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

使用 mysql -u root -p可以进入MySQL操作界面文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

直接使用/usr/local/php5/bin/php /web/test.php执行可以连上数据库文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

apache也重启了,一样无效文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

疑点:为何网页执行失败,命令执行却成功文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

这下就郁闷了,使用php命令直接执行就成功,通过网页执行就失败。难道是apache导致?网上搜索了大堆资料也没找到解决方案,重新编译安装apache问题依旧。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

把localhost改成127.0.0.1成功文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

把localhost改成127.0.0.1后竟然连接成功了,开始陷入思考困局:localhost失败127.0.0.1却成功?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

ping localhost 地址是127.0.0.1没错文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

打开hosts加入文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

127.0.0.1 qttc文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

使用qttc当主机连接也正常,唯独就不认localhost。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

localhost连接方式不同导致文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

为了了解PHP连接数据库时,主机填写localhost与其它的区别阅读了大量资料,最后得知:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

当主机填写为localhost时mysql会采用 unix domain socket连接文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

当主机填写为127.0.0.1时mysql会采用tcp方式连接文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

这是linux套接字网络的特性,win平台不会有这个问题文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

解决方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

在my.cnf的[mysql]区段里添加文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

protocol=tcp文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/9605.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/sjk/9605.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定