VPS部署应用遇SQLSTATE[HY000][2002]错误,求问题排查
Hey Patrick, let's work through this database connection bug you're hitting on your VPS. That SQLSTATE[HY000] [2002] Invalid argument error usually boils down to how your app is trying to reach MySQL, even though your .env looks solid at first glance. Here are the most likely fixes to try:
Check if MySQL is actually listening on localhost
Sometimes MySQL is configured to only bind to your VPS's public IP or a Unix socket instead of127.0.0.1. Log into your VPS and run this command to verify:netstat -tulpn | grep 3306If you don't see
127.0.0.1:3306in the output, try updating your.envto useDB_HOST=127.0.0.1(forces TCP/IP connection instead of relying on localhost resolution) or your VPS's public IP. You can also tweak MySQL's config file (my.cnformy.ini) to setbind-address=127.0.0.1and restart the MySQL service.Try connecting via Unix socket
Many VPS MySQL setups use Unix sockets by default instead of TCP port 3306. If thenetstatcommand didn't show a TCP listener for 3306, update your.envto point to the socket path. Common paths are:- Debian/Ubuntu:
/var/run/mysqld/mysqld.sock - CentOS/RHEL:
/var/lib/mysql/mysql.sock
So yourDB_HOSTwould look like:
DB_HOST=/var/run/mysqld/mysqld.sockYou can also add this line to your Laravel
config/database.phpunder themysqlarray to be explicit:'unix_socket' => '/var/run/mysqld/mysqld.sock',- Debian/Ubuntu:
Verify your MySQL user's connection permissions
Even though you're usingroot, MySQL treatsroot@localhostandroot@127.0.0.1as separate accounts. Log into MySQL via command line:mysql -u root -pThen run this query to check your root user's allowed hosts:
SELECT user, host FROM mysql.user WHERE user='root';If you don't see an entry for
localhostor127.0.0.1, create it with:GRANT ALL PRIVILEGES ON mamaput.* TO 'root'@'localhost' IDENTIFIED BY 'B0j0p@l3'; FLUSH PRIVILEGES;Check for SELinux or firewall blocks
On some Linux distros, SELinux can block PHP from accessing MySQL even on localhost. Temporarily disable it to test:setenforce 0Then restart your app (e.g.,
php artisan serveor reload Nginx/Apache). If this fixes it, make the change permanent:setsebool -P httpd_can_network_connect_db 1Also, double-check your local firewall (like
ufworiptables) isn't blocking port 3306 on localhost.Clear your Laravel config cache
Since you're running in production (APP_ENV=production), Laravel might be using cached config values that don't match your current.env. Run these commands to reset:php artisan config:clear php artisan cache:clearTest the connection directly with MySQL CLI
Rule out app-level issues by testing the connection from the command line:mysql -u root -pB0j0p@l3 -h localhost mamaputIf this fails, the problem is with MySQL itself or your user credentials. If it works, then the issue is likely in your Laravel setup (config cache, missing extensions, etc.).
Start with the command line test and MySQL listening check—those are the most common fixes for this exact error. Let me know if any of these get your app up and running!
内容的提问来源于stack exchange,提问作者Patrick Obafemi

