DB access blocked when initializing WP externally

I accidentally stumbled into the answer today when I forgot to turn on my MAMP managed mysql server. At this juncture it pointed me to the critical difference. This answer is phrased for anyone running MAMP but potentially has broader applicability (certainly LAMP, WAMP, etc.); if you’re experiencing the same problem in an non-MAMP environment than these steps may still apply but the explicit directories would not:

  • When WordPress loads normally it looks in /Applications/MAMP/tmp/mysql/mysql.sock but when run from the command line it instead looks for the socket file in /var/mysql/mysql.sock.
  • In order to allow for both execution environments (aka, normal WP and my command-line “include”) you just need to create a symbolic link:

    ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock

  • You may need superuser privileges, in which case just add sudo:

    sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock

Hope that helps.