title: Run wordpress code from command prompt via PHP
body: <p>I need to run custom WP script from command line :  php myscript.php
  Whatever I do it doesn't work.  I get this message:</p>
  
  <p><strong>Error establishing a database connection</strong></p>
  
  <p>I tried </p>
  
  <pre><code>define('DOING_AJAX', true);
  define('WP_USE_THEMES', false);
  $_SERVER = array(
      "HTTP_HOST" =&gt; "mysite.com",
      "SERVER_NAME" =&gt; "mysite.com",
      "REQUEST_URI" =&gt; "/myscript.php",
      "REQUEST_METHOD" =&gt; "GET"
  );
  require_once('wp-load.php');
  </code></pre>
  
  <p>mentioned <a href="https://wordpress.stackexchange.com/questions/5349/access-wordpress-api-outside-of-wordpress-command-line-php">here</a> but it doesn't work. All server variables set by me are OK, I copied the whole $_SERVER variable from web page launched in browser (there it works)   </p>
  
  <p>My working directory is the root of the WP install so I simply use: <strong>php myscript.php</strong>
  from command line.
  I tried several other codes mentioned on net but non of them works for me.
  e.g the one with :  </p>
  
  <pre><code>define('WP_USE_THEMES', false);
  require('wp-blog-header.php');
  </code></pre>
  
  <p>Always the same message:  <strong>Error establishing a database connection</strong>
  As I said from web browser it works OK.</p>
  
  <p>This is the extraction of mysql strings from php.ini used by php cli:</p>
  
  <pre><code>Configure Command =&gt;  '/var/tmp/apache_mod_php/apache_mod_php-53.3.1~2/php/configure'  '--prefix=/usr' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--disable-dependency-tracking' '--sysconfdir=/private/etc' '--with-apxs2=/usr/sbin/apxs' '--enable-cli' '--with-config-file-path=/etc' '--with-libxml-dir=/usr' '--with-openssl=/usr' '--with-kerberos=/usr' '--with-zlib=/usr' '--enable-bcmath' '--with-bz2=/usr' '--enable-calendar' '--with-curl=/usr' '--enable-exif' '--enable-ftp' '--with-gd' '--with-jpeg-dir=/BinaryCache/apache_mod_php/apache_mod_php-53.3.1~2/Root/usr/local' '--with-png-dir=/BinaryCache/apache_mod_php/apache_mod_php-53.3.1~2/Root/usr/local' '--enable-gd-native-ttf' '--with-ldap=/usr' '--with-ldap-sasl=/usr' '--enable-mbstring' '--enable-mbregex' '--with-mysql=mysqlnd' '--with-mysqli=mysqlnd' '--with-pdo-mysql=mysqlnd' '--with-mysql-sock=/var/mysql/mysql.sock' '--with-iodbc=/usr' '--enable-shmop' '--with-snmp=/usr' '--enable-soap' '--enable-sockets' '--enable-sysvmsg' '--enable-sysvsem' '--enable-sysvshm' '--with-xmlrpc' '--with-iconv-dir=/usr' '--with-xsl=/usr' '--enable-zend-multibyte' '--enable-zip' '--with-pcre-regex=/usr'
  mysql
  Client API version =&gt; mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $
  mysql.allow_local_infile =&gt; On =&gt; On
  mysql.allow_persistent =&gt; On =&gt; On
  mysql.connect_timeout =&gt; 60 =&gt; 60
  mysql.default_host =&gt; no value =&gt; no value
  mysql.default_password =&gt; no value =&gt; no value
  mysql.default_port =&gt; no value =&gt; no value
  mysql.default_socket =&gt; /var/mysql/mysql.sock =&gt; /var/mysql/mysql.sock
  mysql.default_user =&gt; no value =&gt; no value
  mysql.max_links =&gt; Unlimited =&gt; Unlimited
  mysql.max_persistent =&gt; Unlimited =&gt; Unlimited
  mysql.trace_mode =&gt; Off =&gt; Off
  mysqli
  Client API library version =&gt; mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $
  mysqli.allow_local_infile =&gt; On =&gt; On
  mysqli.allow_persistent =&gt; On =&gt; On
  mysqli.default_host =&gt; no value =&gt; no value
  mysqli.default_port =&gt; 3306 =&gt; 3306
  mysqli.default_pw =&gt; no value =&gt; no value
  mysqli.default_socket =&gt; /var/mysql/mysql.sock =&gt; /var/mysql/mysql.sock
  mysqli.default_user =&gt; no value =&gt; no value
  mysqli.max_links =&gt; Unlimited =&gt; Unlimited
  mysqli.max_persistent =&gt; Unlimited =&gt; Unlimited
  mysqli.reconnect =&gt; Off =&gt; Off
  mysqlnd
  mysqlnd =&gt; enabled
  Version =&gt; mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $
  PDO drivers =&gt; mysql, sqlite, sqlite2
  pdo_mysql
  Client API version =&gt; mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $
  pdo_mysql.default_socket =&gt; /var/mysql/mysql.sock =&gt; /var/mysql/mysql.sock
  </code></pre>