连接到 MySql DB (PHP) 时遇到问题

2022-01-05 00:00:00 sql php mysql phpmyadmin

我有以下 PHP 代码可以连接到我的数据库:

I have the following PHP code to connect to my db:

<?php
ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");

?>

但是,我收到以下错误:

However, I get the following error:

Warning: mysql_connect() [function.mysql-connect]: [2002] A 
connection attempt failed because the connected party did not (trying 
to connect via tcp://localhost:3306) in C:Program Files (x86)EasyPHP-
5.3.2iwwwchecklogin.php on line 11

Warning: mysql_connect() [function.mysql-connect]: A connection 
attempt failed because the connected party did not properly respond 
after a period of time, or established connection failed because 
connected host has failed to respond. in C:Program Files (x86)EasyPHP-
5.3.2iwwwchecklogin.php on line 11

Fatal error: Maximum execution time of 30 seconds exceeded in 
C:Program Files (x86)EasyPHP-5.3.2iwwwchecklogin.php on line 11

我可以通过 phpmyadmin 添加数据库/表,但我无法使用 PHP 连接.

I am able to add a db/tables via phpmyadmin but I can't connect using PHP.

这是我的 phpmyadmin 页面的屏幕截图:可能是什么问题?

Here is a screenshot of my phpmyadmin page: What could be the problem?

推荐答案

检查以下内容:

  1. MySQL 是否正在运行?
  2. 是否有任何防火墙会阻止您的计算机接受端口 3306 上的 MySQL 连接?
  3. MySQL 是在侦听端口 3306,还是更改为非标准的端口?
  4. 很奇怪,但尝试从 localhost 更改为 127.0.0.1

基本上,您收到的错误意味着它无法连接到服务器.它向 localhost:3306 发送请求,并且只等待这么长时间才能得到回复.它没有得到它,这意味着请求被阻止(防火墙)或被忽略(MySQL 没有运行和/或正在侦听不同的端口)

Basically, the errors you're getting mean that it cannot connect to the server. It sends request to localhost:3306, and only waits so long for a reply. it's not getting it, which means the request is either blocked (firewall) or ignored (MySQL is not running and/or is listening on a different port)

如果 phpMyAdmin 随 MySQL 安装一起提供,那么它可能被配置为使用适当的不同端口

If phpMyAdmin came with the MySQL install, then it could be that it was configured to use the appropriately different port

相关文章