OS: Linux Mint 21.3 Cinnamon
PHP Version: 8.3
I’m learning PHP and have installed it using the PPA ppa:ondrej/php
. I’m trying to connect to a MySQL database that’s running inside a Docker container.
The Docker file:
FROM mysql
ENV MYSQL_ROOT_PASSWORD=root
ENV MYSQL_USER=user
ENV MYSQL_PASSWORD=pass
ENV MYSQL_DATABASE=blog
COPY init.sql /docker-entrypoint-initdb.d/
EXPOSE 3306
The container is up and running as I am able to connect with it using python as well run SQL queries by going into the docker container.
When I run the PHP built in server using php -S localhost:8080
, I get the following error log
[Thu Sep 26 11:14:47 2024] PHP Warning: PHP Startup: Unable to load dynamic library 'pdo_mysql' (tried: /usr/lib/php/20230831/pdo_mysql (/usr/lib/php/20230831/pdo_mysql: cannot open shared object file: No such file or directory), /usr/lib/php/20230831/pdo_mysql.so (/usr/lib/php/20230831/pdo_mysql.so: undefined symbol: pdo_parse_params)) in Unknown on line 0
[Thu Sep 26 11:14:47 2024] PHP 8.3.11 Development Server (http://localhost:8080) started
[Thu Sep 26 11:14:52 2024] 127.0.0.1:38112 Accepted
[Thu Sep 26 11:14:52 2024] PHP Fatal error: Uncaught PDOException: could not find driver in /home/anon/Desktop/php/index.php:5
Stack trace:
#0 /home/anon/Desktop/php/index.php(5): PDO->__construct()
#1 {main}
thrown in /home/anon/Desktop/php/index.php on line 5
[Thu Sep 26 11:14:52 2024] 127.0.0.1:38112 [500]: GET / - Uncaught PDOException: could not find driver in /home/anon/Desktop/php/index.php:5
Stack trace:
#0 /home/anon/Desktop/php/index.php(5): PDO->__construct()
#1 {main}
thrown in /home/anon/Desktop/php/index.php on line 5
[Thu Sep 26 11:14:52 2024] 127.0.0.1:38112 Closing
the index.php
:
<?php
require("utils/functions.php");
// require("router.php");
$dsn = "mysql:host=localhost;port=3306;dbname=blog;charset=utf8mb4";
$pdo = new PDO($dsn, "user", "pass");
$statement = $pdo->prepare("SELECT * FROM posts");
$statement->execute();
$posts = $statement->fetchAll();
dd($posts)
?>
the functions.php
contains the function dd
:
<?php
function dd($supGlo) {
echo "<pre>";
var_dump($supGlo);
echo "</pre>";
die();
}
?>
I done the following to solve the issue without any success:
- Remove
php-common
and reinstalled PHP - Installed
php-mysql
- Installed
php8.3-mysqlnd
- Installed
php8.3-pdo-mysql
- Installed
php8.3-mysql
- Uncommented in
extension=pdo_mysql
inphp.ini
in both the cli and apache2 directories - restarted the
apache2
service - Have confirmed that I only have one PHP version installed
- Used
phpenmod
to enablepdo_mysql
and restarted theapache2
When I run php --ri pdo
, I get the below output,
PHP Warning: PHP Startup: Unable to load dynamic library 'pdo_mysql' (tried: /usr/lib/php/20230831/pdo_mysql (/usr/lib/php/20230831/pdo_mysql: cannot open shared object file: No such file or directory), /usr/lib/php/20230831/pdo_mysql.so (/usr/lib/php/20230831/pdo_mysql.so: undefined symbol: pdo_parse_params)) in Unknown on line 0
PDO
PDO support => enabled
PDO drivers => mysql
If I comment the extension=pdo_mysql
line in php.ini
then the warning, is removed.
PHP Warning: PHP Startup: Unable to load dynamic library 'pdo_mysql (tried: /usr/lib/php/20230831/pdo_mysql (/usr/lib/php/20230831/pdo_mysql: cannot open shared object file: No such file or directory), /usr/lib/php/20230831/pdo_mysql.so (/usr/lib/php/20230831/pdo_mysql.so: undefined symbol: pdo_parse_params)) in Unknown on line 0'
But the PDO drivers are still missing. Also I have confirmed that the /usr/lib/php/20230831/
directory exists and the shared object pdo_mysql.so
also exists in it.
pippen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4
I solved the above issue,
- Firstly, make sure that you have only one version of PHP installed and that you have installed the
php-mysql
extension from your package manager. - Then inside your
/etc/php/cli
and/etc/php/apache2
directories, edit your php.ini, search for theextension_dir
and set it to your extension directory. - To find your extension directory, go to
/lib/php/*******
the ******** represents YYYYMMDD and can be different for you but will always be in that format, i.e., mine was/lib/php/20230831
. This is your extension directory - Also, if you’re using a docker container, make sure that your PDO host is
127.0.0.1
and notlocalhost
pippen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.