php mysql_connect(): 无法建立连接,因为目标机器主动拒绝它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21987746/
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
mysql_connect(): No connection could be made because the target machine actively refused it
提问by user3335903
I have this problem when I am trying to run my PHP MySQL script. When I try to run my .php file this is what I get.
我在尝试运行 PHP MySQL 脚本时遇到了这个问题。当我尝试运行我的 .php 文件时,这就是我得到的。
mysql_connect(): No connection could be made because the target machine actively refused it
This is code for dbconnect.php:
这是 dbconnect.php 的代码:
<?php
mysql_connect("localhost","root");
mysql_select_db("users");
?>
I tried to use this format before but I don't know what seems to be the problem with this code.
我之前尝试过使用这种格式,但我不知道这段代码似乎有什么问题。
Thank you in advance.
先感谢您。
回答by echo_Me
You forgot password in your connection.
您忘记了连接中的密码。
Try this.
尝试这个。
mysql_connect("localhost","root" ,"password here");
Check Documentation here.
You should switch to
MYSQLIorPDOas you see thatMYSQLis already deprecated.The initial Mysql password is blank according to this info for mysql ver 5.0. you should check your version.
mysql_connect("localhost","root" ,""); // will connect.
您应该切换到
MYSQLI或PDO如您所见,MYSQL它已被弃用。根据mysql ver 5.0的此信息,初始Mysql密码为空。你应该检查你的版本。
mysql_connect("localhost","root" ,""); // will connect.
EDIT:
编辑:
No connection could be made because the target machine actively refused it
无法建立连接,因为目标机器主动拒绝它
means that no error in your code , but either you have firewall which blocks your connection or your sistem is listening in different PORT.
意味着您的代码中没有错误,但是您的防火墙阻止了您的连接,或者您的系统正在侦听不同的端口。
to do: 1-verify your connecting port default is 3306.
要做: 1-验证您的连接端口默认为 3306。
2-try connect with use "127.0.0.1" instead of "localhost" this maybe it listening on "127.0.0.1".
2-尝试使用“127.0.0.1”而不是“localhost”进行连接,这可能是在侦听“127.0.0.1”。
3-It could also go wrong if the other end is listening on UDP, not TCP.
3-如果另一端正在侦听 UDP 而不是 TCP,它也可能出错。
4- verify your firewall connection if its permitted.
4- 如果允许,请验证您的防火墙连接。
回答by enapupe
This error is NOT password related.
此错误与密码无关。
Password related errors display a response from MySQL server. The subject response is NOT coming from MySQL server, but from the client itself. It could not connect to the server because the MACHINE (not the server) refused it, so, it was more likely to be a network issue or some other thing.
密码相关错误显示来自 MySQL 服务器的响应。主题响应不是来自 MySQL 服务器,而是来自客户端本身。它无法连接到服务器,因为 MACHINE(不是服务器)拒绝了它,因此,它更有可能是网络问题或其他一些事情。
Try connecting to 127.0.0.1. Also check the port you are connecting to and which port is mysql running. Check for firewalls blocking connections. Are you sure is MySQL Server running?
尝试连接到 127.0.0.1。还要检查您要连接的端口以及 mysql 正在运行的端口。检查防火墙阻止连接。您确定 MySQL 服务器正在运行吗?
This answer may have additional info: PHP -MySQL:No connection could be made
这个答案可能有额外的信息:PHP -MySQL:No connection can be made
回答by user7752399
Make sure your mysql Service is running, by going to check in Services: in Search type: Services -> check mysql, start service if it's not running.
确保您的 mysql 服务正在运行,通过检查服务:在搜索类型:服务 -> 检查 mysql,如果它没有运行,请启动服务。
回答by Saksham Khurana
Well sometimes even using localhost without any port number can help. E.g.
好吧,有时即使使用没有任何端口号的 localhost 也有帮助。例如
$conn = mysql_connect("localhost","root","");//for no password
回答by Abhinav
It should be mysql_connect("localhost","root" ,"your password");
它应该是 mysql_connect("localhost","root" ,"your password");
If no password, then leave it blank like this: mysql_connect("localhost","root" ,"");
如果没有密码,则将其留空,如下所示: mysql_connect("localhost","root" ,"");
NOTE:Use Mysqli instead of MySql because mysql is deprecated
注意:使用 Mysqli 而不是 MySql,因为 mysql 已被弃用
回答by Umair Rasool
Okay I fixed this error! there is no coding mistakes your SQL connection. it's caused by port# for SQL. go to your XAmp or wamp tools from active drop down and select "use a port other than 'given #' ". Then enter 3306 there. It will fix the issue. Thanks.
好的,我修复了这个错误!您的 SQL 连接没有编码错误。它是由 SQL 的端口号引起的。从活动下拉列表中转到您的 XAmp 或 wamp 工具,然后选择“使用‘给定#’以外的端口”。然后在那里输入 3306。它会解决这个问题。谢谢。
回答by php_beginner
I had the same problem. The issue was that MySql was not running on Xampp so I had to run it for everything to work.
我有同样的问题。问题是 MySql 没有在 Xampp 上运行,所以我必须运行它才能正常工作。
So kindly ensure MySql is running on your localhost. Go to Xampp [or other localhost] control panel and click start/run under Mysql ].
所以请确保 MySql 在您的本地主机上运行。转到 Xampp [或其他本地主机] 控制面板,然后单击 Mysql 下的启动/运行]。
回答by d.abyss
It looks like you forgot to add your password to your connection. Also, you should not use mysql connect anymore as it is officially depreciated. Use MYSQLi or PDO instead. An example of this would be:
您似乎忘记将密码添加到您的连接中。此外,您不应再使用 mysql connect,因为它已正式折旧。请改用 MYSQLi 或 PDO。这方面的一个例子是:
$connection= mysqli_connect("localhost", "root", "password", "database")
or die(mysqli_error("error connecting to database"));

