Warning: mysqli_connect(): (HY000/1049): Unknown database in mac terminal only
When you encounter Warning: mysqli_connect(): (HY000/1049): Unknown database specifically when executing a PHP script via cron on macOS, but see no issues when the same file runs through a browser, the culprit is almost certainly an environment mismatch. The web server and the cron daemon are two distinct processes running in isolation, each with its own configuration, user permissions, and database connection contexts.
The fact that the browser works confirms your PHP code logic and credentials are fundamentally sound. It also implies that the database service itself is running. The discrepancy arises because cron jobs execute as system services rather than under the web server's identity. On macOS, this usually points to one of two scenarios: missing environment variables or a failure to locate the MySQL/MariaDB socket file required for local connections.
Environment Variable Leakage
The most common cause for this specific behavior is that the database credentials are hardcoded in your script, but the path to the php.ini file or the my.cnf configuration differs between the web server and cron. If you rely on environment variables like MYSQL_PWD or MYSQL_HOST, they might be set in your shell profile (like .zshrc or .bash_profile) which loads for interactive terminal sessions but not for system services.
When you run the script manually in Terminal, your shell exports these variables, and mysqli_connect picks them up implicitly if using DSN strings like mysql:host=localhost;dbname=test. However, cron jobs do not inherit your interactive shell's environment by default. They lack the PATH, HOME, and often MYSQL_* variables unless explicitly passed or defined in the crontab entry.
To resolve this, you must ensure your script doesn't rely on inherited environment variables. Instead, pass them directly into the connection string using explicit DSN parameters:
$conn = new mysqli(
getenv('DB_HOST') ?: 'localhost',
getenv('DB_USER'),
getenv('DB_PASS'),
getenv('DB_NAME')
);
This ensures that if env is not set, it defaults to a known working state, but more importantly, it forces you to define the source of truth. If you are using a framework like Laravel, check your .env file location. Cron jobs often run from a different working directory than your web server. Ensure your application loads the environment variables regardless of the current PWD.
The Socket File Discrepancy
On macOS, MySQL and MariaDB often default to socket-based local connections rather than TCP/IP when connecting to localhost. The web server (Apache or Nginx) typically binds to a specific port or uses a different socket path defined in its configuration. Your cron job, running as a system user, might be trying to connect via TCP while the database expects a Unix domain socket, or vice versa.
If your my.cnf or macos.conf defines bind-address = 127.0.0.1, local connections must use the port (usually 3306). If it uses a socket file, the connection string might need to specify the full path:
$conn = new mysqli(
'localhost', // or explicit socket path like '/tmp/mysql.sock'
'user',
'pass',
'database',
3306 // Explicit port if socket is not used
);
Check your my.cnf file for the socket directive. If it points to something other than /tmp/mysql.sock, ensure your cron script knows where to look, or configure the web server and cron user to use the same socket path.
File Permissions and User Context
Cron jobs on macOS often run as a generic system user (like _mysql or root depending on configuration), whereas PHP-FPM or Apache runs as www or _http. If your database is configured to only allow connections from specific hosts or users, the cron job's identity might be rejected differently than the web server's.
Verify that the user running the cron job has read access to any temporary files or config paths your script expects. Additionally, ensure that the db_name specified in the error message actually exists for that specific MySQL user. Sometimes, privileges are granted to a database only for certain hosts (e.g., user@% vs user@localhost), and the cron environment might resolve localhost differently than the web server does.
If you are using Laravel, ensure your .env file is accessible from the directory where the cron job executes. If the cron entry changes directories before running the command, relative paths in your config loader might fail silently until runtime, leading to fallback behaviors that expose connection errors.
By aligning the environment variables, socket configurations, and user contexts between the web server and cron daemon, you can eliminate this discrepancy without needing to change your core application logic.