CentOS Install lamp environment when accessing local MySQL database error

Source: Internet
Author: User
Tags php language

Accessing the local MySQL database error when installing the lamp environment recently

The error is as follows:

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

Problem Description:

The installation of the MySQL server file location is modified in the home directory, causing local access problems.


Looking for a long time on the Internet to find a solution to the problem:

1, first ensure that the use of commands on the local can login, if also prompted sock file location problem, then 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 use mysqli can not access the problem

Workaround:

Find the PHP configuration file (/etc/php.ini) to find the Mysqli.default_socket value

Modified to Mysqli.default_socket =/home/mysqldata/mysql.sock# This is the location of my sock file


Problem analysis (This is the solution I found on the Internet):

On Unix, MySQL programs treat the host name of localhost specially, in a-a-to-be likely different from-what-you-expect CO Mpared 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 was given to specify a port number. To ensure 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, the UNIX system needs to convert the address of localhost to a file

Workaround three other

There is a few ways to solve this problem.

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

  2. You can change the socket inphp.ini: Open the MySQL configuration filemy.cnfTo find where MySQL creates the socket, and set PHP ' smysqli.default_socketTo that path. On my system it ' s/var/run/mysqld/mysqld.sock.

  3. 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 connection file in localhost to 127.0.0.1

2. Viewin the my.cnf filesock文件的位子,并赋值到php.ini:文件中的mysqli.default_socket去

3, PHP language directly more than one parameter points to the sock file location.


This article is from the "1000 Space" blog, be sure to keep this source http://3609884.blog.51cto.com/3599884/1828364

CentOS Install lamp environment when accessing local MySQL database error

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.