php 未知的 MySQL 服务器主机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8938689/
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
Unknown MySQL server host
提问by Malloc
When trying to connect to my database server, i encounter the problem of unknown host:
在尝试连接到我的数据库服务器时,我遇到了未知主机的问题:
Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/2005): Unknown MySQL server host 'xxxxxxxxxxxxx:port' in index.php on line 18
the line 18 is that line where i try to request to connection the the MySQL server:
第 18 行是我尝试请求连接 MySQL 服务器的那一行:
$this->db = new mysqli($db_host, $db_user, $db_psw, $db_name);
I host my database on the 1&1website hosting company.
我在1&1网站托管公司托管我的数据库。
回答by
This is usually the case when name resolving doesn't work on the host. If your connect destination is always the same, you might want to use its IP address for connecting instead.
当名称解析在主机上不起作用时,通常就是这种情况。如果您的连接目标始终相同,您可能希望改用其 IP 地址进行连接。
回答by user3048858
If you use this code:
如果您使用此代码:
$Mysqli= new mysqli('mysql2.servidoreswindows.net:3306',
'usu', 'pass', 'dbname');
you can try to write host without port
您可以尝试在没有端口的情况下编写主机
That is:
那是:
$Mysqli= new mysqli('mysql2.servidoreswindows.net', 'usu', 'pass', 'dbname');
回答by Philip Enc
Please pay attention with AWS security groups:
请注意 AWS 安全组:
In my case I can connect to RDS from my computer throw Telnet and I can connect throw Mysql Workbench also but I cant connect from my EC2 instance that is within the same VPC as the RDS.
在我的情况下,我可以从我的计算机 throw Telnet 连接到 RDS,我也可以连接 throw Mysql Workbench,但我无法从与 RDS 位于同一 VPC 内的 EC2 实例连接。
The solution was:
1. I have created a security group (exampl1) for RDS and assigned to it.
2. I have created a security group (exampl2) for EC2 and assigned to it.
and I have assigned the RDS security group (exampl1) to the EC2 too. << this saves me.
解决方案是:
1. 我为 RDS 创建了一个安全组(示例 1)并分配给它。
2. 我为 EC2 创建了一个安全组(示例 2)并分配给它。
我也将 RDS 安全组(示例 1)分配给了 EC2。<< 这救了我。
Info: If your EC2 has assigned 2 or more security groups, then in the RDS security group inbound source has to create rules as many security groups has your EC2 assigned.
信息:如果您的 EC2 分配了 2 个或更多安全组,则在 RDS 安全组入站源中必须创建规则,因为您的 EC2 分配了许多安全组。
Amazon docs says:
The EC2 instance in the VPC shares the sameVPC security group with the DB instance. http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Overview.RDSSecurityGroups.html
Amazon docs 说:
VPC 中的 EC2 实例与数据库实例共享相同的VPC 安全组。 http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Overview.RDSSecurityGroups.html
I spent all day searching docs.
I hope this help you.
我花了一整天的时间搜索文档。
我希望这对你有帮助。
回答by TibTibs
I know this is an old question. But I ran into this same issue today, and none of the answers I found were the right one.
我知道这是一个老问题。但是我今天遇到了同样的问题,我找到的答案都不是正确的。
The issue in my case ended up being that port 3306 is not open on our firewall, and I was attempting to connect to an internal mysql database from an external server.
我的问题最终是端口 3306 在我们的防火墙上没有打开,我试图从外部服务器连接到内部 mysql 数据库。
I'm not sure why the error it gives you is "Unknown Host" in this case. I would have expected something more like "Unable to connect." or "Connection refused.", but using the exact same code from an internal server worked fine, leading me to this conclusion.
在这种情况下,我不确定为什么它给您的错误是“未知主机”。我本来期望更像“无法连接”。或“连接被拒绝。”,但使用来自内部服务器的完全相同的代码工作正常,导致我得出这个结论。
回答by zundi
Make sure you're not including the "http://" part. I was using http://example.com
in my .env
file. When I left it as example.com
it worked.
确保您不包括“http://”部分。我http://example.com
在我的.env
文件中使用。当我离开它example.com
时。