Browse Source

fix database.php

svalavuo 7 giờ trước cách đây
mục cha
commit
e3b963aa7f
2 tập tin đã thay đổi với 26 bổ sung17 xóa
  1. 26 16
      backend/config/database.php
  2. 0 1
      docker-compose-with-services.yml

+ 26 - 16
backend/config/database.php

@@ -35,23 +35,33 @@ class Database {
     public function getConnection() {
         $this->conn = null;
 
-        // Check for local MySQL socket first
-        $localSocketPath = '/var/run/mysqld/mysqld.sock';
-        $localSocketExists = file_exists($localSocketPath);
+        // Check if we're in Docker environment or if host is a container name
+        $isDockerEnvironment = ($this->host === 'mariadb' || $this->host === 'mysql' || 
+                               strpos($this->host, 'inventory-') === 0 || 
+                               !file_exists('/var/run/mysqld/mysqld.sock'));
         
-        // Try local database connection first if socket exists
-        if ($localSocketExists) {
-            try {
-                $dsn = "mysql:unix_socket=" . $localSocketPath . ";dbname=" . $this->db_name . ";charset=utf8mb4";
-                error_log("Attempting local database connection via socket: $dsn with user " . $this->username);
-                $this->conn = new PDO($dsn, $this->username, $this->password);
-                $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
-                $this->conn->exec("set names utf8");
-                error_log("Local database connection successful via socket");
-                return $this->conn;
-            } catch(PDOException $exception) {
-                error_log("Local database connection failed: " . $exception->getMessage());
-                // Fall through to external connection
+        // In Docker environment, always use TCP connection
+        if ($isDockerEnvironment) {
+            error_log("Docker environment detected, using TCP connection to host: " . $this->host);
+        } else {
+            // Check for local MySQL socket first in non-Docker environments
+            $localSocketPath = '/var/run/mysqld/mysqld.sock';
+            $localSocketExists = file_exists($localSocketPath);
+            
+            // Try local database connection first if socket exists
+            if ($localSocketExists) {
+                try {
+                    $dsn = "mysql:unix_socket=" . $localSocketPath . ";dbname=" . $this->db_name . ";charset=utf8mb4";
+                    error_log("Attempting local database connection via socket: $dsn with user " . $this->username);
+                    $this->conn = new PDO($dsn, $this->username, $this->password);
+                    $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+                    $this->conn->exec("set names utf8");
+                    error_log("Local database connection successful via socket");
+                    return $this->conn;
+                } catch(PDOException $exception) {
+                    error_log("Local database connection failed: " . $exception->getMessage());
+                    // Fall through to external connection
+                }
             }
         }
 

+ 0 - 1
docker-compose-with-services.yml

@@ -78,7 +78,6 @@ services:
       retries: 3
       start_period: 40s
 
-
 networks:
   default:
     name: inventory-network