Fixing nginx Error: Undefined constant PDO::MYSQL_ATTR_USE_BUFFERED_QUERY

I install a lot of Drupal sites day to day, especially when I'm doing dev work.

In the course of doing that, sometimes I'll be working on infrastructure—whether that's an Ansible playbook to configure a Docker container, or testing something on a fresh server or VM.

In any case, I run into the following error every so often in my Nginx error.log:

"php-fpm" nginx Error: Undefined constant PDO::MYSQL_ATTR_USE_BUFFERED_QUERY

The funny thing is, I don't have that error when I'm running CLI commands, like vendor/bin/drush, and can even install and manage the Drupal site and database on the CLI.

The problem, in my case, was that I had applied php-fpm configs using Ansible, but in my playbook I hadn't restarted php-fpm (in my case, on Ubuntu 22.04, php8.3-fpm) after doing so. So FPM was running with outdated config and didn't know that the MySQL/MariaDB drivers were even present on the system.

There are tons of forum posts, Drupal Answers posts, etc. saying the resolution to the issue was installing php8.3-mysql or the like, but if you're using PHP-FPM, make sure you have the MySQL ini added to the PHP config (use php -i to check your config paths), and make sure you've restarted PHP-FPM. Same thing with restarting Apache if you're using the built-in PHP support there.

Why did I post this blog post? Because after the fourth time I've run into this—spending on average like 30 minutes each time diagnosing the issue—I figured I'd write my own blog post so I find the solution I needed more quickly next time :)

Comments

Have you updated your playbooks/roles to restart when needed?

Yes; all my roles and official playbooks on GitHub do this automatically.

However, when building new automation or doing things by hand, I sometimes forget—also when automation runs into an issue on the initial provision, the best advice I have is to reboot the server/VM/container, then try again. That would solve so many of my little issues, ha!

I used to be one of those people that would post a question to a forum but never follow up when I found the problem. I finally changed when I was searching on an issue and the only thing I could find was my question some years prior with no follow up to how I solved it.

Now I do the same; whenever I have an issue that has me searching for an answer I ensure that I post what I find as it's very likely future me will need that information sometime.