二维码

Remember a php mysqli no such file or directory error troubleshooting

1505 人阅读 | 时间:2019年10月17日 09:31

Recently, one of the company's development machine broke down, need to transfer part of the work-related system to another machine, in the process of transferring mantis found that mysql can not be connected, and the error is actually No such file or directory, this error message obviously tells me that the file does not exist, But I am connected to mysql through the network, why do you suggest that the file does not exist? So I wrote a test code.

$mysqli = new mysqli('localhost', 'root', '', 'bugtracker');if ($mysqli->connect_error) {    die('Connect Error (' . $mysqli->connect_errno . '): '. $mysqli->connect_error);
}

Sure enough, it's a mistake!!!!

PHP Warning:  mysqli::mysqli(): (HY000/2002): No such file or directory in /root/test.php on line 2

To make sure the parameters are correct, test with the mysql client

mysql -uroot -hlocalhost

Well, connected, there is no error at all, that means the parameters are no problem, but why the same parameters, mysql client connection is normal, php connection is not? In order to find out exactly, so use trace to run this code again, found that the system called the connect is not actually using tcp, but unixdomain, the file path is /tmp/mysql.sock, and this file really does not exist

connect(3, {sa_family=AF_FILE, path="/tmp/mysql.sock"}, 17) = -1 ENOENT (No such file or directory)

At this point, I already know the source of the error, is the use of unixdomain to connect a non-existing address, resulting in a reporting error, but why use unixdomain connection mysql? And where does this address come from? So then look edout the relevant Funds (Bai) (Du), according to the history books, when using localhost for connection, will use the unixdomain way, so to speak mysql client is also using unixdomain? So I used mysql again.

connect(3, {sa_family=AF_FILE, path="/var/lib/mysql/mysql.sock"}, 110) = 0

Discover that connect does use unixdomain, but... This file path is completely different from the one above, and this path exists. Can the above /tmp/mysql.sock in the end where? So i went through the php document, which made it clear that mysqli's constructor socket parameter (i.e. the unixdomain file path mentioned above) was taken by default from php.ini's mysqli.default.socket, Then check the parameters again.

php -i|grep 'mysqli.default_socket'mysqli.default_socket => no value => no value

Hmm? No value, noisy it is. Then where exactly this goods came from, in order to find out the truth of the matter, began to look through the source code of mysqli, and sure enough, from the source code to find such a paragraph

if (host_len == sizeof("localhost") - 1 && !strncasecmp(host, "localhost", host_len)) {
    DBG_INF_FMT("socket=%s", socket_or_pipe? socket_or_pipe:"n/a");    if (!socket_or_pipe) {
        socket_or_pipe = "/tmp/mysql.sock";
    }
    transport_len = mnd_sprintf(&transport, 0, "unix://%s", socket_or_pipe);
    unix_socket = TRUE;
}

If you use localhost, use unixdomain, and the file path is the socket in the argument, but if the parameter is empty, then the play is dead/tmp/mysql.sock. So the address that doesn't exist in the above misstatement is written to the source code, so you need to set it yourself to the correct value manually. So modify the parameters of mysqli to test again.

$mysqli = new mysqli('localhost', 'root', '', 'bugtracker', 3306, "/var/lib/mysql/mysql.sock");

Sure enough, here, the truth of the matter has been found, not because the localhost was used and the value of mysqli.default-socket was empty, and the default path provided in the source code did not exist. I can't ask here, can I add two judgments? Use localhost and the socket does not use unixdomain for empty, and does not provide a default path, because if you install php and then mysql, then this default-socket will be an empty value.

Of course, to solve this problem is actually very simple, there are a lot of articles on the Internet have written how to solve, such as the value of mysqli.default-socket to the correct path, or localhost to 127.0.0.1 and so on. Mainly want to know why not configure socket will be wrong, to find the root of the problem is good to cure the problem.


©著作权归作者所有:来自ZhiKuGroup博客作者没文化的原创作品,如需转载,请注明出处,否则将追究法律责任 来源:ZhiKuGroup博客,欢迎分享。

评论专区
  • 昵 称必填
  • 邮 箱选填
  • 网 址选填
◎已有 0 人评论
搜索
作者介绍
30天热门
×
×
本站会员尊享VIP特权,现在就加入我们吧!登录注册×
»
会员登录
新用户注册
×
会员注册
已有账号登录
×