php MySqli 无法连接到本地主机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17654482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:13:33  来源:igfitidea点击:

MySqli can't connect to localhost

phpmysqlixampphost

提问by Tenescu Andrei

I have this line of code:

我有这行代码:

$mysqli = new mysqli("localhost", "user", "pass", "db");

I'm using XAMPP for the Apache and MySQL. If I use the line above it throws the following error:

我将 XAMPP 用于 Apache 和 MySQL。如果我使用上面的行,则会引发以下错误:

Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'user'@'localhost' (using password: YES) in C:\xampp\htdocs\xo\php\connect.php on line 2
Failed to connect to MySQL: (1045) Access denied for user 'user'@'localhost' (using password: YES)
Warning: main(): Couldn't fetch mysqli in C:\xampp\htdocs\xo\php\connect.php on line 6

It's the same if I replace localhost with 127.0.0.1(I tried). But if I use my internal network IP(in this case 192.168.1.101) instead of localhost it connects successfully.

如果我用 127.0.0.1 替换 localhost 也是一样的(我试过)。但是,如果我使用我的内部网络 IP(在本例中为 192.168.1.101)而不是 localhost,它会成功连接。

回答by lgabster

Try it this:

试试这个:

$mysqli = new mysqli("localhost", "user", "pass", "db", 3306);
                                                        port

回答by vitor_gaudencio_oliveira

you're having problems with your params

你的参数有问题

$mysqli = new mysqli($localhost, $user, $pass, $db);

$localhost, $user, $pass, $db (these params should be consistent)

回答by kcmaharshi

I got the error:

我得到了错误:

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'dinesh'@'localhost' (using password: YES) in C:\xampp..............

警告: mysqli_connect(): (HY000/1045): Access denied for user 'dinesh'@'localhost' (using password: YES) in C:\xampp .....................

I replaced localhostwith my IP address and it finally worked in Windows 10.

localhost用我的 IP 地址替换了它,它终于在 Windows 10 中工作了。

回答by Hyman Chen

After upgrade from php5 to php7, the same problem stuck me for 2 days and finally I found out it is because at php.ini setup, you also need to define the socket location for mysqli. The original mysql got deprecate after php5.5 so after the upgrade, need to add mysqli.default_socket besides mysql.default_socket for its socket location.

从php5升级到php7后,同样的问题困扰了我2天,最后我发现这是因为在php.ini设置中,您还需要为mysqli定义套接字位置。原来的mysql在php5.5后被弃用,所以升级后,除了mysql.default_socket之外,还需要添加mysqli.default_socket作为它的socket位置。

Just need to make sure the following lines is at php.ini :

只需要确保以下几行在 php.ini :

mysqli.default_socket = /path to/mysql.sock mysql.default_socket = /path to/mysql.sock

mysqli.default_socket = /path to/mysql.sock mysql.default_socket = /path to/mysql.sock

回答by augustov

I think it should be:

我觉得应该是:

$mysqli = new mysqli($localhost, $user, $pass, $db);