Drush can't connect to MySQL server on localhost using WinNMP on Windows

First time I setup and test Drush on my local machine using WinNMP on Windows executing a cache clear on a local Drupal site under Git BASH or cygwin:

drush @webfoobar cache-rebuild

... I get this error:

No Drupal site found, only 'drush' cache was cleared

I excuted the following drush command to investigate the cause of the error:

drush @webfoobar -d st

... got the following output message:

Solution to "The filename, directory name, or volume label syntax is incorrect." error in Drush custom PHP path

It is possible to assign a specific PHP version to execute Drush commands for special Drupal site via Drush aliases configuration file. However in Windows, we can get error:

The filename, directory name, or volume label syntax is incorrect.

… if the we follow the Windows syntax path.

This error is printed from C:\composer\vendor\drush\drush\includes\exec.inc


How to use your custom php.ini in Drush

The following codes shows how can we tell Drush to use our custom php.ini:

$aliases['wf'] = array (
  'uri' => 'http://d8webfoobar.loc',
  'root' => 'C:/WinNMP/WWW/drupal',
  'php' => 'C:/WinNMP/bin/PHP/64bit-php-7.2/php.exe',
  'php-options' => '--php-ini=C:/WinNMP/conf',

To test, execute the following command:

drush @wf status

The screenshot is the output of the command above showing that the custom php.ini is already recognized by Drush:


Get Drush to run Drupal cron regularly using crontab

Drush can Drupal cron without the need of accessing http://www.yoursite.com/cron.php?cron_key=<key>. This means we can safely remove or redirect cron.php to 404 Not Found in our .htaccess:

RewriteRule cron.php / [R=404,L]

To make our crontab script simple, we will use Drush's site aliases. Example if we are running multiple sites in our server: yoursite1.com, yoursite2.com and yoursite3.com we can assign alias @site1, @site2 and @site3 respectively. To do this: