1

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?

Question Overflow
  • 2,103
  • 7
  • 30
  • 45

1 Answers1

3

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.

Ladadadada
  • 26,337
  • 7
  • 59
  • 90