[英]trouble connecting to MySql DB (PHP)
I have the following PHP code to connect to my db: 我有以下PHP代码可连接到我的数据库:
<?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.2i\www\checklogin.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.2i\www\checklogin.php on line 11
Fatal error: Maximum execution time of 30 seconds exceeded in
C:\Program Files (x86)\EasyPHP-5.3.2i\www\checklogin.php on line 11
I am able to add a db/tables via phpmyadmin but I can't connect using PHP. 我可以通过phpmyadmin添加数据库/表,但无法使用PHP进行连接。
Here is a screenshot of my phpmyadmin page: 这是我的phpmyadmin页面的屏幕截图:
What could be the problem?
可能是什么问题呢?
Check the following: 检查以下内容:
localhost
to 127.0.0.1
localhost
更改为127.0.0.1
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. 它将请求发送到
localhost:3306
,并且仅等待很长时间才能得到答复。 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) 它没有得到它,这意味着请求被阻止(防火墙)或被忽略(MySQL未运行和/或正在侦听其他端口)
If phpMyAdmin came with the MySQL install, then it could be that it was configured to use the appropriately different port 如果phpMyAdmin随MySQL安装一起提供,则可能是它已配置为使用适当的不同端口
ha i also had that issue now i fix it by changing port number eg "find.db.13344.hostedfind.com:3306". ha我现在也遇到了该问题,我通过更改端口号来解决它,例如“ find.db.13344.hostedfind.com:3306”。 before it my connection was trying to access the "find.db.13344.hostedfind.com:3307" so it gave me this 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) .
在此之前,我的连接尝试访问“ find.db.13344.hostedfind.com:3307”,因此它给了我这个错误警告:mysql_connect()[function.mysql-connect]:[2002]连接尝试失败,因为连接方没有(尝试通过tcp:// localhost:3306连接)。
I also have noticed this issue linux version of xampp with MySSQL 5.6.12-log running over network. 我还注意到此问题的xampp linux版本通过网络运行MySSQL 5.6.12-log。 Also, a question asked in MySQL forum ( http://forums.mysql.com/read.php?52,294772,294772 ) resembles this, but not satisfactorily answered.
另外,在MySQL论坛( http://forums.mysql.com/read.php?52,294772,294772 )中提出的问题与此类似,但未令人满意地回答。
What I have noticed is, it is probably due to Networking being unstable, or too many mysql connections from other developers in the network connecting to the database server at the same time - and keeping the port number busy for a fraction of time. 我注意到的是,这可能是由于网络不稳定,或者来自网络中其他开发人员的mysql连接过多,同时又连接到了数据库服务器-并使端口号繁忙了一段时间。 It is not often related to the configured connection time, nor maximum execution time, nor wrong username/password etc.
它通常与配置的连接时间,最长执行时间,错误的用户名/密码等无关。
Amazingly, the same script, same password, and same network allows the same server run properly on the second attempt - without having to do anything as a fix. 令人惊讶的是,相同的脚本,相同的密码和相同的网络允许相同的服务器在第二次尝试时正常运行-无需做任何修复。
Hence, it should be your momentarily unstable network fluctuation (or busy mysql connections), that goes on and off, and your script attempted to connect to the server when network was down for a very short time. 因此,应该是您暂时不稳定的网络波动(或繁忙的mysql连接)打开和关闭,并且您的脚本试图在很短的时间内关闭网络时连接到服务器。
That is a guessed answer from personal experience related to this error, may not be exact. 那是与该错误相关的个人经验的猜测答案,可能并不准确。
But if the error message is persistent, you should really need to do something. 但是,如果错误消息持续存在,则您确实需要执行某些操作。
Maybe your MySQL is not using TCP for localhost. 也许您的MySQL不在本地使用TCP。 Please try
请试试
$host='/tmp/mysql.sock';
or whatever socket it might be using. 或它可能正在使用的任何套接字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.