Answer the question
In order to leave comments, you need to log in
Why can't connect to MySQL via socket?
Hello. On Debian Jessie installed Nginx + PHP7-FPM + MariaDB 10. I'm trying to run a test script:
<?php
$mysqli = new mysqli(":/var/run/mysqld/mysqld.sock", "root", "password");
if (mysqli_connect_errno()) {
printf("Ошибка соединения: %s\n", mysqli_connect_error());
exit();
}
printf("Версия сервера MySQL: %s\n", $mysqli->server_info);
$mysqli->close();
?>
Connection error: php_network_getaddresses: getaddrinfo failed: Name or service not known
$mysqli = new mysqli(":/var/run/mysqld/mysqld.sock", "root", "password");
$mysqli = new mysqli("localhost:/var/run/mysqld/mysqld.sock", "root", "password");
Connection error: Connection refused
$mysqli = new mysqli("localhost", "root", "password");
MySQL Server Version: 5.5.5-10.1.16-MariaDB-1~jessie
# locate mysqld.sock
/run/mysqld/mysqld.sock
# php-fpm7.0 --info | grep .default_socket
mysqli.default_socket => no value => no value
pdo_mysql.default_socket => /var/run/mysqld/mysqld.sock => /var/run/mysqld/mysqld.sock
Answer the question
In order to leave comments, you need to log in
In fact, you have a socket file located in /run/mysqld/mysqld.sock, and you connect to /var/run/mysqld/mysqld.sock
Plus, we read the documentation , and there the socket is the last parameter.
Part one, make sure the socket file is in the right place. Connect to MySQL and see what you have here:
I have /opt/local/var/run/mariadb-10.0/mysqld.sock there .
So I need to fix php.ini
; я ленивый, поэтому настраиваю все варианты подключений
[MySQL]
mysql.default_socket = /opt/local/var/run/mariadb-10.0/mysqld.sock
[MySQLi]
mysqli.default_socket = /opt/local/var/run/mariadb-10.0/mysqld.sock
[Pdo_mysql]
pdo_mysql.default_socket=/opt/local/var/run/mariadb-10.0/mysqld.sock
// здесь должно быть соединение через сокет
$mysqli = new mysqli("localhost", "root", "password");
// можно принудительно указать путь к сокету
// я очень не рекомендую так делать,
// т.к. возможны проблемы с развертыванием ваших скриптов в других окружениях
$mysqli = new mysqli("localhost", "root", "password", "db", 0,
"/opt/local/var/run/mariadb-10.0/mysqld.sock");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question