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.
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.
You can change the socket inphp.ini
: Open the MySQL configuration filemy.cnf
To find where MySQL creates the socket, and set PHP ' smysqli.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 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