Resolving "Connection Refused" When Running Laravel Migrations on XAMPP
The error SQLSTATE[HY000] [2002] Connection refused is a network-level signal, not a database logic error. It explicitly means that the MySQL client (in this case, your Laravel application) attempted to open a socket or TCP connection to the MySQL server but was rejected before any SQL query could be processed. The specific query failingâchecking for the migrations table in firstdbâis irrelevant to the root cause; the handshake failed, so the database never saw the request.
When you install XAMPP on macOS (suggested by the hostname Andress-MacBook-Pro), the architecture differs significantly from a standard Windows setup. XAMPP bundles its own MySQL installation, but it does not always start the daemon automatically with every system boot or sometimes requires manual intervention to ensure the port is listening.
Verifying the Daemon Status
The most common cause for this error on macOS is that the MySQL server inside XAMPP is simply not running. Even if phpmyadmin was previously accessible, the service might have crashed or been stopped. You can verify this by checking the status of the XAMPP control panel or using the terminal.
Navigate to your XAMPP installation directory (typically /Applications/XAMPP/xamppfiles/) and run:
./scripts/mysqladmin status
If this command returns nothing or an error indicating the server is down, you must start it. Using the XAMPP Control Panel GUI is often more reliable on macOS than running individual scripts. Look for the "MySQL" row and ensure the "Start" button is clicked. Once the status changes to "running," the mysqld process should be active in your Activity Monitor.
Configuration Mismatch: Port 3306 vs. XAMPP Default
If MySQL is running, the next hurdle is often a configuration mismatch regarding the listening port. Laravel defaults to connecting on port 3306. However, if you have previously installed other database tools (like Homebrew's default MariaDB/MySQL) or modified XAMPP settings, the ports might conflict.
Check your XAMPP MySQL configuration file located at /Applications/XAMPP/xamppfiles/etc/my.ini. Look for the line defining port:
[mysqld]
port = 3306
socket = /tmp/mysql.sock
Ensure that port is set to 3306 and that no other MySQL instances are occupying that port. You can verify available ports with:
lsof -i :3306
If another process holds the port, you must either stop that service or change the XAMPP configuration to use a different port (e.g., 3307) and update your .env file accordingly.
The Socket vs. TCP Connection Issue
The error trace shows a generic connection refusal, but on macOS, XAMPP often relies on Unix domain sockets rather than standard TCP/IP connections for local applications. Laravel's default configuration in config/database.php usually attempts to connect via TCP (host => '127.0.0.1') or a socket path.
If your application is trying to connect via IP (127.0.0.1) but the XAMPP MySQL server is configured to listen only on the socket (or vice versa), the connection will fail immediately. To fix this, check your .env file:
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=firstdb
DB_USERNAME=root
DB_PASSWORD=your_password
If you are unsure whether the server is listening on the socket or TCP, try forcing a TCP connection explicitly in your .env file. If that fails, switch DB_HOST to /tmp/mysql.sock (or the specific path defined in my.ini) and ensure the DB_PORT line is commented out or empty.
Firewall and Permissions
Although less common with localhost connections, macOS firewall settings can sometimes block local services if misconfigured. Ensure that your system firewall allows traffic on port 3306. Additionally, check file permissions on the socket file (often located in /tmp) or the data directory (/Applications/XAMPP/xamppfiles/var/mysql). If the Laravel process (running as user@Andress-MacBook-Pro) lacks read/write access to these directories, the connection will be refused.
Run ls -l /Applications/XAMPP/xamppfiles/var/mysql and ensure the owner is correct (often mysql:wheel). If permissions are restrictive, run:
chown -R mysql:mysql /Applications/XAMPP/xamppfiles/var/mysql
Once the daemon is running on the correct port with accessible sockets, running php artisan migrate should successfully query the information_schema.tables, creating the migrations table if it does not exist.