Mysql_connect (): the connection could not be completed because the target machine actively refused it

I have this problem when I try to run a 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 the code for dbconnect.php:

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

I tried to use this format before, but I do not know what the problem is with this code.

Thanks in advance.

+8
php mysql
source share
8 answers

You have forgotten the password in your connection.

Try it.

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

Check the Documentation here .

  • You should switch to MYSQLI or PDO as you see that MYSQL already out of date .

  • The original mysql password is empty according to this information for mysql version 5.0. You must check your version.

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

EDIT:

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

means there is no error in your code, but you have a firewall that blocks your connection or your system is listening in another PORT.

: 1 - check that your default connection port is 3306.

2-try to connect using "127.0.0.1" instead of "localhost", maybe it is listening on "127.0.0.1".

3. It may also go wrong if the other end is listening on UDP rather than TCP.

4- check the connection to the firewall, if allowed.

+7
source share

This error is NOT password related.

Password errors display a response from the MySQL server. The object response does not come from the MySQL server, but from the client itself. He could not connect to the server because MACHINE (and not the server) refused it, so it most likely was a network problem or some other problem.

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

There may be further information in this answer: PHP -MySQL: the connection could not be completed

+3
source share

Make sure your mysql service is started by going to the Service: in Search Type: Services β†’ check mysql, start the service if it is not running.

+2
source share

Well, sometimes even using localhost without a port number can help. For example.

 $conn = mysql_connect("localhost","root","");//for no password 
+1
source share

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

If there is no password, leave the field empty as follows: mysql_connect("localhost","root" ,"");

NOTE. Use mysqli instead of mysql because mysql is deprecated

0
source share

I had the same problem. The problem was that MySql did not work on Xampp, so I had to run it to work.

So kindly ensure that MySql runs on your localhost. Go to the Xampp control panel [or another local host] and click the "Start / Run" button in Mysql].

0
source share

It looks like you forgot to add your password to your connection. In addition, you should no longer use mysql connect, as it officially depreciates . Use MYSQLi or PDO instead. An example of this might be:

 $connection= mysqli_connect("localhost", "root", "password", "database") or die(mysqli_error("error connecting to database")); 
-one
source share

Ok, I fixed this error! The encoding error is not related to your SQL connection. this is called port # for SQL. go to your XAmp or wamp tool from the active drop-down list and select β€œuse a port other thanβ€œ this # ”. Then enter 3306. This will solve the problem. Thank you.

-one
source share

All Articles