Daniel On The Web Posted August 30, 2016 Share Posted August 30, 2016 <?php $serv = "localhost:81"; $user = "root"; $password = ""; try { $con = new PDO("mysql:host=$serv;dbname=test", $user, $password); $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Successfully connected."; } catch (PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?> Here I'm trying to use PDO to connect to my SQL database "test". However, I receive this error each time I connect: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it. Am I connecting with the wrong server, user or password? Because I fail to see here the mistake. Link to comment Share on other sites More sharing options...
Ingolme Posted August 30, 2016 Share Posted August 30, 2016 Maybe the port number is wrong, or you haven't started up the MySQL process on your local server. Link to comment Share on other sites More sharing options...
Daniel On The Web Posted August 30, 2016 Author Share Posted August 30, 2016 I used Port 81 because I had to change it when preparing Apache. And the MySQL process was active. Link to comment Share on other sites More sharing options...
Ingolme Posted August 30, 2016 Share Posted August 30, 2016 Your PHP code is correct. The issue most likely has something to do with your server setup. I don't know your MySQL server's username, password or port number (and it wouldn't be smart to post the user and password on a public forum), but the issue could be that one of those is wrong. Link to comment Share on other sites More sharing options...
dsonesuk Posted August 30, 2016 Share Posted August 30, 2016 The MYSQL uses a different port, the default usually 3306, so do you really need :80 on the end? MYSQL will listen to 3306 on localhost. Link to comment Share on other sites More sharing options...
Daniel On The Web Posted August 30, 2016 Author Share Posted August 30, 2016 (edited) I don't know your MySQL server's username, password or port number (and it wouldn't be smart to post the user and password on a public forum), but the issue could be that one of those is wrong. Of course. I know that I shouldn't post such kind of information. However, I set up an user, a host and a password and wrote the same information in the PHP and the code is still throwing the error. Edited August 30, 2016 by Daniel On The Web Link to comment Share on other sites More sharing options...
dsonesuk Posted August 30, 2016 Share Posted August 30, 2016 MYSQL DOES NOT USE SAME PORT AS WEB SERVER, so using localhost:80 WON'T work, default port for MYSQL database server is usually 3306, if you use just localhost, by default it will listen for port 3306 for MYSQLl server connection, using localhost:3306 will work also because it pointing to same port, but! localhost:80 WILL NOT! Link to comment Share on other sites More sharing options...
Daniel On The Web Posted August 30, 2016 Author Share Posted August 30, 2016 MYSQL DOES NOT USE SAME PORT AS WEB SERVER, so using localhost:80 WON'T work, default port for MYSQL database server is usually 3306, if you use just localhost, by default it will listen for port 3306 for MYSQLl server connection, using localhost:3306 will work also because it pointing to same port, but! localhost:80 WILL NOT! Both "localhost" and "localhost:3306" didn't work as servers. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 30, 2016 Share Posted August 30, 2016 You installed the MySQL server on the local machine, right? Did you configure it? There's not much information to go on here. Link to comment Share on other sites More sharing options...
Daniel On The Web Posted August 30, 2016 Author Share Posted August 30, 2016 Yes, I configure it but it doesn't work as it should. I admit that it's weird. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 30, 2016 Share Posted August 30, 2016 Check that your firewall is allowing traffic on those ports. Link to comment Share on other sites More sharing options...
Daniel On The Web Posted August 30, 2016 Author Share Posted August 30, 2016 Checked. It wasn't the firewall. Strange. Could I have to change some configuration file? Link to comment Share on other sites More sharing options...
dsonesuk Posted August 30, 2016 Share Posted August 30, 2016 (edited) Both "localhost" and "localhost:3306" didn't work as servers. Yes! BUT did actually check my.ini file or phpinfo() to identify which port mysql is actually using. Edited August 30, 2016 by dsonesuk Link to comment Share on other sites More sharing options...
Daniel On The Web Posted August 30, 2016 Author Share Posted August 30, 2016 Yes! BUT did actually check mysql.ini file or phpinfo() to identify which port mysql is actually using. I checked the port it is using and it's 3306. But I still find the message that says me that the connection failed. Link to comment Share on other sites More sharing options...
davej Posted August 30, 2016 Share Posted August 30, 2016 I use... $svr = "mysql:host=localhost;dbname=mydbname"; try { $pdo = new PDO($svr, $usr, $pwd); Make sure you turned on the pdo mysql extension in php.ini. Seems odd that you don't have a root password but I guess you've tested that in the mysql console. Link to comment Share on other sites More sharing options...
Ingolme Posted August 30, 2016 Share Posted August 30, 2016 It's clear PDO's MySQL extension is active and enabled because it's attempting to connect to the MySQL server. Perhaps you should install a server package like XAMPP on your computer for testing, it takes care of setting up the server properly for you. Installing Apache, MySQL and PHP separately is more prone to human error. Link to comment Share on other sites More sharing options...
Daniel On The Web Posted August 30, 2016 Author Share Posted August 30, 2016 I use... $svr = "mysql:host=localhost;dbname=mydbname"; try { $pdo = new PDO($svr, $usr, $pwd); Make sure you turned on the pdo mysql extension in php.ini. Seems odd that you don't have a root password but I guess you've tested that in the mysql console. Like this?: extension=php_pdo_mysql.dll Because that's how it appears on my php.ini. And it's not commented. It's clear PDO's MySQL extension is active and enabled because it's attempting to connect to the MySQL server. Perhaps you should install a server package like XAMPP on your computer for testing, it takes care of setting up the server properly for you. Installing Apache, MySQL and PHP separately is more prone to human error. I'm already using it. Link to comment Share on other sites More sharing options...
davej Posted August 30, 2016 Share Posted August 30, 2016 Ignoring PDO for a moment, does mysqli work? <?php error_reporting(E_ALL); ini_set('display_errors','On'); echo 'Php is running...'; echo '<br/>Connecting to MySQL... '; $dbcon = mysqli_connect('localhost','root',''); $dbname = 'test'; if (!$dbcon){ echo '<br/>Error: Unable to connect to MySQL.'; }else if(!mysqli_select_db($dbcon,$dbname)){ echo '<br/>Error: Unable to open '.$dbname.' database.'; }else{ echo '<br/>Connected to '.$dbname.' database'; } ?> I have my Apache server running on port 8080 and mysql on the default, which is 3306 and I use the above (except I have a root password). Link to comment Share on other sites More sharing options...
Daniel On The Web Posted August 30, 2016 Author Share Posted August 30, 2016 Ignoring PDO for a moment, does mysqli work? <?php error_reporting(E_ALL); ini_set('display_errors','On'); echo 'Php is running...'; echo '<br/>Connecting to MySQL... '; $dbcon = mysqli_connect('localhost','root',''); $dbname = 'test'; if (!$dbcon){ echo '<br/>Error: Unable to connect to MySQL.'; }else if(!mysqli_select_db($dbcon,$dbname)){ echo '<br/>Error: Unable to open '.$dbname.' database.'; }else{ echo '<br/>Connected to '.$dbname.' database'; } ?> I have my Apache server running on port 8080 and mysql on the default, which is 3306 and I use the above (except I have a root password). I tried with MySQLi but got the same error. Actually I don't care about using PDO or MySQLi. I'm choosing what works best. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 30, 2016 Share Posted August 30, 2016 What does the error message say now? I assume the error message changed when you switched ports? Link to comment Share on other sites More sharing options...
davej Posted August 30, 2016 Share Posted August 30, 2016 Also, is MySQL working fine on the console? And are you on a Mac or a pc? Link to comment Share on other sites More sharing options...
Daniel On The Web Posted August 31, 2016 Author Share Posted August 31, 2016 (edited) With MySQLi: No connection could be made because the target machine actively refused it. With PDO: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it. So they are the same. The console works well. The problem is connecting to the databases using PHP. And I'm using a PC. Edited August 31, 2016 by Daniel On The Web Link to comment Share on other sites More sharing options...
dsonesuk Posted August 31, 2016 Share Posted August 31, 2016 In phpmyadmin select test db ( make sure it is exactly as 'test' not 'Test' or 'test '), select permissions tab, and tell us what it says. Link to comment Share on other sites More sharing options...
Daniel On The Web Posted August 31, 2016 Author Share Posted August 31, 2016 In phpmyadmin select test db ( make sure it is exactly as 'test' not 'Test' or 'test '), select permissions tab, and tell us what it says. It appears to me as "Privileges", but I think it's the same. The user I tried to use to connect to the database is: User name: root Host name: localhost Type: global Privileges: ALL PRIVILEGES Grant: Yes Link to comment Share on other sites More sharing options...
dsonesuk Posted August 31, 2016 Share Posted August 31, 2016 Sorry! I meant privileges, select Home button at very top above database listing, you should see tab 'Users' select that, tell us what it says. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now