Drupal : PDOException: SQLSTATE[HY000][2002] on Azure PAAS

A month back I was trying to browse this site  (https://technobytes.azurewebsites.net)  which is a Drupal Website and is hosted on Azure as an App Service and  suddenly I could see the below exception. I kind of wondered what happened.

I quickly checked that line of code mentioned in the exception but couldn't find much info about exception but I got a hint that cause might be hidden in the sites/default/settings.php

I checked the php script and it had all the information about the SQL connection

Now I checked its socket for connecting to the MySQL server and I could see 127.0.0.1:40456

So I quickly checked the data/mysql/MYSQLCONNSTR_localdb.txt to check the connection string and I could see a different port in the socket.

I changed the port in the settings.php which is there in the connection string and the site started working as expected.

What actually happened?

MySQL runs on a process created on azure called mysqld.exe, due to some reason it was recycled i.e. in simple words the process was be killed and re-spawned, and Port was changed. And it was updated in the connection string file

Resolution:

Find out the new Port from data/mysql/MYSQLCONNSTR_localdb.txt file and update it in your sites/default/settings.php as below


$databases['default']['default'] = array( * 'driver' => 'pgsql', * 'database' => 'databasename', * 'username' => 'username', * 'password' => 'password', * 'host' => 'localhost:port', * 'prefix' => '', * );


I hope this helps you.

Happy Coding !