UPDATE:
I’ve solved the problem. I had MariaDB installed as well and was trying to use a database inside it. Got rid of it there, put it under MySQL, and got it connecting to the database, but then got the error message that the user didn’t have access. Ended up resetting the password for the user and now things are working.
I’m doing a little web development work on a side project and running into an issue trying to connect to the MariaDB database I’m using.
I’m using Windows 11, MariaDB 1.6, PHP 7, and Apache Web Server running under XAMPP. I’m trying to run the following code:
<?php
$dsn = "mysql:host=127.0.0.1;dbname=db_name;charset=utf8;port=3306";
$pdo = new PDO($dsn, "db_user", "db_password", [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
$stmt = $pdo->query("SELECT * FROM tblcontact");
$contacts = $stmt->fetchALL(PDO::FETCH_ASSOC);
print_r($contacts);
?>
Pretty straight forward code.
When I run it in the web browser I get the following error message:
Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it in C:\xampp\htdocs\PHPMVC\index.php:5 Stack trace: #0 C:\index.php(5): PDO->__construct(‘mysql:host=127….’, ‘db_user’, Object(SensitiveParameterValue), Array) #1 {main} thrown in C:\index.php on line 5
If I run the command line client for MariaDB I can log in, see the list of databases, change to the database I’m trying to connect to, run a query and get results returned to me.
I’ve checked the Services panel and the MariaDB service is running. Restarting the service seems to have no effect.
I’ve run netstat -a -b | findstr “3306” to see if I could find out if there were any other services running on port 3306. It doesn’t show any services running on port 3306. That would seem to be the reason for the error message I’m getting above.
So, how do I get PHP to recognize that MariaDB is running, and how do I get it running on port 3306?
If you’ve got a clue, my Hordeling, or know someone who has a clue, leave a comment and let me know.
If you’d like to support my efforts, why not buy me a chocolate chip cookie through my Ko-Fi page? https://ko-fi.com/jhusum
Something is weird with the troubleshooting here… Since 3306 is the default port for MariaDB, and your command line client works, it must be running on that port (unless you are specifying a different port when you run the client?). But then your netstat command didn’t show anything running on 3306, which seems to conflict with the previous assertion. So I’m unsure.
The only other thing I would suggest is to check the firewall software and/or temporarily disable it (perhaps not while exposed to internet).
I’ve turned off the firewall to test and still not getting anything.
Since I have MySQL installed too (which also runs on port 3306) I turned it on as a service. The the error message changes to
Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user ‘db_user’@’localhost’ (using password: YES) in C:\xampp\htdocs\PHPMVC\index.php:5 Stack trace: #0 C:\index.php(5): PDO->__construct(‘mysql:host=loca…’, ‘db_user’, Object(SensitiveParameterValue), Array) #1 {main} thrown in C:\xampp\htdocs\PHPMVC\index.php on line 5
Does the same netstat command show MySQL when it’s running on 3306?
Oddly enough, running netstat when I turn on MySQL through the XAMPP control panel finds port 3306 but simply says it is listening, but doesn’t show mysqld (as expected) associated with it.
TCP 0.0.0.0:3306 JOEBOB:0 LISTENING
TCP [::]:3306 JOEBOB:0 LISTENING
Pingback: Back in the programming saddle – James Husum – Writer