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

Centos error accessing local MYSQL database while installing LAMP environment

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

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.

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