# PHP can't connect to MySQL



## Maelstorm (Jun 24, 2018)

I am running FreeBSD 11.1, PHP 7.2, Apache 2.4, and some version of MySQL.  The problem is that when I try to access the test page from the web, I get an error that says "Failed to connect to database: could not find driver."  I made sure that pdo_mysql was installed, and it shows up on the list when I do `php -m` as shown below (I formatted the lines horizontally so it wouldn't be so long).


```
strata:/usr/local/etc 1162 ### ->php -m
[PHP Modules]
Core ctype date dom filter hash iconv json libxml mysqlnd pcre PDO pdo_mysql pdo_sqlite Phar posix Reflection
session SimpleXML SPL sqlite3 standard tokenizer xml xmlreader xmlwriter Zend OPcache

[Zend Modules]
Zend OPcache
```

The only clues that I have are as follows:

phpinfo() is telling me that it is using sqlite3 from the web.
php -i says it has both pdo_sqlite3 and pdo_mysql from the command line.
It's not the program, because it works correctly under Windows 10.
phpinfo() on Windows from the web says it's using pdo_mysql.
Here's the relevant info from php -i.


```
PDO

PDO support => enabled
PDO drivers => mysql, sqlite

pdo_mysql

PDO Driver for MySQL => enabled
Client API version => mysqlnd 5.0.12-dev - 20150407 - $Id: 38fea24f2847fa7519001be390c98ae0acafe387 $

Directive => Local Value => Master Value
pdo_mysql.default_socket => no value => no value

pdo_sqlite

PDO Driver for SQLite 3.x => enabled
SQLite Library => 3.21.0
```

Here is a partial screenshot of what phpinfo from the web looks like:




At this point, I have no idea what the problem is.  So I am calling Uncle on this one.

So does anyone have any idea as to what I should look at?


----------



## ShelLuser (Jun 24, 2018)

A vague story, I get the impression you're not giving us enough info. First of all: what is /usr/local/etc 1162? If that's your regular etc directory then I can't help but wonder if that isn't asking for problems; normally there are no spaces within the path on FreeBSD.

Anyway, solely going from the screenshots my conclusion is simple: your web environment does not provide support for MySQL.

So: anything relevant in /usr/local/etc/php.conf or /usr/local/etc/php?

Or: `pkg info -x mysql`, does that even list databases/php72-mysqli?  and/or databases/php72-pdo_mysql?


----------



## Maelstorm (Jun 24, 2018)

ShelLuser said:


> A vague story, I get the impression you're not giving us enough info. First of all: what is /usr/local/etc 1162? If that's your regular etc directory then I can't help but wonder if that isn't asking for problems; normally there are no spaces within the path on FreeBSD.



Actually, the 1162 is the command number in the shell history buffer.  The directory *IS* /usr/local/etc.



ShelLuser said:


> Anyway, solely going from the screenshots my conclusion is simple: your web environment does not provide support for MySQL.
> 
> So: anything relevant in /usr/local/etc/php.conf or /usr/local/etc/php?
> 
> Or: `pkg info -x mysql`, does that even list databases/php72-mysqli?  and/or databases/php72-pdo_mysql?



Be that as it may, I finally did get it working.  I had  the package php72-pdo_mysql-7.2.6 installed on the system.  What I didn't do was restart Apache after the install.  Apparently, Apache keeps php in memory ready to go.  So if changes are made to php, then you have to restart it.

Then I spent a couple of hours rolling around in the mud with another problem.  The method that I was using to access the MySQL database server on Windows is a socket, which was unknown to me.  So if you use a socket, the dsn reads like this:

*mysql:hosthame=localhost;charset=utf8*

But, if you try to access via an IP address, then it looks like this:

*mysql:host=127.0.0.1;port=3306;charset=utf8*

So I actually have to fix my code to reflect this.  But the socket option, I moved it from the default location.  So I changed it in php.ini to point to the current location, now it works.  I'm still missing some modules that I have to install, but that's not a problem now that I know what I am looking for.

Thanks for the reply.


----------

