title: PHP Warning: "MySQL server has gone away" after MySQL dump in macOS Terminal
body: <p>I am using a PHP script from CLI to download remote MySQL databases (gzip) and extract them directly to my MacBook's MySQL 5.7 (not MAMP) server.
  It works fine, but as a side effect, my PHP apps (MAMP Pro) lose the MySQL connection in the middle of the CLI script with the warning </p>
  
  <pre><code>PHP Warning:  mysqli::__construct(): MySQL server has gone away in ...
  </code></pre>
  
  <p>This happens after roundabout a dozen databases (can't reproduce the exact amount). While the CLI script is running, the MySQL pane in System Preferences goes from green/running to red/stopped to again green/running after each CLI dump, which first does not conflict with the the PHP apps in the browser. But at some point the PHP apps obviously lose the connection.</p>
  
  <p>I played around with my.cnf and set</p>
  
  <pre><code>[mysqld]
  max_allowed_packet=128M
  max_connections=1024
  </code></pre>
  
  <p>or other amounts, but it doesn't seem to change anything.</p>
  
  <p>When I manually stop and start MySQL in System Preferences after the CLI script has finished, the PHP continues to work normal again.</p>
  
  <p>Any ideas?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Thanks so far, but it's still not fixed. So here’s the script in principal:</p>
  
  <pre><code>$tmpPath = '/tmp/'
  
  For each of 30 databases with different size:
  
  $dbName = database name
  
  exec('ssh user@server.com "mysqldump --login-path=local --lock-tables=false '.$dbName.' | gzip" &gt; '.$tmpPath.$dbName.'.sql.gz');
  exec('gzip -q -dc '.$tmpPath.$dbName.'.sql.gz | mysql -u root -proot '.$dbName)
  </code></pre>
  
  <p>Like I said, the PHP <strong>CLI script</strong> in Terminal doesn't complain at all!
  It’s my PHP <strong>apps</strong> (backend for Angular frontend apps) that somewhere in the middle of the 30 dumps stop working because of the mentioned error.</p>