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

本地Laragon环境Laravel项目遇MySQL max_user_connections报错求助

Fixing "max_user_connections" Error in Laravel with Laragon (Without Increasing Limits Indefinitely)

Hey there, I get it—cranking up max_user_connections is just a band-aid, so let's dig into the root causes and fix this properly. Here's a step-by-step approach tailored to your Laravel + Laragon setup:

1. First, Diagnose What's Using Up Connections

Before making any changes, let's see exactly what's happening with your MySQL connections.

  • Open Laragon's MySQL console (click the Laragon icon > MySQL > MySQL Console), then run these commands:
    • SHOW FULL PROCESSLIST; This will list every active connection, their state (Sleep/Query), and the query they're running. Look for:
      • A ton of Sleep connections (these are idle but still holding slots)
      • Long-running queries that are blocking connections
      • Multiple connections from the same source (like a stuck queue worker)
    • SELECT COUNT(*) FROM information_schema.processlist WHERE USER = 'test_develop'; This gives you the exact number of active connections for your user right now.

2. Fix Laravel's Connection Management

Laravel might be creating more connections than necessary if configured incorrectly.

  • Check Connection Pool Settings
    Edit your config/database.php file for the MySQL connection, add a pool configuration to reuse connections instead of creating new ones every time:

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        // ... other existing config
        'pool' => [
            'min' => 2,
            'max' => 10, // Adjust based on your app's needs—don't set this too high
            'idle_timeout' => 300, // Close idle connections after 5 minutes
        ],
    ],
    

    This tells Laravel to maintain a pool of reusable connections, preventing unnecessary new connections.

  • Check for Connection Leaks in Code

    • Look for places where you manually create connections (e.g., DB::connection('custom')->...) and make sure you're disconnecting them when done with DB::disconnect('custom').
    • Avoid creating new connections inside loops—reuse the default connection instead.
    • If you're running Artisan commands or queue workers, ensure they're not leaking connections. For queue workers, add a timeout (php artisan queue:work --timeout=60) to kill stuck tasks, and consider restarting workers periodically to clear any accumulated connections.
  • Fix N+1 Query Issues
    While not directly a connection issue, N+1 queries (loading related models one by one instead of eager loading) can spike the number of concurrent connections during high traffic. Use Laravel's with() method to eager load relationships and reduce the number of queries.

3. Tweak MySQL to Clean Up Idle Connections

Laragon's default MySQL settings might keep idle connections alive for too long, eating up slots.

  • Open your MySQL config file in Laragon: laragon\bin\mysql\mysql-[version]\my.ini
  • Find and modify these parameters (add them if they don't exist):
    wait_timeout = 300
    interactive_timeout = 300
    
    This tells MySQL to close idle connections after 5 minutes (300 seconds). Save the file, then restart MySQL via Laragon (right-click the Laragon icon > MySQL > Restart).

4. Check Laragon Service Setup

  • Make sure you don't have multiple instances of your Laravel app running (e.g., multiple php artisan serve processes, or duplicate Apache/Nginx sites). Each running instance can create its own set of connections.
  • Verify that Laragon's MySQL service is running smoothly—if it's restarting unexpectedly, it might leave stale connections hanging around.

Final Notes

The key here is to fix the source of the connection bloat, not just raise the limit. By reusing connections, cleaning up idle ones, and fixing code leaks, you should see the connection count drop to a manageable level.

内容的提问来源于stack exchange,提问作者Jass

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:46:44