14

I have this problem when I am trying to run my PHP MySQL script. When I try to run my .php file this is what I get.

mysql_connect(): No connection could be made because the target machine actively refused it

This is code for dbconnect.php:

<?php
mysql_connect("localhost","root");
mysql_select_db("users");
?>

I tried to use this format before but I don't know what seems to be the problem with this code.

Thank you in advance.

user3335903
  • 1,355
  • 4
  • 13
  • 13

11 Answers11

9

You forgot password in your connection.

Try this.

    mysql_connect("localhost","root" ,"password here");

Check Documentation here.

  • You should switch to MYSQLI or PDO as you see that MYSQL is already deprecated.

  • The initial Mysql password is blank according to this info for mysql ver 5.0. you should check your version.

      mysql_connect("localhost","root" ,""); // will connect.
    

EDIT:

No connection could be made because the target machine actively refused it

means that no error in your code , but either you have firewall which blocks your connection or your sistem is listening in different PORT.

to do: 1-verify your connecting port default is 3306.

2-try connect with use "127.0.0.1" instead of "localhost" this maybe it listening on "127.0.0.1".

3-It could also go wrong if the other end is listening on UDP, not TCP.

4- verify your firewall connection if its permitted.

echo_Me
  • 36,552
  • 5
  • 55
  • 77
  • 2
    Thank you for the advice. But I did not put any password for my DB. What should I put as a syntax for that? – user3335903 Feb 24 '14 at 12:34
  • i dont know what password is by default , but you can go to phpmyadmin and set new password for specified user. maybe is empty password. try use `""` ; , but you keep in mind to use a password. – echo_Me Feb 24 '14 at 12:38
  • Usually user access permission issues trigger a different error. Something like "Access denied for user 'root'@'localhost' (using password: NO) " – enapupe Feb 24 '14 at 12:40
  • I put on a password, but when I tried to refresh the localhost/phpmyadmin it looks like it crashed, so I have to reinstall it. – user3335903 Feb 24 '14 at 12:42
  • 2
    Is it windows? What version is the mysql server? Have you tried replacing localhost with 127.0.0.1? Is the port correct? Default is 3306 – enapupe Feb 24 '14 at 12:46
  • Yeah I have tried that. But when I posted a password for my DB. I got this message `#2002 - No connection could be made because the target machine actively refused it.` – user3335903 Feb 24 '14 at 13:03
  • 4-check your firewall connection , 5-try uninstall and install your server. 6- post what you using , (windows, localhost server, mysql version, ...) – echo_Me Feb 24 '14 at 13:35
  • @echo_Me you should get rid of your answer (the upper part), it is not helpful and has many upvotes.. – enapupe Feb 24 '14 at 13:47
  • @user3335903 please take a look at my answer, it is not the solution but it seems the better way to address the issue. – enapupe Feb 24 '14 at 13:48
  • 1
    I answered my question. Please refer to [this](http://stackoverflow.com/questions/21988792/2002-no-connection-could-be-made-because-the-target-machine-actively-refused) – user3335903 Feb 24 '14 at 13:50
  • this solution is same i told you to check and set your password, – echo_Me Feb 24 '14 at 13:55
  • @enapupe it really helpful the upper part as he forgot to make password in his connection and its wrong. – echo_Me Feb 24 '14 at 13:57
  • Is there something wrong if I try to help others as well? By answering my own question? I don't see anything wrong with that. – user3335903 Feb 24 '14 at 14:04
  • answer it here , not create other question with same issue . this question is discussed here. – echo_Me Feb 24 '14 at 14:05
  • Does it have the same issue? If yes, my apologies I posted in another question. – user3335903 Feb 24 '14 at 14:11
5

This error is NOT password related.

Password related errors display a response from MySQL server. The subject response is NOT coming from MySQL server, but from the client itself. It could not connect to the server because the MACHINE (not the server) refused it, so, it was more likely to be a network issue or some other thing.

Try connecting to 127.0.0.1. Also check the port you are connecting to and which port is mysql running. Check for firewalls blocking connections. Are you sure is MySQL Server running?

This answer may have additional info: PHP -MySQL:No connection could be made

Community
  • 1
  • 1
enapupe
  • 13,199
  • 3
  • 28
  • 43
4

Make sure your mysql Service is running, by going to check in Services: in Search type: Services -> check mysql, start service if it's not running.

1

Well sometimes even using localhost without any port number can help. E.g.

$conn = mysql_connect("localhost","root","");//for no password
Saksham Khurana
  • 672
  • 9
  • 24
1

Just check your Server Port and do connection as follows:

  • If your port no. is 3308:

  • Username = root,

  • Password = "",

  • database name = form (OPTIONAL),

  • then,

    $con=mysqli_connect("localhost:3308","root","","form");

0

It should be mysql_connect("localhost","root" ,"your password");

If no password, then leave it blank like this: mysql_connect("localhost","root" ,"");

NOTE: Use Mysqli instead of MySql because mysql is deprecated

Hkachhia
  • 4,275
  • 5
  • 38
  • 74
Abhinav
  • 7,842
  • 10
  • 47
  • 86
0

I had the same problem. The issue was that MySql was not running on Xampp so I had to run it for everything to work.

So kindly ensure MySql is running on your localhost. Go to Xampp [or other localhost] control panel and click start/run under Mysql ].

0

Okay I fixed this error! there is no coding mistakes your SQL connection. it's caused by port# for SQL. go to your XAmp or wamp tools from active drop down and select "use a port other than 'given #' ". Then enter 3306 there. It will fix the issue. Thanks.

0

Changing "mysql://username:password@/databasename" to "mysql://username:password@localhost/databasename" fixed the issue for me.

Anushri HV
  • 31
  • 3
0

I had the same problem.

There are two database engines - MySQL DB and Maria DB. In your system, the database engine which is working may not over the default port. So check the active database engine and its port. Connect the database using:

mysqli_connect(host, username, password, dbname, port, socket)

for example:

mysqli_connect("localhost", "username", "password", "dbname", "3307")

The port number is available in the my.ini file or in the status menu of wampserver.

Hoppo
  • 1,070
  • 1
  • 13
  • 32
-2

It looks like you forgot to add your password to your connection. Also, you should not use mysql connect anymore as it is officially depreciated. Use MYSQLi or PDO instead. An example of this would be:

$connection= mysqli_connect("localhost", "root", "password", "database")
or die(mysqli_error("error connecting to database"));
d.abyss
  • 194
  • 1
  • 4
  • 26