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

15

16

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.

Making answered 24/2, 2014 at 12:26 Comment(6)
Do you have mysql runningHotchkiss
you forgot third parameter of mysql_connect. i.e. passowrdGratifying
What type of host is this? OS?Mercurochrome
@WayneWhitty > I am using Windows 7 as an OS.Making
check here. #19518066 and css-tricks.com/forums/topic/… it has soln what you needEshelman
Try $con = mysql_connect("localhost", "root", ""); mysql_select_db("users", $con);Automatize
M
9

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.

Misdo answered 24/2, 2014 at 12:27 Comment(15)
Thank you for the advice. But I did not put any password for my DB. What should I put as a syntax for that?Making
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.Misdo
Usually user access permission issues trigger a different error. Something like "Access denied for user 'root'@'localhost' (using password: NO) "Harlot
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.Making
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 3306Harlot
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.Making
4-check your firewall connection , 5-try uninstall and install your server. 6- post what you using , (windows, localhost server, mysql version, ...)Misdo
@Misdo you should get rid of your answer (the upper part), it is not helpful and has many upvotes..Harlot
@Making please take a look at my answer, it is not the solution but it seems the better way to address the issue.Harlot
I answered my question. Please refer to thisMaking
this solution is same i told you to check and set your password,Misdo
@Harlot it really helpful the upper part as he forgot to make password in his connection and its wrong.Misdo
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.Making
answer it here , not create other question with same issue . this question is discussed here.Misdo
Does it have the same issue? If yes, my apologies I posted in another question.Making
H
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

Harlot answered 24/2, 2014 at 12:50 Comment(0)
P
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.

Pylorus answered 22/3, 2017 at 15:58 Comment(0)
M
2

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");

Martinelli answered 23/7, 2021 at 13:33 Comment(0)
I
1

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

$conn = mysql_connect("localhost","root","");//for no password
Itinerary answered 15/2, 2017 at 15:22 Comment(1)
This answer is not related to the problemAzotic
T
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

Thickskinned answered 24/2, 2014 at 12:33 Comment(0)
L
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 ].

Legist answered 13/5, 2014 at 14:38 Comment(0)
A
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.

Acromion answered 29/10, 2017 at 12:52 Comment(0)
I
0

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

Incoming answered 1/6, 2020 at 20:54 Comment(0)
C
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.

Cuisine answered 5/7, 2020 at 5:7 Comment(0)
W
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.

Welbie answered 1/10, 2022 at 14:50 Comment(0)
B
0

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

Backup answered 30/4, 2023 at 8:47 Comment(0)
T
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.

Tamarisk answered 29/5, 2023 at 15:34 Comment(0)
B
0

I tried go to task manager and search for mysql and found program name mysql running every time I got error. I don't know it is from mysql workbench or something, but once I click end task on mysql, I able to run mysql server on Xampp.

Bluebonnet answered 26/7 at 4:23 Comment(0)
D
-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"));
Dimitri answered 24/2, 2014 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.