Reputation: 53
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
Upvotes: 4
Views: 6975
Reputation:
By passing "127.0.0.1"
as the first argument to the mysqli
constructor, you force mysqli to connect to the MySQL server over TCP. This causes the socket argument to be ignored.
To force the connection to be made over a UNIX socket, pass NULL
or "localhost"
as the first argument.
Upvotes: 9