Answers.Body: <p>OK, I was being stupid. Typo in the Drupal database password in the CiviCRM config file. </p>
  
  <p>Digging into civicrm/packages/DB/mysql.php and inserting some logging showed that the passwords being used for the Drupal user were just <em>slightly</em> different between Drupal and CiviCRM. CiviCRM was adding an extra '@' to the end. </p>
  
  <p>Didn't see it before because due to the CiviCRM config file format, it was a double @ - my eye didn't see the double.</p>
  
  <p>define( 'CIVICRM_UF_DSN'           , 'mysql://host_drupal:123456789@@localhost/host_drupal?new_link=true' );</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-01T18:13:03.733Z
Answers.LastActivityDate: 2016-07-01T18:13:03.733Z
Answers.UserDisplayName: GHolmes
Answers.Body: <p>Commandline and webserver PHP differ in a few ways.</p>
  
  <ul>
  <li>They can read different PHP configurations via <code>php.ini</code></li>
  <li>They may therefore have different PHP modules enabled (this doesn't seem to be the problem for you, but might cause CLI PHP to lack the <code>mysql</code> module)</li>
  <li>CLI PHP doesn't automatically have the HTTP host populated</li>
  </ul>
  
  <p>IMO the most likely cause is that the relocation to a dev environment has changed behaviour in the last of those, if you're using Drupal's <a href="https://www.drupal.org/documentation/install/multi-site" rel="nofollow">multisite functionality</a>. You might have addressed this for the new dev environment, but not informed the cron task of your changed config.</p>
  
  <p>If so, you can probably address this by -</p>
  
  <ul>
  <li>updating the cron command to use the dev URL (<code>drush -l example.dev civicrm-cron</code>), or </li>
  <li>through Drupal's <a href="https://api.drupal.org/api/drupal/sites%21example.sites.php/7.x" rel="nofollow"><code>sites.php</code></a>, or </li>
  <li>symlink <code>sites/example.dev =&gt; sites/example.com</code> directory (*ew).</li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-30T15:21:20.437Z
Answers.Text: The cron job is actually calling it via URL though. I edited my question.
Answers.UserDisplayName: GHolmes
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-01T00:05:24.100Z
Answers.Text: This answer doesn't apply to your question then, sorry! Will leave it here for posterity & to ensure no-one repeats my error :)
Answers.UserDisplayName: Chris Burgess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-29T22:47:10.857Z
Answers.LastActivityDate: 2016-06-29T23:04:53.980Z
Answers.LastEditDate: 2016-06-29T23:04:53.980Z
Answers.UserDisplayName: Chris Burgess
Body: <p>I moved a site - actually copied a production site to a development region. </p>
  
  <p>The site is working in the development region, including CiviCRM, but the CiviCRM cron job fails:</p>
  
  <pre><code>Jun 29 16:33:02  [info] $Fatal Error Details = Array
  (
      [callback] =&gt; Array
          (
              [0] =&gt; CRM_Core_Error
              [1] =&gt; handle
          )
  
      [code] =&gt; -24
      [message] =&gt; DB Error: connect failed
      [mode] =&gt; 16
      [debug_info] =&gt;  [nativecode=mysql_connect(): Access denied for user 'hostuser_drupal'@'localhost' (using password: YES)]
      [type] =&gt; DB_Error
      [user_info] =&gt;  [nativecode=mysql_connect(): Access denied for user 'hostuser_drupal'@'localhost' (using password: YES)]
      [to_string] =&gt; [db_error: message="DB Error: connect failed" code=-24 mode=callback callback=CRM_Core_Error::handle prefix="" info=" [nativecode=mysql_connect(): Access denied for user 'hostuser_drupal'@'localhost' (using password: YES)]"]
  )
  </code></pre>
  
  <p>I can log into the site as the same user that the cron job is using, and everything works. But the cron job fails, saying that the Drupal database user is denied.</p>
  
  <p>There is a Drupal database and a CiviCRM database. There is a MySQL user for each database. At this point, both users have access to both databases. </p>
  
  <p>Obviously the credentials in the config files <em>are</em> correct, since Drupal and CiviCRM work as normal in the browser. But the cron task fails as described above, whether called by cron or by using the cron URL in the browser. </p>
  
  <p>Here is the cron job:</p>
  
  <pre><code>wget -O - -q -t 1 'http://somedevsite.com/sites/all/modules/civicrm/bin/cron.php?name=admin&amp;pass=33333333333333&amp;key=44444444444444444444444444444444'
  </code></pre>
  
  <p>What could be going on?  </p>
  
  <p>EDIT
  I switched the cron task to call /civicrm/bin/cli.php directly (via php) and it fails in exactly the same way. </p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-06-29T22:47:19.907Z
Comments.Text: Might better inform your responses if you include the cron command you're using here - make sure to remove your user/pass details when doing so :)
Comments.UserDisplayName: Chris Burgess
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-06-30T15:06:27.813Z
Comments.Text: Edited to add cron command.
Comments.UserDisplayName: GHolmes
Tags: <database><cron-job><cron>
Title: database connection fails, but only in cron
ThreadText: # database connection fails, but only in cron
  
  (Asked by: username_0 on 2016-06-29)
  
  I moved a site - actually copied a production site to a development region.
  
  The site is working in the development region, including CiviCRM, but the CiviCRM cron job fails:
  
  ```
  Jun 29 16:33:02  [info] $Fatal Error Details = Array
  (
      [callback] => Array
          (
              [0] => CRM_Core_Error
              [1] => handle
          )
  
      [code] => -24
      [message] => DB Error: connect failed
      [mode] => 16
      [debug_info] =>  [nativecode=mysql_connect(): Access denied for user 'hostuser_drupal'@'localhost' (using password: YES)]
      [type] => DB_Error
      [user_info] =>  [nativecode=mysql_connect(): Access denied for user 'hostuser_drupal'@'localhost' (using password: YES)]
      [to_string] => [db_error: message="DB Error: connect failed" code=-24 mode=callback callback=CRM_Core_Error::handle prefix="" info=" [nativecode=mysql_connect(): Access denied for user 'hostuser_drupal'@'localhost' (using password: YES)]"]
  )
  
  ```
  
  I can log into the site as the same user that the cron job is using, and everything works. But the cron job fails, saying that the Drupal database user is denied.
  
  There is a Drupal database and a CiviCRM database. There is a MySQL user for each database. At this point, both users have access to both databases.
  
  Obviously the credentials in the config files *are* correct, since Drupal and CiviCRM work as normal in the browser. But the cron task fails as described above, whether called by cron or by using the cron URL in the browser.
  
  Here is the cron job:
  
  ```
  wget -O - -q -t 1 'http://somedevsite.com/sites/all/modules/civicrm/bin/cron.php?name=admin&pass=33333333333333&key=44444444444444444444444444444444'
  
  ```
  
  What could be going on?
  
  EDIT
  I switched the cron task to call /civicrm/bin/cli.php directly (via php) and it fails in exactly the same way.
  
  **Tags:** database, cron-job, cron
  
  > **Comments:**
  > **username_1**: Might better inform your responses if you include the cron command you're using here - make sure to remove your user/pass details when doing so :)  
  > **username_0**: Edited to add cron command.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2016-07-01. Score: 1 | Accepted answer
  
  OK, I was being stupid. Typo in the Drupal database password in the CiviCRM config file.
  
  Digging into civicrm/packages/DB/mysql.php and inserting some logging showed that the passwords being used for the Drupal user were just *slightly* different between Drupal and CiviCRM. CiviCRM was adding an extra '@' to the end.
  
  Didn't see it before because due to the CiviCRM config file format, it was a double @ - my eye didn't see the double.
  
  define( 'CIVICRM\_UF\_DSN' , 'mysql://host\_drupal:123456789@@localhost/host\_drupal?new\_link=true' );
  
  
  ---
  
  ### Answer by username_1 on 2016-06-29. Score: 0
  
  Commandline and webserver PHP differ in a few ways.
  
  * They can read different PHP configurations via `php.ini`
  * They may therefore have different PHP modules enabled (this doesn't seem to be the problem for you, but might cause CLI PHP to lack the `mysql` module)
  * CLI PHP doesn't automatically have the HTTP host populated
  
  IMO the most likely cause is that the relocation to a dev environment has changed behaviour in the last of those, if you're using Drupal's [multisite functionality](https://www.drupal.org/documentation/install/multi-site). You might have addressed this for the new dev environment, but not informed the cron task of your changed config.
  
  If so, you can probably address this by -
  
  * updating the cron command to use the dev URL (`drush -l example.dev civicrm-cron`), or
  * through Drupal's [`sites.php`](https://api.drupal.org/api/drupal/sites%21example.sites.php/7.x), or
  * symlink `sites/example.dev => sites/example.com` directory (\*ew).
  
  > **Comments:**
  > **username_0**: The cron job is actually calling it via URL though. I edited my question.  
  > **username_1**: This answer doesn't apply to your question then, sorry! Will leave it here for posterity & to ensure no-one repeats my error :)  
  
  ---
  
  