0

I am trying to run a php file through shell script multiple times.

I open connection like this (index.php).

mysqli_connect('localhost', 'root', 'password', 'database')

I got following error

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

I could increase max_connections in mysql my.cnf file. But I think this is not a good idea because it slows things down.

Here is my .sh file code when I run. It hits php file 750 times

#!/bin/bash

max=750
for i in `seq 0 $max`
do
    php index.php $i &
done

Can I share one connection across a file when it is hit simultaneously? Or is there any better approach?

Any Help?

Ali Mehdi
  • 884
  • 1
  • 11
  • 33
  • 1
    Show us the source code of the page showing the error. – twodee Aug 17 '16 at 12:09
  • 2
    Take a look on http://stackoverflow.com/questions/29224001/how-to-share-a-mysql-connection-between-2-different-php-processes – Michail M. Aug 17 '16 at 12:11
  • Updated question @CoderDudeTwodee – Ali Mehdi Aug 17 '16 at 12:12
  • You're either creating the connection twice or more in the same page or their is a server misconfiguration (less likely). – twodee Aug 17 '16 at 12:17
  • Max connections defaults to 151 according to the mysql manual. So you are likely hitting that. Echo $max to see when the error occurs. https://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html – Progrock Aug 17 '16 at 12:19
  • @Progrock Yeah I was able to solve this as I explained in my question by increasing connection. My real question is that can I share one connection across same file running multiple times? – Ali Mehdi Aug 17 '16 at 12:25
  • Could you add some more context to the question. Do you really need to background those processes? What are you doing? – Progrock Aug 17 '16 at 12:27

0 Answers0