You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VPS部署应用遇SQLSTATE[HY000][2002]错误,求问题排查

解决SQLSTATE[HY000] [2002] Invalid Argument数据库连接错误

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 of 127.0.0.1. Log into your VPS and run this command to verify:

    netstat -tulpn | grep 3306
    

    If you don't see 127.0.0.1:3306 in the output, try updating your .env to use DB_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.cnf or my.ini) to set bind-address=127.0.0.1 and restart the MySQL service.

  • Try connecting via Unix socket
    Many VPS MySQL setups use Unix sockets by default instead of TCP port 3306. If the netstat command didn't show a TCP listener for 3306, update your .env to point to the socket path. Common paths are:

    • Debian/Ubuntu: /var/run/mysqld/mysqld.sock
    • CentOS/RHEL: /var/lib/mysql/mysql.sock
      So your DB_HOST would look like:
    DB_HOST=/var/run/mysqld/mysqld.sock
    

    You can also add this line to your Laravel config/database.php under the mysql array to be explicit:

    'unix_socket' => '/var/run/mysqld/mysqld.sock',
    
  • Verify your MySQL user's connection permissions
    Even though you're using root, MySQL treats root@localhost and root@127.0.0.1 as separate accounts. Log into MySQL via command line:

    mysql -u root -p
    

    Then 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 localhost or 127.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 0
    

    Then restart your app (e.g., php artisan serve or reload Nginx/Apache). If this fixes it, make the change permanent:

    setsebool -P httpd_can_network_connect_db 1
    

    Also, double-check your local firewall (like ufw or iptables) 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:clear
    
  • Test 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 mamaput
    

    If 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:57:28