Perfect solution to the problem that MySQL cannot connect to the database through localhost

Perfect solution to the problem that MySQL cannot connect to the database through localhost

Problem: The PHP program on one server cannot connect to the database via the localhost address, but can connect normally if it is set to 127.0.0.1, and can also connect to other database servers normally. The MySQL permissions are set correctly, and the database can be connected normally through the mysql command line client.

Analysis: This is a typical case where the socket is not configured correctly.

There are two ways to connect to a MySQL database: TCP/IP (the kind commonly understood as a port) and Unix sockets (generally called sockets or socks). In most cases, localhost can be used to represent the local machine 127.0.0.1, but when connecting to MySQL, the two cannot be mixed, and localhost and 127.0.0.1 are also set separately in the permission settings in MySQL. When set to 127.0.0.1, the system connects to the database via TCP/IP; when set to localhost, the system connects to the database via socket.

Solution: First, check where the socket file of the local MySQL is. The command to check is:

mysqld --verbose --help | grep socket

The output shows the location of the socket file. For example, this server shows

socket /var/run/mysqld/mysqld.sock

Then modify the php configuration file php.ini to correspond to it.

Find this one:

mysql.default_socket =

Generally this item is empty, change it to:

mysql.default_socket = /var/run/mysqld/mysqld.sock

The file queried in the previous step should be written here and set according to your situation. At this point, the PHP configuration has been modified. If it is CLI (command line) mode or CGI mode, it will take effect immediately. If it is FASTCGI mode, you need to restart the fastcgi process.

The above article perfectly solves the problem that MySQL cannot connect to the database through localhost. This is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • How to quickly modify the host attribute of a MySQL user
  • Does the % in the newly created MySQL user include localhost?
  • How to allow all hosts to access mysql
  • Solution to the problem that MySQL can connect using localhost but cannot connect using IP
  • A brief discussion on the matching rules of host and user when Mysql connects to the database

<<:  Graphical tutorial on installing CentOS 7.3 on VMWare

>>:  Implementation of vscode custom vue template

Recommend

How to delete an image in Docker

The command to delete images in docker is docker ...

Linux ssh server configuration code example

Use the following terminal command to install the...

Introduction to MySQL isolation level, lock and MVCC

This article aims to clarify the relationship bet...

Webpack loads css files and its configuration method

webpack loads css files and its configuration Aft...

A great collection of web standards learning resources

These specifications are designed to allow for bac...

Linux uses dual network card bond and screwdriver interface

What is bond NIC bond is a technology that is com...

Font Treasure House 50 exquisite free English font resources Part 2

Designers have their own font library, which allo...

Implementation of CSS scroll bar style settings

webkit scrollbar style reset 1. The scrollbar con...

Detailed explanation of the implementation of MySQL auto-increment primary key

Table of contents 1. Where is the self-incremente...

Problems with using multiple single quotes and triple quotes in MySQL concat

When dynamically concatenating strings, we often ...

Full steps to create a password generator using Node.js

Table of contents 1. Preparation 2. Writing comma...

Windows10 mysql 8.0.12 non-installation version configuration startup method

This article shares the specific steps for config...

Native JavaScript implementation of progress bar

The specific code for JavaScript to implement the...

Compatibility with the inline-block property

<br />A year ago, there were no articles abo...