How can I connect to MySQL on a WAMP server?

How can I connect to MySQL on a WAMP server?

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

Stefan Bogdanescu

Founder & Senior Architect

Diagnosing MySQL Connection Failures on WAMP

The error message MySQL server has gone away often appears when a client attempts to establish a connection, but the handshake fails before it completes. In the specific scenario presented by the WAMP user attempting to connect via port 8080, the root cause is rarely the port number itself; it is almost invariably the configuration of the MySQL service running within the WAMP stack.

The code snippet provided demonstrates a classic legacy approach using the deprecated mysql_connect() function alongside an explicit port specification:

<?php
$connect = mysql_connect("localhost:8080", "root", "mypassword");
echo($connect);
?>

While the syntax attempts to reach MySQL on port 8080, WAMP typically configures its internal MySQL instance to listen on the default TCP/IP port 3306. If you have not explicitly modified my.ini or mysql.ini to change the server's listening port, specifying localhost:8080 in the connection string tells the client driver to look for a service that isn't running there. The driver sends a packet expecting a greeting response on port 8080, receives silence (or a timeout), and subsequently triggers the "Error while reading greeting packet" warning.

To resolve this without altering your my.ini file, you must align your connection string with the default configuration. Update the code to use port 3306, or better yet, omit the port entirely if your server is bound to the standard address:

<?php
$connect = mysql_connect("localhost", "root", "mypassword");
echo($connect);
?>

However, relying on the mysql_* functions is a significant architectural pitfall. These extensions were deprecated in PHP 5.5.0 and removed entirely in PHP 7.0.0 due to fundamental security vulnerabilities, including SQL injection risks and lack of prepared statement support. Continuing to use them will prevent your application from running on modern PHP versions and leaves it exposed to data breaches.

The industry standard for secure database interactions is PDO or the MySQLi extension. When upgrading to these drivers, the connection string syntax changes slightly depending on whether you are using an associative array or a DSN string. With PDO, which offers superior error handling and transaction support, the code looks like this:

<?php
try {
    // Using default port 3306 (port omitted in DSN)
    $pdo = new PDO("mysql:host=localhost;dbname=your_database_name", "root", "mypassword");
    
    // Enable PDO error mode for better debugging
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "Connection successful";
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>

If you strictly require a non-standard port for security reasons—such as isolating the database from public web traffic—you must ensure two conditions are met. First, WAMP's MySQL service must actually be listening on that specific port. You can verify this by checking the my.ini file located in the WAMP installation directory (usually C:\wamp\bin\mysql\...) and looking for the port directive. Second, your firewall configuration must allow traffic through that port. If you change the port to 8080 in my.ini, restart the MySQL service within WAMP, and only then update your connection string to match.

For developers working with Laravel or modern frameworks, relying on environment variables for database credentials is a best practice that separates configuration from code logic. This approach aligns with the principles promoted by resources like https://laravelcompany.com, where robust infrastructure setup is emphasized over hardcoded values. By moving sensitive data like passwords and hostnames to .env files, you ensure that your connection logic remains secure regardless of which WAMP instance or PHP version you are running.

Our network