I know there are many questions like this, but i didn't find any solution in it.
Things i tried:-
checked firewall
restarted my PC and Apache server
restarted MYSQL
checked my code
Tried everything i know and found on internet
here's my code:-
<?php
$dbhost = 'localhost:3360';
$dbuser = 'root';
$dbpass = '';
$db = 'test_db13';
$conn = mysqli_connect($dbhost,$dbuser,'',$db);
if(! $conn){
die('Could not connect connect: ') ;
}
echo 'Successfully Connected';
$sql = 'Connected Successfully';
$retvalue = mysqli_query($sql);
if(! $retvalue){
die('Cannot connect to SQL: ');
}
echo 'DataBase test_db13 has successfully created';
mysqli_close($conn);
?>
I did set the password, but it still is showing me the error.
Here's the firewall picture:-
lastly XAMMP is running here's the proof
In your PHP code you have set the incorrect port, this is what the code should be
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db = 'test_db13';
The port in your code is set to 3360
when it should be 3306
, however as this is the default port, you don't need to specify.
If you are using a non default MySQL port number then do the following:
33747
then paste the following
$cfg['Servers'][$i]['port'] = 33747;
under the
/* Authentication type and info */
section.You have entered wrong port number 3360 instead of 3306. You dont need to write database port number if you are using daefault (3306 in case of MySQL)
I finally found a solution. I wasted hours just trying to figure what this issue was. I tried deleting all those files suggested above and it didn't work for me, I tried adding new inbound rules to firewall for myslqd.exe and it didn't work. The thing that is causing this error is MySQL port is misconfigured and the fix was really simple. if you are using Wamp or Xampp go to Main Folder/Bin/mysql/mysql/ and find a file named my.ini
Open my.ini file press CTRL + F and inside it search for PORT and change whatever value of port to - 3306 and save file;
After that go to Wamp icon at the bottom of the taskbar (system tray) and left click choose mysql option and click "test port 3306 used" and see if it gives you any error. you can also click use other port other than whatever is shown there and port 3306.
Goodluck. if it works comment.
In most of cases it is data log problem. Follow the steps.
i) Go to data folder of mysql. For xampp go to C:\xampp\mysql\data.
ii) Look for log file name like ib_logfile0 and ib_logfile1.
iii) Create backup and delete those files.
iv) Restart apache and mysql.
It is just a matter of changing the port user by mysql:3308 to 3306
Just right click on wamp-> select tools
under Port Used By mysql:3308 clcik on "use a port other than 3308
In the text port text box appear : type 3306 and save. Wait until the wampserver restarts and get green.
Now run your PHP code, It will work. That's it - Good Luck
For those who came here looking for the answer and didnt type 3306 wrong...If like myself, you have wasted hours with no luck searching for this answer, then possibly this may help.
If you are seeing this: (HY000/2002): No connection could be made because the target machine actively refused it
Then my understanding is that it cant connect for one of the following below. Now which..
1) is your wamp, mamp, etc icon GREEN? Either way, right-click the icon --> click tools --> test both the port used for Apache (typically 80) and for Mariadb (3307?). Should say 'It is correct' for both.
2) Error comes from a .php file. So, check your dbconnect.php.
<?php
$servername = "localhost";
$username = "your_username";
$password = "your_pw";
$dbname = "your_dbname";
$port = "3307";
?>
Is your setup correct? Does your user exist? Do they have rights? Does port match the tested port in 1)? Doesn't have to be 3307 and user can be root. You can also left click the green icon --> click MariaDB and view used port as shown in the image below. All good? Positive? ok!
3) Error comes when you login to phpmyadmin. So, check your my.ini.
Open my.ini by left clicking the green icon --> click MariaDB -->
; The following options will be passed to all MariaDB clients
[client]
;password = your_password
port = 3307
socket = /tmp/mariadb.sock
; Here follows entries for some specific programs
; The MariaDB server
[wampmariadb64]
;skip-grant-tables
port = 3307
socket = /tmp/mariadb.sock
Make sure the ports match the port MariaDB is being testing on. Then finally..
[mysqld]
port = 3307
At the bottom of my.ini, make sure this port matches as well.
4) 1-3 done? restart your WAMP and cross your fingers!
Source: Stackoverflow.com