php链接mysql数据库不能用localhost的解决办法

php链接mysql数据库不能用localhost的解决方法
网上看了好多解决的方法,但是还是不管用,我忽然想起Windows中有hosts这么一个文件,既然localhost不能用,那我就把localhost解析成127.0.0.1,问题解决。
host文件目录:C:\WINDOWS\system32\drivers\etc\hosts
PS:
用记事本打开hosts文件,就可以看见了微软对这个文件的说明。Hosts文件文一般有如下面的基本内容

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 localhost



这个文件是根据TCP/IP for Windows 的标准来工作的。它的作用是定义IP地址和
Host name(主机名)的映射关系,是一个映射IP地址和Host name (主机名) 的规定。这个规定中,要求每段只能包括一个映射关系,也就是一个IP地址和一个与之有映射关系的主机名。 IP地址要放在每段的最前面,映射的Host name(主机名)在IP后面,中间用空格分隔。对于这段的映射说明,用“#”分割后用文字说明。

三. Hosts文件的工作方式
现在让我们来看看Hosts在Windows中是怎么工作的。
我们知道在网络*问网站,要首先通过DNS服务器把要访问的网络域名(XXXX.com)解析成XXX.XXX.XXX.XXX的IP地址后,计算机才能对这个网络域名作访问。
要是对于每个域名请求我们都要等待域名服务器解析后返回IP信息,这样访问网络的效率就会降低,因为DNS做域名解析和返回IP都需要时间。
为了提高对经常访问的网络域名的解析效率,可以通过利用Hosts文件中建立域名和IP的映射关系来达到目的。根据Windows系统规定,在进行DNS请求以前,Windows系统会先检查自己的Hosts文件中是否有这个网络域名映射关系。如果有则,调用这个IP地址映射,如果没有,再向已知的DNS服务器提出域名解析。也就是说Hosts的请求级别比DNS高。

本文出自 “韩少西” 博客,请务必保留此出处http://hanshaoxi.blog.51cto.com/1711339/329713