Installing WordPress on a Mac

Installing WordPress on a Mac without using MAMP was a difficult process. I haven’t installed MAMP on a Mac in a long time, but I preferred to use the built-in components instead installing and managing a separate set of applications. What follows is a description of how I got it to work.

My system:

  • Mac OS: High Sierra
  • MySQL version: 8.0.11
  • PHP version: 10.1.14

I started with a clean install of High Sierra and used the default Apache and PHP that come with the OS.  I followed the WordPress install instructions to download and set it up, creating a new MySQL user account for the WordPress database.  No problems.

When I ran the install.php script, it failed to connect to the database without giving me any additional information why.  The first thing I checked was the MySQL login and confirmed that I could connect to MySQL with that login and access the WordPress database.  My next step was to create a test PHP page to debug accessing mysql from PHP.  I initially used mysql_connect for this but  discovered that that is no longer supported.  I switched to mysqli_connect and edited wp-db.php to force it to use mysqli.  That failed too because I was using ‘localhost’ for the host name, which caused PHP to hang for a while and then fail because “MySQL server has gone away”.  I figured ‘localhost’ wasn’t being resolved correctly and changed to ‘127.0.0.1’.

Next the script failed with the following error:  “The server requested authentication method unknown to the client [caching_sha2_password]”.  This is a great example of an ambiguous, misleading error message.  It seemed to be saying that I requested an authentication method named “unknown”, but what it really meant is “The server requested an authentication method that is unknown to the client and that method name is caching_sha2_password.”  I am amazed that developers are still trying to save a few bytes here and there by using ungrammatical or cryptic error messages instead of opting for clarity.

After googling this error for a while I discovered that MySQL has recently changed their user authentication scheme to a new one that breaks legacy code like PHP.  There are a number of solutions to be found in StackOverflow (my favorite site on the internet), but the solution that worked for me was as follows.

  1. Create a mysql.cnf file with two lines.
    [mysqld]
    default_authentication_plugin = mysql_native_password
  2. Change the WordPress MySQL account to use mysql_native_password
    ALTER USER ‘xxx’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘whatever’;
  3. Use the Mac’s MySQL Preferences pane to tell MySQL to use this configuration file and restart MySQL.

Bingo – my install.php script ran correctly.

My lessons learned:

  • WordPress looks nice but it uses PHP, which is a POS.
  • Installing MySQL on a Mac is a big pain.  Attempting to install MariaDB is a bigger pain.
  • Don’t trust MySQL not to break your applications unexpectedly.

Here’s my PHP test page.

<?php

$host = '127.0.0.1';
$user = 'foobar';
$pswd = 'foobar';
$db = 'wordpress';

echo "\n\nConnecting to $db@$host with '$user'/'$pswd'\n";
$link = mysqli_connect($host, $user, $pswd, $db);

if (!$link) {
    die("\n*** Could not connect: " . mysqli_connect_error() . "\n");
}

echo "\n**** Connected successfully\n";
mysqli_close($link);
?>