无法连接到 MySQL 数据库 - PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19433268/
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
Can't connect to MySQL database - PHP
提问by user2867893
Quick question here, I'm new to web hosting and I'm just having a muck around to see what's what. I've created the following PHP document and hosted it on my free 000webhost domain:
在这里快速提问,我是网络托管的新手,我只是想看看是什么。我创建了以下 PHP 文档并将其托管在我的免费 000webhost 域中:
<?php
$mysql_host = "mysqlXX.000webhost.com";
$mysql_database = "***********_people";
$mysql_user = "*************_admin";
$mysql_password = "******************";
// Create connection
$con=mysqli_connect($mysql_host,$mysql_database,$mysql_user,$mysql_password);
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
I have set up (hopefully correctly) a small table in my database with 3 fields:
我在我的数据库中设置了(希望正确)一个包含 3 个字段的小表:
- ID - No Null, auto-increment, integer, primary
- First Name - VarChar, Length 20
- Last Name - VarChar, Length 20
- ID - 无 Null、自动递增、整数、主要
- 名字 - VarChar,长度 20
- 姓氏 - VarChar,长度 20
And entered data for each one. When I open the file from my control panel, I get the following error:
并为每个人输入数据。当我从控制面板打开文件时,出现以下错误:
Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user 'a4935911_people'@'10.1.1.45' (using password: YES) in /home/a4935911/public_html/MySQL+PHP.php on line 9
警告:mysqli_connect() [function.mysqli-connect]:(28000/1045):在 /home/a4935911/public_html/MySQL+PHP 中拒绝用户“a4935911_people”@“10.1.1.45”(使用密码:YES)的访问。第 9 行的 php
AND
和
Failed to connect to MySQL: Access denied for user 'a4935911_people'@'10.1.1.45' (using password: YES)
无法连接到 MySQL:用户“a4935911_people”@“10.1.1.45”的访问被拒绝(使用密码:是)
Any ideas, anyone?
任何想法,任何人?
回答by Andrey Volk
Wrong argument sequence here:
这里的参数顺序错误:
$con=mysqli_connect($mysql_host,$mysql_database,$mysql_user,$mysql_password);
Look at the message: Access denied for user 'a4935911_people'@'10.1.1.45' (using password: YES)
where a4935911_people
is not a user, it is your database's name as set above $mysql_database = "***********_people";
.
查看消息:Access denied for user 'a4935911_people'@'10.1.1.45' (using password: YES)
where a4935911_people
is not a user, it is your database's name as set up $mysql_database = "***********_people";
。
Use: Host, user, password, dbname
as described in the Documentation.
使用:Host, user, password, dbname
如文档中所述。
Solution:
解决方案:
$con=mysqli_connect($mysql_host, $mysql_user, $mysql_password, $mysql_database);
回答by Abdul Razak
argument sequence is following order.
$host ="localhost" // change to your host
$username ="root" // change to your mysql user name by default root
$pass =" " //add password if any
mysql_connect($host,$username,$pass) //don't change the sequence
mysql_select("test") // create the test database in mysql
回答by Elon Than
Two possible reasons:
两个可能的原因:
Wrong username/password - check it carefully.
You can't connect remotely, connections are allowed only via localhost. Check your hosting website for such information. If it's not allowed, you can do nothing.
错误的用户名/密码 - 仔细检查。
您无法远程连接,只能通过本地主机连接。检查您的托管网站以获取此类信息。如果不允许,您将无能为力。
回答by ddavison
In Addition to the possible conclusions that @ElonThan provides...
除了@ElonThan 提供的可能结论之外......
it's also possible that your username lacks the proper privileges to the database you are connecting to.
也有可能您的用户名缺乏对您正在连接的数据库的适当权限。
回答by Kamal AL-Hinai
For 000webhost.com (Free Hosting Service ) you should Test your code online by uploading it to 000webhost.com server.
对于 000webhost.com(免费托管服务),您应该通过将代码上传到 000webhost.com 服务器来在线测试您的代码。
That will solve your problem for sure.
那肯定能解决你的问题。
回答by Rajesh Paul
You need to set up mysql to allow remote connection for a particular userin the MySQL database server i.e. mysqlXX.000webhost.comhere . The default syntax is:
您需要设置 mysql 以允许远程连接MySQL 数据库服务器中的特定用户,即此处的mysqlXX.000webhost.com。默认语法是:
grant <permission> on <database> to <user>@<location> identified by <password>
grant <permission> on <database> to <user>@<location> identified by <password>
So here you have to use(as you have specified the parameters)-
所以在这里你必须使用(因为你已经指定了参数)-
grant all on ***********_people.* to '*************_admin'@'10.1.1.45' identified by '******************'
grant all on ***********_people.* to '*************_admin'@'10.1.1.45' identified by '******************'
So for your case run the following command-
grant all on database_name.* to 'username'@'your_ipaddress' identified by 'password'
因此,对于您的情况,请运行以下命令-
grant all on database_name.* to 'username'@'your_ipaddress' identified by 'password'
Run this command in MySQL command prompt.
在 MySQL 命令提示符下运行此命令。
This will allow usernameto connect from your IPusing that passwordand give all permissions on all tables in the specified database.
这将允许用户名以从您的IP连接使用的密码,并就在指定的数据库中所有表的所有权限。
- To allow any userto connect from any IP addressand use all the tables in any databaseuse the following syntax-
- 要允许任何用户从任何 IP 地址连接并使用任何数据库中的所有表,请使用以下语法-
grant all on *.* to '%'@'%' identified by 'password'
grant all on *.* to '%'@'%' identified by 'password'
And finally you have to use the following command-
最后你必须使用以下命令 -
FLUSH PRIVILEGES;
FLUSH PRIVILEGES;
To reload all privileges.
重新加载所有权限。