Installing MySQL on Mac OS X El Capitan

A few months ago, I wrote about how to install MySQL on Windows with IIS. Now I want to show you how to create an AMP web programming stack (Apache, MySQL & PHP) on Mac OS X. As with Windows, you could just go with a ready-made development environment like MAMP but, if you’re going to be doing real database and web design work with MySQL, it’s best to know how everything works.

Background

For this demonstration, I’m going to be using Mac OS X El Capitan (v.10.11), the latest version as of this writing.  My installation environment is a hosted Mac server with 1 GB of RAM and 40 GB of disk space. I will be demonstrating the process using the Terminal commands.

Unlike Windows, OS X already includes the Apache web server and PHP language pre-installed so it’s a much simpler process to ensure that everything is working together. You will need root access in order to install and configure the components.

Establishing Root Access

Open the Terminal environment from the Utilities (Go >> Utilities from top menu and then select Terminal).

In OS X, it is possible to get root access by activating the root user account with dsenableroot. Improper use of this command can open up your system to security issues, however. It’s safer to use sudo (superuser do) on the individual commands or temporarily switch users with sudo su –.

For more information on working with the root user, see https://support.apple.com/en-us/HT204012.

Activating Apache

You can activate Apache simply by issuing either of the the following commands in the Terminal.

sudo apachectl start
sudo apachectl restart

The restart command will start Apache even if it’s stopped.  There won’t be any notification on the command line as to the change in status so you will need to verify that Apache is working by opening a web browser window and navigating to the following address:

http://localhost

If everything is working, you should get a simple white screen with the heading “It works!” indicating that the web server is up and running.

Mac OS X Apache confirmation screen
You can determine if Apache is running on OS X by navigating to http://localhost.

Configuring PHP and Apache

The Apache configuration file (httpd.conf) must be changed to load the necessary PHP module so that PHP pages can be served up.  This is easily done from the Terminal. First, you will need to change to the Apache directory and you should make a backup of the file.

cd /etc/apache2/
cp httpd.conf httpd.conf.bak

To edit the file, you can use the command line editor vi or the Mac TextEdit program, either of which can be opened from the Terminal.

vi httpd.conf
open -a TextEdit httpd.conf

If you choose the TextEdit command from above, the TextEdit program will open outside the Terminal and will load the configuration file for you to work with.

In either editor, you’ll need to find the following line:

#LoadModule php5_module libexec/apache2/libphp5.so

Remove the hashtag (#) from the beginning of the line to un-comment it and then save the file. You will then need to restart Apache with the following command.

sudo apachectl restart

To verify that PHP is working with Apache, you can create a phpinfo.php file in the web document root folder where the localhost files reside. On OS X, this folder is generally \Library\WebServer\Documents.  Create a simple text file in that folder with the following text on the first line:

<?php phpinfo(); ?>

Save the file with the name phpinfo.php and then load the file in your web browser.

http://localhost/phpinfo.php

If everything is working, you should then get the PHP settings screen.

PHP configuration page in Safari.
Creating a PHPInfo.php file to display the configuration page is a standard way of testing PHP.

Installing MySQL

The latest versions of MySQL Community Server are available in TAR and DMG format from http://dev.mysql.com/downloads/mysql. I recommend the DMG format which is a disk image that can be mounted as a drive accessible from your Desktop.

DMG image file mounted and opened to show MySQL PKG file.
MySQL Community Server can be downloaded from the MySQL site as a DMG image file.

The image file contains a single PKG file which you can double-click to run the installation wizard. As of this writing, the latest version, 5.7.11 is labeled for OS X 10.10 but I was able to install it with no problems on El Capitan (10.11). During the installation, you will be asked to create a password for the root MySQL user – make sure to write this  password down somewhere safe!

After the installation is finished, it’s a good idea to add the MySQL program directory to your PATH statement so you can run it from any directory. Use the following line in the Terminal to do this.

export PATH=/usr/local/mysql/bin:$PATH

Then, try logging in to your new MySQL server as the root user.

mysql -u root -p
A MySQL login and exit in the Mac OS X Terminal
Logging in to and exiting from MySQL.

It’s also a good idea to run mysql_secure_installation which does the following to secure your database server:

  1. Enables you to change the root password that was chosen during installation.
  2. Provides the option to remove the anonymous MySQL user.
  3. Provides the option to disable remote logins.
  4. Allows you to remove the test database and access to it.
  5. Reload the privilege tables

Now that MySQL is working, it needs to be able to work with PHP. This is done by creating a link to the MySQL socket file which will allow PHP to communicate with the server. Entering the following commands in the Terminal will create a new MySQL directory in the system Var directory and a symbolic link in that directory to the socket file.

cd /var 
mkdir mysql
cd mysql
ln -s /tmp/mysql.sock mysql.sock

When the MySQL installation is finished, you should be able to Access the MySQL preference pane shown in Figure 2.11 from the System Preferences panel. This pane enables you to start and stop your MySQL server and set it to start when the OS starts up.

009-controlpanel

Installing the Design Tools

You probably won’t want to manage all of your databases from the Terminal and OS X supports some of the same graphical tools as Linux and Windows.

MySQL Workbench

MySQL Workbench is available from http://dev.mysql.com/downloads/workbench as a DMG image file. Installing Workbench is as simple as opening the mounted DMG image and dragging the MySQL Workbench icon to the Applications folder when prompted. You will then be able to run the program from your Applications folder.

Installation screen for MySQL Workbench
Installation of MySQL Workbench

PHPMyAdmin

As a PHP application, PHPMyAdmin can also be installed within your new AMP environment, regardless of the operating system.

  1. Download the latest version from PHPMyAdmin.net. The package is available in ZIP format.
  2. UnZIP the files to a PHPMyAdmin subdirectory under your Localhost folder, usually \Library\WebServer\Documents.
  3. Copy the config.sample.inc.php file to config.inc.php and open the new file for editing.
  4. Look for the line that starts with $cfg[‘blowfish_secret’] = and add an encryption value of your choice. This value can be a series of random letters and numbers if you like.

If you would like to have a default user and password for phpMyAdmin, you can add the following lines to the $cfg['Servers'] section of the file, substituting your own values for the user name and password.

$cfg['Servers'][$i]['user']          = '<username>';
$cfg['Servers'][$i]['password']      = '<password>';

Once this is done, you should be able to run PHPMyAdmin simply by navigating to http://localhost/PHPMyAdmin.

For more information on installing PHPMyAdmin, see the documentation at https://phpmyadmin.readthedocs.org/en/latest/setup.html.

Sign up for our newsletter to receive updates about new projects, including the upcoming book "Self-Guided SQL"!

We respect your privacy and will never share your information with third-parties. See our privacy policy for more information.

×