15

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,475
  • 4
  • 14
  • 13

14 Answers14

8

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 system 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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
echo_Me
  • 37,078
  • 5
  • 58
  • 78
  • 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
6

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: No connection could be made using mysql_* API

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
enapupe
  • 15,691
  • 3
  • 29
  • 45
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
  • 872
  • 13
  • 26
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");

Harshil Khamar
  • 186
  • 1
  • 6
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,463
  • 6
  • 41
  • 76
Abhinav
  • 8,028
  • 12
  • 48
  • 89
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.

Umair Rasool
  • 41
  • 1
  • 8
0

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

Anushri HV
  • 51
  • 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,130
  • 1
  • 13
  • 32
0

I was having the same error. I was using Xampp only for web server and MySQL8.0 running on local port (not the one included in Xampp). I found out that MySQL was not running.

On Windows.

Go to Services (search for services in Cortana) and check MySQL if it is running. In my case it was set to manual, right click on it-> go to properties set startup type to automatic.

Hope error will resolve it by then.

0

I just stop MYSQL80 service and then I started my xampp mysql and it works for my case

Alpha
  • 1,413
  • 3
  • 9
  • 23
0

No Default DBMS Default DBMS : none This means that none of the database managers (MariaDB and/or MySQL) use port 3306, and it is therefore imperative to specify the port number in connection requests since this is not the default port 3306. It is then essential that you choose which DBMS you want to use by default; to do this, use the built-in tools (Right-click -> Tools) to assign port 3306 (Use a port other than xxxx) to the DBMS (MariaDB or MySQL) you want to set as default.

kmnowak
  • 804
  • 1
  • 8
  • 23
-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
  • 204
  • 1
  • 4
  • 26