I found a PHP error in my server log that says:

[02:09:27 UTC] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory' in /var/www/include/db_connect.php:7
Stack trace:
`#0 /var/www/include/db_connect.php(7): PDO->__construct('mysql:dbname=my...', 'apache', '123...')

Further examination reveals that this error occurs at a time when the server is booting up. The journal log contains the following relevant lines:

02:09:22 systemd[1]: Starting The Apache HTTP Server...
02:09:22 systemd[1]: Starting MariaDB database server...
02:09:22 systemd[1]: Starting The PHP FastCGI Process Manager...
02:09:24 systemd[1]: Started The Apache HTTP Server.
02:09:25 mysqld_safe[713]: Logging to '/var/log/mariadb/mariadb.log'.
02:09:25 mysqld_safe[713]: Starting mysqld daemon with databases from /var/lib/mysql
02:09:25 systemd[1]: Started The PHP FastCGI Process Manager.
02:09:28 NetworkManager[579]: <info> startup complete
02:09:28 systemd[1]: Started MariaDB database server.

Since the PHP error occurs at the 27th second, three seconds after Apache has started, why does PHP complain that it couldn't find the file? In any case, should the starting sequence of Apache, PHP-FPM and MariaDB be reversed so that Apache is the last to get started?


Although the PHP error happens three seconds after Apache started up, it is still one second before MariaDB has finished starting up.

It might be instructive to look at line 7 of the PHP file db_connect.php. I would wager it's trying to connect to the database using a socket, and since the database hasn't created the socket yet, you get a No such file or directory error.

I'm guessing that you only have one server here running everything, rather than a pool with separate database and web tiers. So your website is down while the server is rebooting and the first few requests also fail until the database comes up. If you want to improve your uptime, you will need a cluster of some sort. If you just want to prevent the error happening, make sure the database is up before starting Apache. The method for achieving this depends on your distro.

  • Yes, I was revising my question to add that part and I now understand why the error occurs. You are fast :) – Question Overflow Aug 6 '14 at 14:44
  • Does that mean I have to start those processes manually? Or is there a way to specify the sequence? – Question Overflow Aug 6 '14 at 14:44
  • Usually distributions provide mechanisms to specify start-up sequence. Please have a look at your distribution documentation. – Tero Kilkanen Aug 6 '14 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.