I have two mysql.sock
file.
One is at /opt/lampp/var/mysql/mysql.sock
.
Another one is at /var/run/mysqld/mysqld.sock
.
I want to create a connection using first mysql.sock file.
I also set the default_socket in php.ini
.
But it always connect to second one.
The program won't be error, even if I use wrong path of mysql.sock
.
This is my code:
<?php
$servername = "127.0.0.1";
$username = "my_username";
$password = "my_password";
$database = "";
$port = "3306";
$socket = "/opt/lampp/var/mysql/mysql.sock";
// Create connection
$conn = new mysqli($servername, $username, $password, $database, $port, $socket);
//Check the host info
echo $conn->host_info."\n";
// Check connection
if ($conn->connect_errno) {
printf("Connect failed: %s\n", $conn->connect_error);
exit();
}
$conn->query("set character set utf8");
//Which socket did I connect?
$result = $conn->query("SHOW VARIABLES WHERE Variable_name = 'socket';");
$row = mysqli_fetch_assoc($result);
echo $row['Value']."\n";
//Check the default_socket in php.ini file
echo ini_get("mysqli.default_socket");
?>
The output is:
127.0.0.1 via TCP/IP
/var/run/mysqld/mysqld.sock
/opt/lampp/var/mysql/mysql.sock
How to solve this problem?
Update:
ls -l /opt/lampp/var/mysql/mysql.sock /var/run/mysqld/mysqld.sock
Output is
srwxrwxrwx 1 mysql mysql 0 7月 13 18:10 /opt/lampp/var/mysql/mysql.sock
srwxrwxrwx 1 mysql mysql 0 7月 6 21:06 /var/run/mysqld/mysqld.sock
/opt/lampp/var/mysql/mysql.sock
. I had modifiedphp.ini
and set default_socket to/opt/lampp/var/mysql/mysql.sock
. But it didn't work. It always connect to the second socket file,/var/run/mysqld/mysqld.sock
. If I connect correctly, the second file of output would be/opt/lampp/var/mysql/mysql.sock
, right?new mysqli
line, add this debug statement:echo $conn->host_info . "\n";
And please update the question with new information (in addition to commenting). Other would-be answerers shouldn't have to read the comments for this sort of needed information.echo $conn->host_info."<br>";
after$conn = new mysqli($servername, $username, $password, $database, $port, $socket);
. The output is127.0.0.1 via TCP/IP
.ls -l /opt/lampp/var/mysql/mysql.sock /var/run/mysqld/mysqld.sock
in your question? (I suspect that one of the two is a symbolic link…)