Installing MySQL and Configuring MySQL with PHP on Mac OS X (10.8.x and 10.9.x)

Note: before proceeding, make sure the root, or super, user account is enabled.

Note: The dollar sign character ($) included in the following command line examples is used to indicate The Terminal’s prompt. It should not be typed in any of the directions listed below.

  1. Visit the Download MySQL Community Server section of the Oracle web site and download mysql-5.6.15-osx10.7-x86_64.dmg, which is the 64-bit version of MySQL 5.6.x for Mac OS X 10.7 or higher, or download the file directly from mysql-5.6.15-osx10.7-x86_64.dmg.
  2. You don’t need to open an account at Oracle to download MySQL. Click “No thanks, just start my download” to continue.
    Figure 1: Highlighting the section of the MySQL download page that allows users to bypass registration.
  3. Double-click the mysql-5.6.13-osx10.7-x86_64.dmg disk image file.
  4. Choose mysql-5.6.13-osx10.7-x86_64.pkg and use your mouse’s secondary click option (right-click for righties; left-click for lefties) and choose the first option from the menu: “Open.” (The MySQL.prefPane and MySQLStartupItem.pkg files won’t be used in this tutorial, but, feel free to install them if you want a GUI for starting and stopping MySQL.)
    Figure 2: Placing focus on the mysql-5.6.13-osx10.7-x86_64.pkg file.
  5. Launch The Terminal.
  6. Start MySQL. You’ll need your root password for this step.
    $ sudo /usr/local/mysql/bin/mysqld_safe
  7. Your Terminal’s prompt should be hanging.
  8. Open another Terminal window: command + N
  9. Make sure you’re home.
    $ cd
  10. Set the root password, replacing YOUR_PASSWORD with your own password.
    $ sudo /usr/local/mysql/bin/mysqladmin -u root password 'YOUR_PASSWORD'
  11. Your password is now sitting in a plain text file called .bash_history, which is in your home folder. Type the following to clear it:
    $ history -c
  12. Verify your having cleared the file.
    $ history
    You should not see your password in the output.
  13. Log in to MySQL as the root user.
    $ /usr/local/mysql/bin/mysql -u root -p
  14. Enter your password. Your output should look very similar to
    Welcome to the MySQL monitor.   Commands end with ; or \g.
    Your MySQL connection id is 1
    Server version: 5.6.15 MySQL Community Server (GPL)
    Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  15. Exit MySQL.
    mysql> exit
  16. Navigate to the etc folder.
    $ cd /etc
  17. A file called php.ini.default resides in the etc folder. We’ll need to rename it to php.ini so that we can configure MySQL to work with PHP.
    $ sudo cp php.ini.default php.ini
  18. Assume the super user, or root, identity. You’ll need your rootpassword.
    $ su
  19. Note the permissions (likely -r--r--r--) on php.ini.
    $ ls -la php.ini
  20. We’ll change permissions on the file temporarily so we can edit it. (The file php.ini and its enclosing folder etc belong to the OS, and, by default, cannot be modified by any non-super user. Hence the permission change.)
    $ chmod 777 php.ini
  21. In this step, we’ll configure PHP with the three most familiar MySQL database interfaces: PDO, MySQLi, and MySQL. Launch emacs on php.ini to start.
    $ emacs php.ini
  22. First, find the entry pdo_mysql.default_socket, which might appear at or around line 986. In Mac OS X, version 10.8.x, the line looks like
    pdo_mysql.default_socket = /var/mysql/mysql.sock
    and in version 10.9.x, it looks like
    pdo_mysql.default_socket =
    In both cases, the entry needs to look like
    pdo_mysql.default_socket = /tmp/mysql.sock
  23. Do the same for mysql.default_socket and mysqli.default_socket. Both should be modified to look like
    mysql.default_socket = /tmp/mysql.sock
    mysqli.default_socket = /tmp/mysql.sock
  24. Save the file. Type control + x s. That is, hold the control key down and simultaneously type x followed by s.
  25. Exit emacs. Type control + x c: hold the control key down and simultaneously type x followed by c.
  26. Change the permissions on php.ini back to 444.
    $ chmod 444 php.ini
  27. Restart the web server.
    $ apachectl restart
  28. Stop MySQL.
    $ /usr/local/mysql/bin/mysqladmin -u root -p shutdown
  29. Rescind the super user role.
    $ exit
  30. And finally, a note on character sets. If you know — or even think — you might work with characters outside the English language, then it’s a good idea to set your MySQL server, database, client, and connection character sets now. Create a file called my.cnf and place it in /etc. In it, put the following:
    collation-server = utf8_unicode_ci
    init-connect = 'SET NAMES utf8'
    character-set-server = utf8
  31. Log back in to MySQL and type status once you’re logged in. Your Server, Db, Client, and Conn settings should be set to utf8.