php mysqli_connect(): (HY000/2002): 由于目标机器主动拒绝,无法建立连接

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

mysqli_connect(): (HY000/2002): No connection could be made because the target machine actively refused it

phpmysqlixampp

提问by Sami

I know there are many questions like this, but i didn't find any solution in it.

我知道有很多这样的问题,但我没有找到任何解决方案。

Things i tried:-

我尝试过的事情:-

  • checked firewall

  • restarted my PC and Apache server

  • restarted MYSQL

  • checked my code

  • Tried everything i know and found on internet

  • 检查防火墙

  • 重新启动了我的 PC 和 Apache 服务器

  • 重新启动 MYSQL

  • 检查了我的代码

  • 尝试了我在互联网上所知道和发现的一切

here's my code:-

这是我的代码:-

<?php

$dbhost = 'localhost:3360';
$dbuser = 'root';
$dbpass = '';
$db     = 'test_db13';


$conn  = mysqli_connect($dbhost,$dbuser,'',$db);

if(! $conn){
  die('Could not connect connect: ') ;
}

echo 'Successfully Connected';


$sql = 'Connected Successfully';
  $retvalue = mysqli_query($sql);

  if(! $retvalue){
    die('Cannot connect to SQL: ');
  }

echo 'DataBase test_db13 has successfully created';

mysqli_close($conn);

 ?>

I did set the password, but it still is showing me the error.

我确实设置了密码,但它仍然向我显示错误。

Here's the firewall picture:- enter image description here

这是防火墙图片:- 在此处输入图片说明

lastly XAMMP is running here's the proof

最后 XAMMP 正在运行,这是证明

enter image description here

在此处输入图片说明

回答by Qirel

If you look at your XAMPP Control Panel, it's clearly stated that the port to the MySQL server is 3306- you provided 3360. The 3306is default, and thus doesn't need to be specified. Even so, the 5th parameter of mysqli_connect()is the port, which is where it should be specified.

如果您查看 XAMPP 控制面板,会清楚地说明 MySQL 服务器的端口是3306- 您提供的3360. 这3306是默认值,因此不需要指定。即便如此,第 5 个参数mysqli_connect()是端口,也就是应该指定的地​​方。

You could just remove the port specification altogether, as you're using the default port, making it

您可以完全删除端口规范,因为您使用的是默认端口,使其成为

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db     = 'test_db13';

References

参考

回答by dy'sback

You have entered wrong port number 3360 instead of 3306. You dont need to write database port number if you are using daefault (3306 in case of MySQL)

您输入了错误的端口号 3360 而不是 3306。如果您使用 daefault,则不需要写入数据库端口号(MySQL 的情况下为 3306)

回答by Roshan Bhumbra

In your PHP code you have set the incorrect port, this is what the code should be

在您的 PHP 代码中,您设置了不正确的端口,这就是代码应该是什么

<?php

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db     = 'test_db13';

The port in your code is set to 3360when it should be 3306, however as this is the default port, you don't need to specify.

代码中的端口设置为3360应为3306,但由于这是默认端口,因此无需指定。

回答by Ravi Ranjan

In most of cases it is data log problem. Follow the steps.

大多数情况下是数据日志问题。按照步骤。

i) Go to data folder of mysql. For xampp go to C:\xampp\mysql\data.

i) 转到 mysql 的数据文件夹。对于 xampp,请转到 C:\xampp\mysql\data。

ii) Look for log file name like ib_logfile0 and ib_logfile1.

ii) 查找日志文件名,如 ib_logfile0 和 ib_logfile1。

iii) Create backup and delete those files.

iii) 创建备份并删除这些文件。

iv) Restart apache and mysql.

iv) 重新启动 apache 和 mysql。

回答by Daniel Albus

For those who came here looking for the answer and didnt type 3306 wrong...If like myself, you have wasted hours with no luck searching for this answer, then possibly this may help.

对于那些来到这里寻找答案并且没有输入 3306 错误的人......如果像我一样,你已经浪费了几个小时而没有找到这个答案,那么这可能会有所帮助。

If you are seeing this: (HY000/2002): No connection could be made because the target machine actively refused it

如果你看到这个:(HY000/2002):无法建立连接,因为目标机器主动拒绝它

Then my understanding is that it cant connect for one of the following below. Now which..

然后我的理解是它无法连接以下之一。现在哪个..

1) is your wamp, mamp, etc icon GREEN? Either way, right-click the icon --> click tools --> test both the port used for Apache (typically 80) and for Mariadb (3307?). Should say 'It is correct' for both.

1) 你的 wamp、mamp 等图标是绿色的吗?无论哪种方式,右键单击图标 --> 单击工具 --> 测试用于 Apache(通常为 80)和用于 Mariadb(3307?)的端口。应该对两者都说“这是正确的”。

2) Error comes from a .php file. So, check your dbconnect.php.

2) 错误来自 .php 文件。因此,请检查您的 dbconnect.php。

<?php
$servername = "localhost";
$username = "your_username";
$password = "your_pw";
$dbname = "your_dbname";
$port = "3307";
?>

Is your setup correct? Does your user exist? Do they have rights? Does port match the tested port in 1)? Doesn't have to be 3307 and user can be root. You can also left click the green icon --> click MariaDB and view used port as shown in the image below. All good? Positive? ok!

你的设置正确吗?你的用户存在吗?他们有权利吗?端口是否与 1) 中的测试端口匹配?不必是 3307,用户可以是 root。您也可以左键单击绿色图标 --> 单击 MariaDB 并查看使用的端口,如下图所示。都好?积极的?好的!

enter image description here

在此处输入图片说明

3) Error comes when you login to phpmyadmin. So, check your my.ini.

3) 登录 phpmyadmin 时出现错误。因此,请检查您的 my.ini。

enter image description here

在此处输入图片说明

Open my.ini by left clicking the green icon --> click MariaDB -->

通过左键单击绿色图标打开 my.ini --> 单击 MariaDB -->

; The following options will be passed to all MariaDB clients
[client]
;password = your_password
port = 3307
socket = /tmp/mariadb.sock

; Here follows entries for some specific programs

; The MariaDB server
[wampmariadb64]
;skip-grant-tables
port = 3307
socket = /tmp/mariadb.sock

Make sure the ports match the port MariaDB is being testing on. Then finally..

确保端口与 MariaDB 正在测试的端口匹配。然后终于..

[mysqld]
port = 3307

At the bottom of my.ini, make sure this port matches as well.

在 my.ini 的底部,确保此端口也匹配。

4) 1-3 done? restart your WAMP and cross your fingers!

4) 1-3 完成了吗?重新启动您的 WAMP 并交叉手指!