php 从本地主机连接到服务器数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23860910/
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
Connect to server database from localhost
提问by falcon
What I'm trying to do is connecting to server database from localhost.
我想要做的是从本地主机连接到服务器数据库。
$host = 'http://www.my-domain.com/phpmyadmin/';
$user = 'u5er';
$pass = 'pa55w0rd';
$db = 'db_name';
$con = mysqli_connect($host,$user,$pass,$db) or die("Error " . mysqli_error($con));
$sql = "SELECT col FROM test WHERE id = '1'";
$result = mysqli_query($con,$sql);
Errors
错误
Warning: mysqli_connect(): in C:\xampp\htdocs\sp_concord\cenova_nabidka\page\vytvor.php on line 362
Warning: mysqli_connect(): in C:\xampp\htdocs\sp_concord\cenova_nabidka\page\vytvor.php on line 362
Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\sp_concord\cenova_nabidka\page\vytvor.php on line 362
Error
Is it possible to resolve it? How to find out if my server allows external connections? And how to define from which IP addresses it should allow access to database?
有没有可能解决它?如何确定我的服务器是否允许外部连接?以及如何定义它应该允许从哪些 IP 地址访问数据库?
Thank you for any suggestion.
谢谢你的任何建议。
EDIT:Ok, I have changed the host to my-domain.com and now it reports the following errors. My IP has no acces to MySQL server...
编辑:好的,我已将主机更改为 my-domain.com,现在它报告以下错误。我的 IP 无法访问 MySQL 服务器...
Warning: mysqli_connect(): (HY000/1130): Host '88.146.210.54' is not allowed to connect to this MySQL server in C:\xampp\htdocs\sp_concord\cenova_nabidka\page\vytvor.php on line 362
Failed to connect to MySQL: Host '88.146.210.54' is not allowed to connect to this MySQL server
Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\sp_concord\cenova_nabidka\page\vytvor.php on line 368
回答by Reids Meke Meke
if you upload your localhost file to web server, some host provider, use "localhost" too as hostname
如果您将 localhost 文件上传到 Web 服务器(某些主机提供商),也请使用“localhost”作为主机名
$host = 'localhost';
$user = 'u5er';
$pass = 'pa55w0rd';
$db = 'db_name';
"Host 'XXX.XXX.XXX' is not allowed to connect to this MySQL server" appear when you didn't configure host to grant accessing database
未配置主机授予访问数据库权限时出现“Host 'XXX.XXX.XXX' is not allowed to connect to this MySQL server”
回答by Martin Konecny
Your logging has an error is on the following line:
您的日志记录有错误在以下行:
$con = mysqli_connect($host,$user,$pass,$db) or die("Error " . mysqli_error($con));
$con
is never assigned a value (base mysqli_connect
failed) and you are passing it to mysqli_error()
.
$con
从未分配过值(基础mysqli_connect
失败)并且您将其传递给mysqli_error()
.
Try the following instead - it will give you the information you need about why you cannot connect:
请尝试以下操作 - 它将为您提供有关无法连接的原因的所需信息:
$con = mysqli_connect($host,$user,$pass,$db);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
As user Dagon mentioned, your host string should be something like my-domain.com
or even more likely, localhost
and not http://www.my-domain.com/phpmyadmin/
正如用户 Dagon 所提到的,您的主机字符串应该类似于my-domain.com
或更可能,localhost
而不是http://www.my-domain.com/phpmyadmin/
回答by Kinnectus
Most external databases are connected to via an IP or domain/subdomain with correct port number. You have put "http" which is port 80. Most MySQL are 3306. Your host/extremal provider should have given you an IP of the database server and logon details...
大多数外部数据库通过具有正确端口号的 IP 或域/子域连接到。您已经输入了端口 80 的“http”。大多数 MySQL 是 3306。您的主机/极端提供商应该为您提供数据库服务器的 IP 和登录详细信息...
回答by user3674632
Your IP address must be registered to your host in order to connect to the database
您的 IP 地址必须注册到您的主机才能连接到数据库
回答by bitfhacker
Yes, it's possible to resolve that. Change the variable $host to the right hostname (localhost or other).
是的,可以解决这个问题。将变量 $host 更改为正确的主机名(localhost 或其他)。
To find out if your server allows external connections you can use an online portscan to see if you have the mysql port (default is 3306) open to the outside world.
要了解您的服务器是否允许外部连接,您可以使用在线端口扫描来查看您的 mysql 端口(默认为 3306)是否对外开放。
To define which IP addresses can access your database you should use an firewall rule.
要定义哪些 IP 地址可以访问您的数据库,您应该使用防火墙规则。
回答by DMCoding
The host string is wrong. A valid mysql URL will be of the form 'mysql://hostname.tld/database'. However its very unusual that administrators make SQL databases available over the internet. Most likely you should try to run your php application from the same server or network as the database server.
主机字符串错误。有效的 mysql URL 将采用“mysql://hostname.tld/database”形式。然而,管理员通过 Internet 提供 SQL 数据库是非常不寻常的。您很可能应该尝试从与数据库服务器相同的服务器或网络运行您的 php 应用程序。