Index: trunk/lib/App.inc.php
===================================================================
--- trunk/lib/App.inc.php	(revision 732)
+++ trunk/lib/App.inc.php	(revision 733)
@@ -413,14 +413,14 @@
 
             // DB connection parameters taken from environment variables in the server httpd.conf file (readable only by root)âŠ
-            if (!empty($_SERVER['DB_SERVER']) && !$this->getParam('db_server')) {
+            if (isset($_SERVER['DB_SERVER']) && '' != $_SERVER['DB_SERVER'] && null === $this->getParam('db_server')) {
                 $this->setParam(array('db_server' => $_SERVER['DB_SERVER']));
             }
-            if (!empty($_SERVER['DB_NAME']) && !$this->getParam('db_name')) {
+            if (isset($_SERVER['DB_NAME']) && '' != $_SERVER['DB_NAME'] && null === $this->getParam('db_name')) {
                 $this->setParam(array('db_name' => $_SERVER['DB_NAME']));
             }
-            if (!empty($_SERVER['DB_USER']) && !$this->getParam('db_user')) {
+            if (isset($_SERVER['DB_USER']) && '' != $_SERVER['DB_USER'] && null === $this->getParam('db_user')) {
                 $this->setParam(array('db_user' => $_SERVER['DB_USER']));
             }
-            if (!empty($_SERVER['DB_PASS']) && !$this->getParam('db_pass')) {
+            if (isset($_SERVER['DB_PASS']) && '' != $_SERVER['DB_PASS'] && null === $this->getParam('db_pass')) {
                 $this->setParam(array('db_pass' => $_SERVER['DB_PASS']));
             }
Index: trunk/lib/PDO.inc.php
===================================================================
--- trunk/lib/PDO.inc.php	(revision 732)
+++ trunk/lib/PDO.inc.php	(revision 733)
@@ -81,4 +81,7 @@
         'character_set' => '',
         'collation' => '',
+
+        // Reconnection attempt limit. Set to 0 to disable retries, i.e., only the first connect will be attempted.
+        'retry_limit' => 8,
     );
 
@@ -164,5 +167,5 @@
     * @since    09 Jul 2019 08:16:42
     */
-    public function connect()
+    public function connect($retry_num=0)
     {
         $app =& App::getInstance();
@@ -174,5 +177,5 @@
 
         // If db_server not specified, assume localhost.
-        if (!$this->getParam('db_server')) {
+        if (null === $this->_params['db_server']) {
             $this->setParam(array('db_server' => 'localhost'));
         }
@@ -193,5 +196,5 @@
             $this->dbh = new \PDO($dsn, $this->getParam('db_user'), $this->getParam('db_pass'), $options);
         } catch (\PDOException $e) {
-            $mysql_error_msg = sprintf('PDO connect %s: %s (db_server=%s, db_name=%s, db_user=%s, db_pass=%s)',
+            $mysql_error_msg = sprintf('PDO connect %s: %s (db_server=%s, db_name=%s, db_user=%s, db_pass=%s)%s',
                 get_class($e),
                 $e->getMessage(),
@@ -199,12 +202,24 @@
                 $this->getParam('db_name'),
                 $this->getParam('db_user'),
-                ('' == $this->getParam('db_pass') ? 'NO' : 'YES')
+                ('' == $this->getParam('db_pass') ? 'NO' : 'YES'),
+                ($retry_num > 0 ? ' retry ' . $retry_num : '')
             );
-            $app->logMsg($mysql_error_msg, LOG_EMERG, __FILE__, __LINE__);
+            // Use LOG_NOTICE for first connection attempts, and LOG_EMERG for the last one.
+            $app->logMsg($mysql_error_msg, ($retry_num >= $this->getParam('retry_limit') ? LOG_EMERG : LOG_NOTICE), __FILE__, __LINE__);
+
+            // These are probably transient errors:
+            // SQLSTATE[HY000] [2002] Connection refused
+            // SQLSTATE[HY000] [2002] No such file or directory
+            // SQLSTATE[HY000] [2006] MySQL server has gone away
+            if ($retry_num < $this->getParam('retry_limit') && (strpos($e->getMessage(), '[2002]') !== false || strpos($e->getMessage(), '[2006]') !== false)) {
+                // Try again after a delay:
+                usleep(500000);
+                return $this->connect(++$retry_num);
+            }
 
             // Print helpful or pretty error?
             if ($this->getParam('db_debug') && $app->getParam('display_errors')) {
                 if (!$app->isCLI()) {
-                    printf('<pre style="padding:1em;background:#ddd;font:0.9rem monospace;">%s</pre>', $mysql_error_msg);
+                    printf('<pre style="padding:1em;background:#ddd;font:10px monospace;">%s</pre>', $mysql_error_msg);
                 }
             }
