title: Can't Seem to hit MySQL max_connections Although New Connections Fail Under Load
body: <p>We are benchmarking one of our sites with ab (apachebench) on a page that creates a MySQL connection, queries and then closes the connection (through PHP).</p>
  
  <p>We have hard coded our my.cnf with a max_connections limit of 500.</p>
  
  <p>When we run the stress test, MySQL never seems to hit the connection limit we specify although we DO HAVE "unable to connect to the database" type errors returned from our script.  It's as if MySQL can't open any more connections than around ~237 at one time.</p>
  
  <p>Here is our current my.cnf:</p>
  
  <pre><code>[mysqld]
  max_connections = 500
  port            = 3306
  socket          = /var/mysql/mysql.sock
  skip-external-locking
  key_buffer_size = 384M
  max_allowed_packet = 1M
  sort_buffer_size = 2M
  read_buffer_size = 2M
  read_rnd_buffer_size = 8M
  myisam_sort_buffer_size = 64M
  thread_cache_size = 8
  query_cache_size = 128M
  thread_concurrency = 8
  max_heap_table_size = 512M
  tmp_table_size = 512M
  table_cache = 2048
  </code></pre>
  
  <p>User Limits:</p>
  
  <pre><code>$ ulimit -a
  core file size          (blocks, -c) 0
  data seg size           (kbytes, -d) unlimited
  file size               (blocks, -f) unlimited
  max locked memory       (kbytes, -l) unlimited
  max memory size         (kbytes, -m) unlimited
  open files                      (-n) 16384
  pipe size            (512 bytes, -p) 1
  stack size              (kbytes, -s) 8192
  cpu time               (seconds, -t) unlimited
  max user processes              (-u) 1250
  virtual memory          (kbytes, -v) unlimited
  </code></pre>
  
  <p>The problem seems to be either with PHP or MySQL (or of course misconfiguration by us).</p>
  
  <p>Any help, suggestions and tips are greatly appreciated - thanks in advance.</p>