|
@@ -35,23 +35,33 @@ class Database {
|
|
|
public function getConnection() {
|
|
public function getConnection() {
|
|
|
$this->conn = null;
|
|
$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
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|