75

I am trying to setup webserver with PHP 7 RC3 + Nginx on Ubuntu 14.04 (for test purposes).

I installed Ubuntu in Vagrant using ubuntu/trusty64 and PHP 7 RC 3 from Ondřej Surý (https://launchpad.net/~ondrej/+archive/ubuntu/php-7.0).

I can not find the way to install MySQL PDO (PHP sees PDO class but not anything related to MySQL, like PDO::MYSQL_ATTR_DIRECT_QUERY etc.)

Looks like there is no lib php7.0-mysql (by analogy with standard php5-mysqlnd and php7.0-fpm etc. from Ondřej)

Section PDO in phpinfo():

PDO support      enabled
PDO drivers      no value

How can I get it?

SmxCde
  • 5,053
  • 7
  • 25
  • 45
  • 3
    You say Ubuntu 12.04, but then that you're using `ubuntu/trusty64` which is 14.04. Assuming that you are in fact using Trusty (after all, Ondřej hasn't produced packages for Precise) then his [`php-modules`](https://launchpad.net/~ondrej/+archive/ubuntu/php-7.0/+sourcepub/5374980/+listing-archive-extra) package will provide `php-mysql`. – eggyal Sep 23 '15 at 00:00
  • Yes, you are absolutely right! (version is fixed). And your suggestion with module name works. Thanks! If you add it as answer I will mark it as correct. – SmxCde Sep 23 '15 at 00:08
  • My mistake - i was searching like this `apt-cache search php pdo` and module `php-mysql` in this case is not shown. – SmxCde Sep 23 '15 at 00:13
  • If it's anything like standard Ubuntu/Debian you can also install `php5-mysqlnd` to use the [mysql native driver](http://php.net/manual/en/mysqli.overview.php#mysqli.overview.mysqlnd), which according to that link is "strongly encouraged", even if the Ubuntu devs don't use it by default. – Mike Sep 23 '15 at 01:10
  • 1
    Mike, `php5-mysqlnd` does not work for me cause I install `PHP 7`. – Pavel Sep 23 '15 at 03:28
  • For PHP 7 on CentOS 7, mysqlnd package name is php70w-mysqlnd (big advantage over regular mysql driver is support of returning the correct data types [e.g. int as int, not as string]). – George Kagan Jul 20 '16 at 06:49

13 Answers13

141

For thoses running Linux with apache2 you need to install php-mysql

apt-get install php-mysql

or if you are running ubuntu 16.04 or higher just running the following command will be enought, no need to edit your php.ini file

apt-get install php7.2-mysql

If you are running ubuntu 15.10 or below:

Edit your php.ini file, it's located at /etc/php/[version]/apache2/php.ini and search for pdo_mysql you might found something like this

;extension=pdo_mysql.so

Change it to this

extension=pdo_mysql.so

Save the file and restart apache

service apache2 restart

Check that it's available in your phpinfo()

Angel115
  • 2,135
  • 1
  • 17
  • 20
  • 3
    Just a general FYI, I would **highly** recommend using [MySQLND](http://php.net/manual/en/intro.mysqlnd.php)(`php-mysqlnd`) instead of the older `php-mysql` package – Machavity Sep 23 '16 at 13:06
  • Not everyone has the luxury of choosing what library the customer app is already using, and not everyone can just "apt-get" – dagelf Mar 01 '17 at 07:12
  • Note: "service restart apache2" should read "service apache2 restart". For Ubuntu 16.04 I used only "sudo apt-get install php7.0-mysql" and "sudo service apache2 restart". – Anthony Scaife Oct 01 '17 at 20:13
  • I had to install php-mysql again. Seems some update has been made in that repository. – Rodolfo Velasco Mar 06 '18 at 17:09
  • 1
    Works well with PHP 7.2 as `apt-get install php7.2-mysql`. – ryryan Jul 25 '18 at 12:48
  • Works well with PHP 8.0 too `apt-get install php8.0-mysql` – Daniel Juric Mar 08 '21 at 02:30
42

First install php-mysql

sudo apt-get install php7.0-mysql
//change the version number based on the php version

then enable the module

sudo phpenmod pdo_mysql

and restart apache

sudo service apache2 restart 
StateLess
  • 5,344
  • 3
  • 20
  • 29
6

On ubuntu 18.04 following works for me

type this in your terminal

For php7.3

sudo apt-get install php7.3-gd php7.3-mysql

For php7.0

sudo apt-get install php7.0-gd php7.0-mysql

For php5

sudo apt-get install php5-gd php5-mysql
Sujeet malvi
  • 375
  • 3
  • 6
5

Since eggyal didn't provided his comment as answer after he gave right advice in a comment - i am posting it here: In my case I had to install module php-mysql. See comments under the question for details.

Community
  • 1
  • 1
SmxCde
  • 5,053
  • 7
  • 25
  • 45
5

First, check if your php.ini has the extension enabled "php_pdo_mysql" and "php_mysqli" and the path of "extension_dir" is correct. If you need one of above configuration, then, you must restart the php-fpm to apply the changes.

In my case (where i am using the Windows OS in the company, i really prefer OSX or Linux), i solved the problem putting this values in the php.ini:

; ...

extension_dir = "ext"

; ... 

extension=php_mysqli.dll
extension=php_pdo_mysql.dll

; ...

I hope this helps.

João Paulo Cercal
  • 733
  • 1
  • 8
  • 12
4

If you are on windows, and your php folder is not in your PATH, you have set the absolute directory in your php.ini

for example:

extension_dir = "C:/php7/ext"

and uncomment

extension=php_mysqli.dll
extension=php_pdo_mysql.dll

Restart apache2.4 and it should work.

I hope it helps.

user1455180
  • 511
  • 4
  • 9
4
  1. download the source code of php 7 and extract it.
  2. open your terminal
  3. swim to the ext/mysqli directory
  4. use commands:

    phpize

    ./configure

    make

    make install (as root)

  5. enable extension=mysqli.so in your php.ini file
  6. done!

This worked for me

micl
  • 91
  • 1
  • 8
  • Thanks, the same worked for pdo_mysql. Not sure why it isn't compiled as part of default make;make install o_O – dagelf Mar 01 '17 at 07:15
2

I had, pretty much, the same problem. I was able to see that PDO was enabled but I had no available drivers (using PHP 7-RC4). I managed to resolve the issue by adding the php_pdo_mysql extension to those which were enabled.

Hope this helps!

jgadrow
  • 21
  • 1
2

I resolved my problem on ubunto 20.4 by reinstalling php-mysql.

Remove php-mysql:

sudo apt purge php7.2-mysql

Then install php-mysql:

sudo apt install php7.2-mysql

It will add new configurations in php.ini

SEYED BABAK ASHRAFI
  • 4,093
  • 4
  • 22
  • 32
1

Had the same issue, resolved by actually enabling the extension in the php.ini with the right file name. It was listed as php_pdo_mysql.so but the module name in /lib/php/modules was called just pdo_mysql.so

So just remove the "php_" prefix from the php.ini file and then restart the httpd service and it worked like a charm.

Please note that I'm using Arch and thus path names and services may be different depending on your distrubution.

Xorifelse
  • 7,878
  • 1
  • 27
  • 38
1

I'll start with the answer then context NOTE this fix was logged above, I'm just re-stating it for anyone googling.

  1. Download the source code of php 7 and extract it.
  2. open your terminal
  3. swim to the ext/pdo_mysql directory
  4. use commands:

    phpize

    ./configure

    make

    make install (as root)

  5. enable extension=mysqli.so in your php.ini file

This is logged as an answer from here (please upvote it if it helped you too): https://stackoverflow.com/a/39277373/3912517

Context: I'm trying to add LimeSurvey to the standard WordPress Docker. The single point holding me back is "PHP PDO driver library" which is "None found"

php -i | grep PDO                                                                                  
PHP Warning:  PHP Startup: Unable to load dynamic library 'pdo_odbc' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20170718/pdo_odbc (/usr/local/lib/php/extensions/no-debug-non-zts-20170718/pdo_odbc: cannot open shared object file: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20170718/pdo_odbc.so (/usr/local/lib/php/extensions/no-debug-non-zts-20170718/pdo_odbc.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
PHP Warning:  Module 'mysqli' already loaded in Unknown on line 0
PDO
PDO support => enabled
PDO drivers => sqlite
PDO Driver for SQLite 3.x => enabled

Ubuntu 16 (Ubuntu 7.3.0)

apt-get install php7.0-mysql

Result:

Package 'php7.0-mysql' has no installation candidate

Get instructions saying all I have to do is run this:

add-apt-repository -y ppa:ondrej/apache2

But then I get this:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 223: ordinal not in range(128)

So I try and force some type of UTF: LC_ALL=C.UTF-8 add-apt-repository -y ppa:ondrej/apache2 and I get this: no valid OpenPGP data found.

Follow some other instructions to run this: apt-get update and I get this: Err:14 http://ppa.launchpad.net/ondrej/apache2/ubuntu cosmic/main amd64 Packages 404 Not Found Err:15 http://ppa.launchpad.net/ondrej/php/ubuntu cosmic/main amd64 Packages 404 Not Found and - I think because of that - I then get:

The repository 'http://ppa.launchpad.net/ondrej/apache2/ubuntu cosmic Release' does not have a Release file.

By this stage, I'm still getting this on apt-get update:

Package 'php7.0-mysql' has no installation candidate.

I start trying to add in php libraries, got Unicode issues, tried to get around that and.... you get the idea... whack-a-mole. I gave up and looked to see if I could compile it and I found the answer I started with.

You might be wondering why I wrote so much? So that anyone googling can find this solution (including me!).

  • I have needed to come back here to repeat this, and then had to search down how to find php.ini because my docker install is into a different location: php --ini – user3912517 Nov 27 '18 at 01:33
0
['class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost:3306;dbname=testdb',
    'username' => 'user',
    'password' => 'password',
    'charset' => 'utf8',]

It's simple: Just provide the port number along with the host name and set default sock path to your mysql.sock file path in php.ini which the server is running on.

0

Just as an update for those working with installing Linux programs, please note, that things such as 'php7.0-common' are now simply known as 'php-common'.

Anthony Pulse
  • 71
  • 1
  • 7
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 24 '22 at 08:26