• 15
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 187

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 187
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I'm getting the following errors in my script:

mysqli_connect(): (08004/1040): Too many connections 
mysqli_connect(): (HY000/1040): Too many connections

What is the difference and how can I solve this problem?

"Too many connections" indicates, that your script is opening at least more than one connection to the database. Basically, to one server, only one connection is needed. Getting this error is either a misconfiguration of the server (which I assume isn't the case because max connections = zero isn't an option) or some programming errors in your script.

Check for re-openings of your database connections (mysqli_connect). There should only be one per script (!) and usually you should take care of reusing open connections OR close them properly after script execution (mysqli_close)

  • 11
Reply Report
      • 2
    • I read that mysqli auto closes the connections. In my application I am connecting the db on every ajax call and using json to return data. is the connection closed after the JSON response ?
      • 2
    • @sqlchild No, as long as you do not close it, it will linger open depending on settings of wait_timeout and interactive_timeout in your my.conf. See manual, but best practice is to close unused connections and release resources.
      • 2
    • "your script is opening at least more than one connection" Not really. The script could open just one connection and being called multiple times while the connection is still open.
    • "as long as you do not close it, it will linger open" According to PHP doc: "Open non-persistent MySQL connections and result sets are automatically destroyed when a PHP script finishes its execution ... explicitly closing open connections and freeing result sets is optional"

While I could not tell you the difference between the 2 error numbers above, I can tell you what causes this.

Your MySQL database only allows so many connections at the same time. If you connect to MySQL via PHP, then you generally open a new connection every time a page on your site loads. So if you've got too much traffic to your site this can cause this issue.

I think it is pretty common for people to have one connection to their database per page load, and multiple queries for sure. So really what it comes down to are 3 points:

(Let me just tell you now, persistent connections will not solve your issue.)

If you have access to your server's CLI/SSH, try to increase the limit by modifying your MySQL configuration (don't forget to restart the service for changes to take affect). This will of course consume more system resources on your database server.

If you have a lot of AJAX requests or other internal database connections you should try to get these down to a single script with a single call. Your site may make multiple AJAX calls to various PHP files that pulls MySQL data, which uses a whole database connection for each one. Instead, create a single PHP file to collect all the data you need on a given page, this script can get all the data you need while only using 1 database connection.

  • 2
Reply Report
      • 1
    • I read that mysqli auto closes the connections. In my application I am connecting the db on every ajax call and using json to return data. is the connection closed after the JSON response is sent to the user or does it needs to be closed ?
    • I have read the same, but my experience has not agreed with it. I specifically call the mysqli close function at the end of my scripts to be sure.

Steps to resolve that issue:

  1. Check MySQL connection limit in configuration file or my.cnf
  2. Run below command to check:

    mysql -e "show variables like '%connection%';"
    
  3. You will see like this:

    max_connections = 500
    
  4. Increase it as per you want:

    max_connections = 50000
    
  5. Restart the MySQL service:

    $ service MySQL restart
    

Now check your website, I hope the error will not occur!

Thank You!

  • 1
Reply Report

You should also check if your disk is full, this can cause the same error:

df -h

will show you the remaining space on each partition, you probably have to check the root partition / (or /var/ in case you have an extra partition for this):

df -h /
  • -1
Reply Report