title: Why does Shell Script to create database via mysql work in terminal but not with exec, passthru, or shell_exec?
body: <p>I have the following basic shell script here:</p>
  <pre><code>#!/usr/bin/env bash
  
  # run as root
  if [ &quot;`id -u`&quot; -ne 0 ]; then
   exec sudo &quot;$0&quot; &quot;$@&quot;
   exit 99
  fi
  
  # dbname mandatory
  if [ -z ${1+x} ]; then
    exit 1;
  fi
  
  set -a
  source /path/to/vars.env
  
  dbhost=${dbhost:=localhost}
  
  dbname=&quot;$1&quot;
  
  mysql --defaults-extra-file=$dbcnf_path&lt;&lt;EOF
  CREATE DATABASE IF NOT EXISTS $dbname;
  GRANT ALL PRIVILEGES ON $dbname.* TO '$dbuser'@'$dbhost';
  ALTER DATABASE $dbname CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
  EOF
  
  echo &quot;Database $dbname was processed.&quot;
  </code></pre>
  <p>I have a <code>vars.env</code> file that just sets the <code>dbcnf_path</code> (full directory path to a config.cnf file), <code>dbuser</code>, and <code>dbhost</code> variable values.</p>
  <p>The <code>config.cnf</code> file looks like this:</p>
  <pre><code>[client]
  user = &quot;user&quot;
  password = &quot;mypassword&quot;
  host = &quot;localhost&quot;
  </code></pre>
  <p>I have edited <code>/etc/sudoers</code> file to allow this .sh file to even run as root (in case that is a problem, which shouldn't be I don't think cause it just accessing mysql).</p>
  <p>The php code that executes this (I have tried all ways:  <code>passthru</code>, <code>shell_exec</code>, and finally <code>exec</code> none of them create the database) is as follows:</p>
  <pre><code>&lt;?php 
      $dbName = 'dbNameTest';
      $cmd = './mysql_test.sh ' . $dbName;
      $data = exec($cmd, $output);
  ?&gt;
  </code></pre>
  <p>Not sure why the database will not be created.  Any ideas how this might need to be changed in order to work via php with exec, shell_exec, or even passthru?  I'm not sure why it's not working, seems a bit odd unless that was intentional to not allow apache to update the database via php in a shell script.  I've just recently updated bash to the newest version and mysql is running version 8.x.x if that helps.</p>