Jump to content

[SOLVED] Unable to connect to mysql server via localhost


NoUser2U
 Share

Recommended Posts

Dear All,I'm having trouble getting MySQL Community server 5.5.11 to work in combination with Apache 2.2.17 and PHP 5.3.6I'm runing Windows 7 Professional (32-bit).PHP and Apache both are working, but when i execute a simple php-script in which i try to connect to the database, like this:

<?php $con = mysql_connect("localhost","root","mypassword") or die(mysql_error); ?>

Then i get the error:Warning: mysql_connect() [function.mysql-connect]: [2002] Unable to connect because the target computer ac (trying to connect via tcp://localhost:3306) in C:\server\apache\htdocs\t.php on line 3Warning: mysql_connect() [function.mysql-connect]: Unable to connect because the target computer actively refused the connection. in C:\server\apache\htdocs\t.php on line 3Unable to connect because the target computer has refused the connection(i translated the error to english since it was given in the language of my country).Other information:- I have installed httpd-2.2.17-win32-x86-no_ssl.msi and php-5.3.6-Win32-VC9-x86.zip (threadsafe VC9 version). Both apache and PHP 5.3.6 work after configuring the httpd.conf. Apache is running as a windows service for all users.- In the php.ini i have set the extension_dir to extension_dir = "c:/server/php/ext" (i have php, apache and mysql all installed in maps with the corresponding names in a global map called 'server' on the C-drive. So the 'server'-map contains the maps 'mysql', 'php' and 'apache').- in the php.ini i have uncommented the extension for php_mysql.dll & php_mysqli.dll.Mysql is running as a service. When using mysql with the "MySQL Command Line Client" then there are no problems; i'm able to create databases and do everything else with it. I haven't touched the my.ini though and in it (under the [CLIENT] or [sERVER] sections), i see nothing about any ports or hosts or anything. The other my-xxx.ini files all have a port-specification, saying "port=3306".The password in the $con (see code) is correct inputted and i also tried for host using "127.0.0.1" and more alternatives, but nothing seems to help. I have really tried everything to solve this but no success unfortunately. So basically, i really don't know what to do now in order to fix this.Please, any help is appreciated.PS: All this is done after a fresh Windows 7 install. After the windows 7 install i had immediately disabled the windows firewall (and it currently still is disabled). After disabling the windows firewall i installed apache, php and mysql, exactly in thát order. I have no other software, anti-virus or so installed, but solely the previously mentioned 3 programs because i thought a fresh windows 7 install would solve the problem, but that doesn't seem to help unfortunately :).PPS: before, i was using WAMP with php 5.3.6 and i believe also the newest apache and mysql-versions, and that didn't give any problems. But because i wanted to learn more about the different configurations of i decided to deinstall WAMP and install every program seperately.

Edited by Donotknow
Link to comment
Share on other sites

Thank you for your quick reply justsomeguy.I tried with the localhost part as you suggested, but that still gives the same error. Telnetting port 3306 gave me the following (translated):A connection is trying to establish with 3306...Unable to connect to the host, on port 3306: Failed to connectFor this, i typed 'telnet localhost 3306' in a command prompt, i don't know if this is the correct way to use telnet though, since i don't have any experience with telnetting.

Edited by Donotknow
Link to comment
Share on other sites

Yeah that should be fine for telnet. If it says it failed to connect, that means that either MySQL is not listening (make sure the service is running, maybe restart it), or that the connection is being blocked by something like a firewall. Since you're going to localhost it shouldn't be an issue on the router, but I'm not positive about that.

Link to comment
Share on other sites

Thank you for your reply justsomeguy,Fortunately i managed to fix this problem. What i did was the following:I check the my.ini file again and saw a line called skip-networking. After commenting that line out (putting a # in front of it), everything seems to work normally, i do not get the error message and connection via localhost is made :). (When i was using WAMP i never looked at the my.ini (or configuration files) so i don't know whether this line was commented out too when using WAMP, XAMP or something similar. Can somebody please check this out for me? Thnx in advance)I also looked up what the skip-networking line exactly does. Seems it has something to do with TCP connection and it is recommended to keep this line UNCOMMENTED when using mysql on the same machine as the server. Doing a quick search gave me this (source: http://bugs.mysql.com/bug.php?id=234):Many people run mysql on the same machine as the web server, and thus don't need externalnetworking enabled at ALL. A service that is turned off is always more secure than aservice that's turned on.Besides, mysql comes by default with no root password, and a variety ofless-than-perfectly-secure privs. So, we should encourage more people to"--skip-networking".Now i'm running mysql on my computer, which has php and apache installed on and therefore, mysql is installed on the same machine as the webserver is. According to the above description, i should have the 'skip-networking' line enabled (i.e., not commented) due to security reasons, but doing so will make mysql unusable via localhost.So now i have the line commented out, does this mean other people are able to connect to my databases though (assuming they have proper login-details) ? (i'm sorry if this is a very basic question, but i have little networking-knowledge)Thank you anyway for your previous help justsomeguy! :)

Edited by Donotknow
Link to comment
Share on other sites

It might be accessible, unless something else is blocking it. To test that, open a browser on your web server and go here:https://www.grc.com/x/ne.dll?bh0bkyd2Click the proceed button, and you'll see a blue form on the next page. Enter 3306 into the text field and click User Specified Custom Port Probe. That site will scan your computer on that port to see if it's open.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...