R_14_14 Posted September 28, 2010 Share Posted September 28, 2010 Hi, I can't seem to connect to MySQL via PHP. I get the following error message when I try: Warning: 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)\Apache Software Foundation\Apache2.2\htdocs\mysqlconnect.php on line 3 Warning: 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)\Apache Software Foundation\Apache2.2\htdocs\mysqlconnect.php on line 3 Fatal error: Maximum execution time of 30 seconds exceeded in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\mysqlconnect.php on line 3 I am running windows 7 with Apache 2.2, PHP 5.3 and MySQL server 5.1. In task manager, ApacheMonitor is running under processes and Apache is running under services. MySQL is listed under services, but is stopped. When I try to access MySQL via the command line I get the following error message: ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost' (10061) So it would seem I have set up Apache/MySQL wrong. Could anyone please tell me what I should check for? P.S. when I first installed MySQL I could access it via the command line fine, but I still couldn't connect via PHP. Not being able to access it via command line is a recent development. Quote Link to comment https://forums.phpfreaks.com/topic/214602-error-2003-hy000-cant-connect-to-mysql-server-on-localhost-10061/ Share on other sites More sharing options...
mikosiko Posted September 28, 2010 Share Posted September 28, 2010 MySQL is listed under services, but is stopped have you tried to start the service?... what happens if you do? Quote Link to comment https://forums.phpfreaks.com/topic/214602-error-2003-hy000-cant-connect-to-mysql-server-on-localhost-10061/#findComment-1116746 Share on other sites More sharing options...
R_14_14 Posted September 28, 2010 Author Share Posted September 28, 2010 I couldn't at first start the service from task manager, even though I have admin rights, etc., but I've now got MySQL running and can access it from command line. However, I still get the first error message when trying to connect via PHP. Any ideas what could be causing this? My PHP script is as follows: <?php $link = mysql_connect ('localhost', 'root', 'PASSWORD'); if (!$link) { die('Could not connect: ' . mysql_error()); } else { print 'successful'; } mysql_close($link); ?> The only thing I wasn't sure about was the username 'root', when installing MySQL it never asked me for a username. Could this or something else in the PHP script be the issue? or is there something wrong with the way I have set up PHP/MySQL/Apache? Quote Link to comment https://forums.phpfreaks.com/topic/214602-error-2003-hy000-cant-connect-to-mysql-server-on-localhost-10061/#findComment-1116817 Share on other sites More sharing options...
mikosiko Posted September 29, 2010 Share Posted September 29, 2010 run this and look if it shows MYSQL information <?php phpinfo(); ?> if you don't find an entry for MYSQL check your php.ini to see if the mysql extension is enabled or not... also check if effectively MYSQL is listen in port 3306 (look your mysql ini file)... also look for your mysql .err file and look what it say... and another idea... if you have a firewall look if the port 3306 is allowed Quote Link to comment https://forums.phpfreaks.com/topic/214602-error-2003-hy000-cant-connect-to-mysql-server-on-localhost-10061/#findComment-1116934 Share on other sites More sharing options...
R_14_14 Posted September 29, 2010 Author Share Posted September 29, 2010 OK thanks for the suggestions so far. No luck though: I have run phpinfo(); there is indeed a MySQL section. I have attached the output that phpinfo gives, I know no-one is eager to trawl through it looking for the problem, but if anyone has an idea please do look at it, it would be a massive help! MySQL is indeed listening at port 3306; although I am just trying to connect to MySQL locally, from the same computer that has the database on it (eventually I just want to test a website that I am designing), so I dont know, does this still apply? Also I have tried to connect with my firewall completely turned off, just in case, but still get same error message. I couldn't find a mysql.err file in the MySQL installation directory, however I could find a mysqld_error.h file. It lists many errors, most of them I believe are due to me entering invalid syntax from the command line, when I was still playing around, but I have attached it in case it's useful. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/214602-error-2003-hy000-cant-connect-to-mysql-server-on-localhost-10061/#findComment-1117103 Share on other sites More sharing options...
R_14_14 Posted September 29, 2010 Author Share Posted September 29, 2010 O and I have also run the following from the command line: CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'passwrd'; and changed the user to 'testuser' and password to 'passwrd' etc. in my PHP script to make absolutely sure I am using the right user, host and password - still get same error message. Quote Link to comment https://forums.phpfreaks.com/topic/214602-error-2003-hy000-cant-connect-to-mysql-server-on-localhost-10061/#findComment-1117104 Share on other sites More sharing options...
mikosiko Posted September 29, 2010 Share Posted September 29, 2010 I was not referring to mysql.err I said mysql .err file (there is an space there )... normally the .err file is named <your computer name>.err and is located under your mysql data directory. I did check your phpinfo() output and effectively mysql and mysqli are enabled... also I did notice that you have a 64b machine (AMD64)... did you installed the 64b mysql version or the 32b version?.... that could be a probably cause or your errors, other than that I have read several other users reporting the same error and pointing to a faulty mysql installation ... and re-install it from scratch has solved the issue... you can try checking the mysql version first (32 or 63b) and if that doesn't work maybe a re-install will help hope this help Quote Link to comment https://forums.phpfreaks.com/topic/214602-error-2003-hy000-cant-connect-to-mysql-server-on-localhost-10061/#findComment-1117177 Share on other sites More sharing options...
R_14_14 Posted September 29, 2010 Author Share Posted September 29, 2010 Thanks everyone for your help. In the end I solved the problem by uninstalling everything and re-installing using WAMP. much easier..... Quote Link to comment https://forums.phpfreaks.com/topic/214602-error-2003-hy000-cant-connect-to-mysql-server-on-localhost-10061/#findComment-1117230 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.