Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What if PHP cannot successfully connect to mysql through localhost?

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

The following is to understand how PHP can not successfully connect to mysql through localhost. I believe you will benefit a lot after reading it. The text is not in the essence. I hope that PHP can not successfully connect to mysql through localhost.

First of all, figure out the difference between mysql connection localhost and 127.0.0.1:

127.0.0.1 belongs to the TCP/IP connection, which is transmitted through the network card, depends on the network card, and is restricted by the network firewall and network card. Localhot belongs to the Unix socket connection and is not transmitted through the network card! It will not be parsed into IP, will not be restricted by network firewalls and network cards, and will not occupy network cards and network resources. In general, it is best to use localhost for local services when setting up the program.

My operating system php uses 127.0.0.1 but can connect to mysql:

However, you cannot connect to mysql through localhost:

You get the same error when you try phpmyadmin, but there is also a valuable error prompt (the socket is not set correctly):

The socket here refers to a .sock file in which php communicates with mysql.

Analyze the problem:

My mysql uses mariadb5.5 and is installed through yum, so there is little chance that the problem lies with mysql itself.

Looking for the socket keyword through phpinfo, we can find that mysql.default_socket does not set the default value, but pdo_mysql gives the default value and the location of sock

Edit php.ini to give mysql.default_socket the default path, and the problem can be solved by corresponding to the socket file storage path of mysql. However, there is also a lazy way to create a soft connection to the socket file of mysql in the default path specified by php, so there is no need to modify php.ini.

View the mysql socket path:

# cat / etc/my.cnf

Create a soft connection:

# ln-s / var/lib/mysql/mysql.sock / tmp

Problem solving:

After reading this article about how PHP can't successfully connect to mysql through localhost, many readers will want to know more about it. If you need more industry information, you can follow our industry information section.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report