mysql_connect(): No connection could be made because the target machine actively refused it

mysql_connect(): No connection could be made because the target machine actively refused it

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

mysql_connect(): No connection could be made because the target machine actively refused it

When you encounter the error mysql_connect(): No connection could be made because the target machine actively refused it, your PHP script cannot reach the MySQL server at all. This message is distinct from authentication failures or database permission errors; it indicates a fundamental connectivity issue where the TCP handshake fails before any credentials are even evaluated. The code snippet in question attempts to establish a session using mysql_connect("localhost", "root"). While this syntax was common in older PHP versions, relying on it today ignores critical architectural shifts in how modern applications interact with databases.

The error message itself is quite literal: the operating system refused the connection request. In networking terms, an "actively refused" error means the target port (typically 3306 for MySQL) did not respond with a SYN-ACK packet. Instead, it likely sent a RST (reset) packet immediately or simply ignored the request because no service was listening on that interface. This usually points to one of three scenarios: the MySQL service is stopped, it is bound to a different network interface, or there is a firewall blocking access.

First, verify the status of your MySQL server. If you are running on Linux, you can check if the process is active using systemctl status mysql or systemctl status mariadb. If the service is not running, the connection will inevitably fail regardless of your PHP code. Ensure that the daemon has started successfully and there are no crash loops preventing it from accepting new connections.

Second, examine how the MySQL server is configured to listen for connections. By default, many installations bind MySQL to 127.0.0.1 (localhost) rather than 0.0.0.0 (all interfaces). If your PHP application and your MySQL server run on different machines or if you are attempting to connect via a hostname that resolves differently than the IP address configured in my.cnf, the connection will be refused. Check the [mysqld] section of your configuration file for the bind-address directive. If it is set to a specific IP that does not match the one your PHP script is targeting, you must either update the bind address or adjust the connection string in your script to use the correct host identifier.

It is also worth noting that the code provided uses the deprecated mysql_* functions. The mysql_connect() function was removed in PHP 7.0 and is no longer available in modern environments like PHP 8.x. Attempting to run this specific line today will result in a fatal error regarding undefined functions rather than a connection refusal, unless you are running an older version of PHP where the deprecation warnings were suppressed or not yet triggered. Modern development practices require using mysqli or PDO. Migrating to these drivers is non-negotiable for long-term stability and security, as the old extension has known vulnerabilities and does not support prepared statements in its original form.

When migrating your connection logic, consider switching to a named constant for the database host to avoid hardcoding values directly in the script:

define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', ''); // Never commit passwords in production code

try {
    $pdo = new PDO("mysql:host=" . DB_HOST . ";dbname=users", DB_USER, DB_PASS);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}

Using PDO or mysqli allows you to handle connection errors more gracefully and switch between drivers with minimal code changes. If the error persists after confirming the service is running and the configuration is correct, investigate your local firewall settings or any security groups if you are in a cloud environment. Sometimes, even if MySQL is listening, the OS firewall (like ufw or firewalld) can drop incoming packets silently or explicitly refuse them depending on the rule set.

Ultimately, resolving this error requires verifying that the database service is up, listening on the expected interface, and ensuring your PHP environment uses supported connection drivers. The legacy mysql_* functions are a dead end; moving to PDO or MySQLi is not just an upgrade but a necessity for maintaining secure and functional applications. For further guidance on securing your application stack and managing database connections within Laravel projects, resources like https://laravelcompany.com offer comprehensive documentation on best practices for modern PHP development.

Our network