search
HomeBackend DevelopmentPHP TutorialPHP/MySQL中的localhost和127.0.01

接上一篇 Nginx 和 PHP-FPM 权限安全配置最后一段关于数据库连接失败的处理。

之前配置WordPress时, 当时想让mysql连接走tcp, google一下就得到答案:

define('DB_HOST', '127.0.0.1');

但是, 当时没注意看wp-config.php配置, 原先已经有一条配置了:

define('DB_HOST', 'localhost');

后来在针对Discuz论坛做权限处理时, 因为mysql也是走的unix domain socket, 所以想看看如何改为走tcp, 当时看到是配了localhost, 总想着是不是配一个端口上去就强制走tcp了。

折腾了半天, 还是不行, 直接通过报错定位PHP代码, 最终定位到 mysql_connect()函数上。

看了文档也没看出哪里有问题, 随手把 localhost改为 127.0.0.1, 突然发现行了, 兴奋之余也感觉很莫名其妙。

初以为是PHP对 localhost作了特殊处理, 直接解析为mysql sock路径了。

今天折腾完手头事情再回头看看这个问题。

依然在本地作了一个PHP-FPM chroot的隔离环境, 测试脚本:

root@gentoo-local /var/www/test % cat index.php<meta charset="utf-8"><?php    define('DB_HOST','localhost');    //define('DB_HOST','127.0.0.1');    define('DB_USER','root');    define('DB_PWD','******');    $connect = mysql_connect(DB_HOST, DB_USER, DB_PWD) or die('数据库连接失败,错误信息:'.mysql_error());    echo $connect;?>

和昨天的情况一样, 如果是 localhost, 则失败; localhost:3306也失败; 127.0.0.1则OK。

因为知道是哪块的问题, 所以搜索起来离正确的答案也就越来越近。

于是搜到了这篇回答 Warning: mysql_connect(): [2002] No such file or directory, 里面有一句话:

The reason is that "localhost" is a special name for the mysql driver making it use the unix socket to connect to mysql instead of the a tcp socket.

这里说是因为mysql对localhost的解析问题。和我之前猜测是PHP解析的不一致。

本地mysql命令行客户端测试, 正常情况下下面两者都OK ( -h localhost是默认方式, 可不指定):

$ mysql -h localhost -u root -p   # OK$ mysql -h 127.0.0.1 -u root -p   # OK

因为 localhost 是 /etc/hosts中定义的, 所以临时注释掉这块, 自然localhost就变成无意义的字串了, 再次尝试:

$ mysql -h localhost -u root -p   # OK$ mysql -h 127.0.0.1 -u root -p   # OK

两者依然OK, 所以可以确定原因应该是mysql driver对 db host 的解析问题了。

另外, 通过 lsof也可以看到连接进程的通信:

# localhostmysql 27971 tankywoo  3u  unix 0xffff880078d25540  0t0  371911 type=STREAM# 127.0.0.1mysql 28249 tankywoo  3u  IPv4 373797       0t0  TCP localhost:50187->localhost:mysql (ESTABLISHED)

按照经验来说, localhost和 127.0.0.1是等价的, 如果主机是非IP地址, 都会尝试去做解析操作。MySQL可能是考虑到unix domain socket效率更高, 所以默认对localhost作了这个处理。

但是这种处理太不明显, 或者说模凌两可, 所以给我的感觉是「坑」的性质更大一些。

后续针对这块又搜了下, 发现遇到这个问题的人真不少, 就比如上面StackOverflow的那个回答, 有接近300的Up vote就能看出来。

:(

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor