title: How to execute a MySQL command using shell script from PHP page?
body: <p>I want to execute a shell script from php page which will execute a MySQL command.</p>
  
  <p>To do this I followed the way shown here: <a href="https://stackoverflow.com/a/8055745/2117868">https://stackoverflow.com/a/8055745/2117868</a></p>
  
  <p>Here is my <code>sqlscript.sh</code></p>
  
  <pre><code>#!/bin/sh
  sudo wget -t 50 -O /tmp/update.sql http://example.com/update.sql
  if [ $? -eq 0 ]; then
      mysql -h "localhost" -u "root" "-pXXXXXXXX" "database-name" &lt; "/tmp/update.sql"
      if [ $? -eq 0 ]; then
          sudo rm /tmp/update.sql
          echo "200"
      else
          echo "502"
      fi
  else
      echo "404"
  fi
  </code></pre>
  
  <p>And my php page is <code>runscript.php</code></p>
  
  <pre><code>&lt;?php
      shell_exec("sudo /path/to/script/sqlscript.sh");
  ?&gt;
  </code></pre>
  
  <p>Now, when I'm calling the <code>sqlscript.sh</code> from the <strong>server console</strong> or <strong>php page</strong> <code>runscript.php</code> it works perfectly and returns <code>200</code> as expected.</p>
  
  <p>But when I take MYSQL user and password in <code>~/.my.cnf</code> so I don't have to put it on the command-line at all:</p>
  
  <pre><code>[client]
  user = root
  password = XXXXXXXX
  </code></pre>
  
  <p>And added credentials in <code>sudoer</code> to execute this script without password.</p>
  
  <pre><code>User_Alias WWW_USER = www-data
  Cmnd_Alias WWW_COMMANDS_SQL = /path/to/script/sqlscript.sh
  WWW_USER ALL = (ALL) NOPASSWD: WWW_COMMANDS_SQL
  </code></pre>
  
  <p>Here is my <code>sqlscript.sh</code></p>
  
  <pre><code>#!/bin/sh
  sudo wget -t 50 -O /tmp/update.sql http://example.com/update.sql
  if [ $? -eq 0 ]; then
      mysql -h "localhost" "database-name" &lt; "/tmp/update.sql"
      if [ $? -eq 0 ]; then
          sudo rm /tmp/update.sql
          echo "200"
      else
          echo "502"
      fi
  else
      echo "404"
  fi
  </code></pre>
  
  <p>Now, when I'm calling the <code>sqlscript.sh</code> from the <strong>server console</strong> it works perfectly and returns <code>200</code> as expected. But if I call it from the php page</p>
  
  <pre><code>http://example.com/runscript.php
  </code></pre>
  
  <p>it returns <code>502</code> as it couldn't execute the SQL command.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Here is the Apache <code>error.log</code></p>
  
  <pre><code>--2014-12-05 10:51:55--  http://example.com/update.sql
  Resolving example.com (example.com)... 162.144.71.XXX
  Connecting to example.com (example.com)|162.144.71.XXX|:80... connected.
  HTTP request sent, awaiting response... 200 OK
  Length: 146 [text/x-sql]
  Saving to: `/tmp/update.sql'
  
       0K                                                       100% 10.9M=0s
  
  2014-12-05 10:51:55 (10.9 MB/s) - `/tmp/update.sql' saved [146/146]
  
  ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
  </code></pre>
  
  <p>So, please help me to solve the issue.</p>