Jump to content
The Uniform Server Community
MordoSairon

Newbie question: error conecting to MySQL server

Recommended Posts

Hello!

 

I'm following Quick MySQL guide and I can't complete point 2: PHP Connect. I get this warning message:

 

Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'localhost' (10061) in W:\www\open_db.php on line 2

Error connecting to mysql

 

I've checked password is ok and apparently server is running (if I change dbhost for a non existent, I get a different error). My last idea is that maybe there's a port problem, because I change server listener port from 80 to 8080 due to initial problems with php. This is rather an intuition than a good idea xD but I don't know anything else to try. Also if the listener port is the problem, I don't know how to fix it ^^U

 

Thank you in advance for your help.

 

Best regards! ^^

 

 

[EDIT]

 

I tried to include my particular listener port like this:

 

$dbhost = 'localhost:8080'; //server name localhost or 127.0.0.1

 

Now I get this message:

 

Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server during query in W:\www\open_db.php on line 2

 

Fatal error: Maximum execution time of 30 seconds exceeded in W:\www\open_db.php on line 2

 

Now I really don't know what else can I do :S

 

[/EDIT]

Share this post


Link to post
Share on other sites

Hi there,

You only need to connect to localhost without any port. MySQL runs on port 3306 by default.

 

Please try:

 

$dbhost = "localhost";

 

If you are still having a problem.. find this code:

 

mysql_connect($dbhost, $dbuser, $dbpass);

 

and ensure it looks like this:

 

mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error());

 

This will show a better error - you may show us this so we can further determine what is happening.

Share this post


Link to post
Share on other sites

Hello!

 

I've change my code to see full description error and erased port 8080 from dbhost, but I don't see anything new:

 

Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'localhost' (10061) in W:\www\open_db.php on line 2

Can't connect to MySQL server on 'localhost' (10061)

 

Maybe it's a port problem? Should I change for another one or enable it someway?

 

Thank you very much!

 

Best regards,

 

 

[EDIT] I changed port to 3307, but I didn't get any results. I don't know if netstat command should show any status line for mysql listening port, but it doesn't show anything (even for 3306 before changing port). [/EDIT]

Share this post


Link to post
Share on other sites

According to php tutorial, once you have created a database, there are a few ways you can connect to it.

To connect from a PHP script, just put this in your file:

<?
 mysql_connect("DBSERVER", "DBUSERNAME", "DBPASSWORD");
 mysql_select_db("DBNAME");
 ?>

using the following substitutions for the bold terms as above, plus substituting DBPASSWORD with your own mysql password:

 

* Replace DBSERVER with the correct database servername for your site.

* Replace DBUSERNAME with your own mysql username.

* Replace DBNAME with your own mysql databasename.

 

The connection will be closed automatically when the script ends. To close the connection before, use the mysql_close() function:

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
// some code
mysql_close($con);
?>

Share this post


Link to post
Share on other sites

Hello!

 

I've change my code to see full description error and erased port 8080 from dbhost, but I don't see anything new:

Maybe it's a port problem? Should I change for another one or enable it someway?

 

Thank you very much!

 

Best regards,

[EDIT] I changed port to 3307, but I didn't get any results. I don't know if netstat command should show any status line for mysql listening port, but it doesn't show anything (even for 3306 before changing port). [/EDIT]

 

Instead of using "localhost", put in "127.0.0.1" for the dbhost.

 

See refs to Windows 7 and MySQL.

Share this post


Link to post
Share on other sites

Voda: thanks for your help, I'm following tutorial as well and I think mi code is correct:

 

This is my config_db.php:

<? // Database configuration file config_db.php
$dbhost = 'localhost'; //server name localhost or 127.0.0.1
$dbuser = 'root';	  //User name default root 
$dbpass = 'root';  //Password default root -- I will be using ''fred123''
?>

 

And this is open_db.php:

<? // Open database open_db.php
include_once $_SERVER['DOCUMENT_ROOT'] . "/config_db.php";

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
?>

 

I get this error when executing open_db.php. I think everything is correct, it's the same code in the tutorial (except the include_once, but that's not failing :S).

 

BobS: I've already tried that, I see no difference but that in the error message shows '127.0.0.1' instead of 'localhost' :S

 

See refs to Windows 7 and MySQL.

I'm running on Windows XP, I've already search this error in google but dind't find a solution.

 

Thank you very much for your help.

 

Best regards,

Share this post


Link to post
Share on other sites

I can’t see anything wrong with your code.

I assume the error message is similar to this:

Can't connect to MySQL server on 'localhost' (10061)

One cause of the above error your firewall may be blocking connection on port 3306.

 

Disconnect your PC from the Internets and disable your firewall.

Restart servers and see if you can connect to MySQL server.

If that works you need to add an exception in your firewall to allow MySQL connection.

 

Before connecting to Internet remember to restart your firewall

 

General notes:

I assume you are using an older version of Uniform Server because PHP short tags ( ?>) are working.

Your code will not run on newer version that run PHP 5.3.* always use full tags

(<?php ?>)

 

On Windows XP you can use either "127.0.0.1" or locahost for dbhost

 

All the best

Ric :)

Share this post


Link to post
Share on other sites

Hello!

 

Thank you very much for your help. My message error is exactly:

 

Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'localhost' (10061) in W:\www\open_db.php on line 4

Can't connect to MySQL server on 'localhost' (10061)

 

I've tried to disconnect from the internet and then shut down my firewall. After stop and restart server I see no difference (same error message). Anyway, I've already thought about the firewall an added an exception for mysql like this:

 

firewallu.png

 

I think like this there shouldn't be any problems of communication but who really knows...

 

Best regards,

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...