php 警告:mysqli_connect():未知的 MySQL 服务器主机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16858965/
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 11:47:46  来源:igfitidea点击:

Warning: mysqli_connect(): Unknown MySQL server host

phpmysqlmysqli

提问by DoubleTrouble

I have some trouble connecting to a mysql server. I can connect using the linux terminal, so I know that my host, port, user, and password works. I cannot, however, connect using PHP.

我在连接到 mysql 服务器时遇到了一些问题。我可以使用 linux 终端连接,所以我知道我的主机、端口、用户和密码有效。但是,我无法使用 PHP 进行连接。

PHP Version:5.2.17

PHP 版本:5.2.17

Server version:5.5.27-log MySQL Community Server (GPL)

服务器版本:5.5.27-log MySQL Community Server (GPL)

Here is a test code example:

这是一个测试代码示例:

<?php

$link = mysqli_connect('host.com:5306', 'user', 'pass', 'db');

/* check connection */

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if (!mysqli_query($link, "SET a=1")) {
    printf("Errormessage: %s\n", mysqli_error($link));
}

/* close connection */
mysqli_close($link);    

?>

Gives the following warning:

给出以下警告:

Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2005): Unknown MySQL server host 'host.com:5306' (1) in /.../test.php on line 3

Connect failed: Unknown MySQL server host 'host.com:5306' (1)

警告: mysqli_connect() [function.mysqli-connect]: (HY000/2005): Unknown MySQL server host 'host.com:5306' (1) in /.../test.php on line 3

连接失败:未知 MySQL 服务器主机“host.com:5306”(1)

Any ideas on what to do?

关于该怎么做的任何想法?

回答by Sony

The port number must be a separate argument:

端口号必须是一个单独的参数:

$link = mysqli_connect('host', 'user', 'pass', 'db', 5306);

回答by Your Common Sense

Any ideas on what to do?

关于该怎么做的任何想法?

Sure. A simple three-step solution for any php function's problem:

当然。任何 php 函数问题的简单三步解决方案:

  1. Open your favorite browser
  2. type php.net/in the address bar followed by a problem function's name:
    `php.net/mysqli_connect` in your case
  3. Hit Enter
  1. 打开您喜欢的浏览器
  2. php.net/在地址栏中键入问题函数的名称:
    在您的情况下为`php.net/mysqli_connect`
  3. 按回车键

Now you have the function's description and can check the proper parameters list.

现在您有了函数的描述并且可以检查正确的参数列表。

回答by Daniel Chernenkov

The host parameter in the mysql function need to be "localhost" or "127.0.0.1"

mysql函数中的host参数需要为“localhost”或“127.0.0.1”

You set the parameter as "host:port" and that is your mistake. It's need to be the full alias of the network in the server or the remote server with the full IP address. You can found more information about this error here: http://compnetworking.about.com/od/workingwithipaddresses/g/127_0_0_1_def.htm

您将参数设置为“host:port”,这是您的错误。它需要是服务器中网络的完整别名或具有完整 IP 地址的远程服务器。您可以在此处找到有关此错误的更多信息:http: //compnetworking.about.com/od/workingwithipaddresses/g/127_0_0_1_def.htm

The mysql book in php.net: http://www.php.net/manual/en/function.mysql-connect.php

php.net中的mysql书:http://www.php.net/manual/en/function.mysql-connect.php

回答by Bouffe

host is the IP of your mysql server, or maybe "localhost"

host 是你的 mysql 服务器的 IP,或者可能是“localhost”