Get the App
SLTechnology News&Howtos  ›  Database  › 

Centos error accessing local MYSQL database while installing LAMP environment

Shulou Source: shulou.com Published: 2022-06-01 12:52:06 10月12日 Update

Recently, there was an error accessing the local MYSQL database while installing the LAMP environment

The error is as follows:

Warning: mysqli::mysqli (): (HY000/2002): Can't connect to local MySQL server

Problem description:

Local access problems are caused by modifying the location of the installation mysql server files in the home directory.

After searching for a long time on the Internet, we finally found a solution to the problem:

1. First, make sure that you can log in using the command on the local machine. If there is also a problem prompting the location of the sock file, you need to modify the mysql configuration file (/ etc/my.cnf).

Add the following configuration:

[mysql]

Socket=/home/mysqldata/mysql.sock#, this is the location of my sock file.

2. Php cannot be accessed by using mysqli

Solution:

Find the PHP configuration file (/ etc/php.ini) and find the value mysqli.default_socket

Change to mysqli.default_socket = / home/mysqldata/mysql.sock# this is the location of my sock file

Problem analysis (this is the solution I found online):

On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a-port or-P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use-- host or-h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the-protocol=TCP option.

In fact, it means that the UNIX system needs to convert the localhost address to the file.

There are three other solutions.

There are a few ways to solve this problem.

You can just use TCP/IP instead of the Unix socket. You would do this by using 127.0.0.1instead of localhost when you connect. The Unix socket might by faster and safer to use, though.

You can change the socket in php.ini: open the MySQL configuration file my.cnf to find where MySQL creates the socket, and set PHP's mysqli.default_socket to that path. On my system it's / var/run/mysqld/mysqld.sock.

Configure the socket directly in the PHP script when opening the connection. For example:

$db = new MySQLi ('localhost',' kamil','* *,', 0, / var/run/mysqld/mysqld.sock')

1. Modify the localhost in the connection file to 127.0.0.1

2. Check the location of the sock file in the my.cnf file and assign it to mysqli.default_socket in the php.ini: file.

3. PHP language has one more parameter directly pointing to the location of the sock file.

Tags: File problem location method this is configuration error data database environment three seats parameters commands addresses that is point to scheme server Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Apple macOS Shulou Information Shulou Tech Info