Install MySQL on Mac Using DMG; Uninstall MySQL on Mac; MySQL is very straight forward to install on a Mac. If you do not already have the Homebrew package manager for Mac you’ll need to install that first. If you would rather not install Homebrew you can also install MySQL using the DMG file available on the MySQL site. Installing Homebrew. Download Homebrew for free. Open-source, free package manager for Mac and Linux. HomeBrew is a open source package manager for Mac and Linux systems to easily install programs that Apple or your Linux system did not provide. Consider supporting the project on their Patreon! Note: Homebrew will download and install Command Line Tools for Xcode 8.0 as part of the installation process. At this time of writing, Homebrew has MySQL version 8 as default, but as we're aiming to get 5.7, we'll need to append @5.7 to the default package key: Enter the following command: $ brew info [email protected].
Update 22 Oct 2019: Instructions updated for OSX 10.15 Catalina
After suffering some pretty bad issues with MAMP, I decided to set everything up with homebrew instead. The result was surprisingly a much faster and (in my opinion) easier to configure setup.
As a tl;dr, we’ll be setting up Homebrew MySQL and PHP and using OSX’s built in Apache.
In this tutorial I’m using the subl command which will open a file for editing in Sublime Text. If you don’t use Sublime Text, replace subl with nano or vi or any other app you use to edit text/config files.
![Download Download](https://tableplus.io/assets/images/dbngin/local-db-server.png)
Homebrew Setup
Homebrew is a package manager for OSX. It makes installation of a wide variety of useful apps super easy.
Installation instructions are on thehomebrew homepage but you can also just run the following:
MySQL
I lied! We’re installing MariaDB instead! At the time of writing MySQL version 8.0.11 has just changed its default authentication method to caching_sha2_password which isn’t supported in PHP. It’s a huge hassle so we’ll just use the drop-in replacement MariaDB instead.
Install and configure MariaDB.
Add the following to the end of the file to add support for large imports:
Make MySQL start when you log in:
The default installation comes with a passwordless root user. So secure it with:
![Download mysql for mac using brew Download mysql for mac using brew](/uploads/1/1/8/1/118142624/120954189.png)
SSL
Like all developers I like working on a custom subdomain – in this case localhost.com. We need to create a self-signed wildcard SSL certificate and get Chrome accepting it.
Create a folder /Users/your_username/Sites/certs and inside it run the following:
This should have created two files – server.crt and server.key which will be used in the apache config below to get HTTPS up and running.
But first, because this certificate is self-signed, it’ll result in a This site’s security certificate is not trusted! error in Chrome. That can be fixed through adding the cert to OSX’s keychain app.
- open /Applications/Utilities/Keychain Access.app (In OSX 10.15 this was only visible in Finder and not Terminal for some reason)
- Under Keychains at the top left click System
- Click File – Import Items and select your server.crt file
- Now in the list find your newly added cert, double click it, expand the Trust section and set everything to Always Trust
- These changes will only take effect after a browser restart.
Apache and PHP
OSX 10.15 Catalina comes (at the time of writing) with Apache 2.4.41.
To configure apache (with SSL):
gt;
SetHandler application/x-httpd-php
</FilesMatch>
SetHandler application/x-httpd-php
</FilesMatch>
# As with content, we want to load all site definitions from a Sites folder in our
# home directory. At the bottom replace the following:
# Include /private/etc/apache2/other/*.conf
IncludeOptional /Users/your_username/Sites/*.conf
# home directory. At the bottom replace the following:
# Include /private/etc/apache2/other/*.conf
IncludeOptional /Users/your_username/Sites/*.conf
# The error and custom logs too
CustomLog “/Users/your_username/Sites/logs/apache2/access_log” common
ErrorLog “/Users/your_username/Sites/logs/apache2/error_log”
CustomLog “/Users/your_username/Sites/logs/apache2/access_log” common
ErrorLog “/Users/your_username/Sites/logs/apache2/error_log”
# Uncomment to load the SSL config
Include /private/etc/apache2/extra/httpd-ssl.conf
Now configure the default SSL options:
Include /private/etc/apache2/extra/httpd-ssl.conf
Now configure the default SSL options:
Since this is a development machine, you’ll probably also want to enable the ever popular xdebug which luckily for us comes pre-compiled with OSX. What OSX doesn’t come with, however, is a default php.ini though it does have a sample file. We can use that:
Then simply add extension=xdebug.so below all the extension= lines in your new /etc/php.ini file.
VirtualHosts
I like to split virtualhosts up into one for each site and store them all in /Users/your_username/Sites/ folder.
Create a file /Users/your_username/Sites/mysite.localhost.com.conf and add the following:
Finally, restart apache and you should be good to go!
Resources
- Much of the content of this article came from David Marcus’s really great post Set up localhost on macOS High Sierra (Apache, MySQL, and PHP 7) with SSL/HTTPS.
- To get SSL certs working correctly in the browser I followed the instructions on Jed Schmidt’s gist How to set up stress-free SSL on an OS X development machine.
- Because I’m positive I’ve missed or incorrectly written at least some of this article I’ve uploaded copies of all relevant files. httpd.confhttpd-ssl.confmysite.localhost.com.conf
macOS Update: While these instructions still work, there are new posts for recent versions of macOS, the latest being Install Apache, PHP, and MySQL on macOS Mojave.
I have installed Apache, PHP, and MySQL on Mac OS X since Leopard. Each time doing so by hand. Each version of Mac OS X having some minor difference. This post serves as much for my own record as to outline how to install Apache, MySQL, and PHP for a local development environment on Mac OS X Mountain Lion Mavericks.
I am aware of the several packages available, notably MAMP. These packages help get you started quickly. But they forego the learning experience and, as most developers report, eventually break. Personally, the choice to do it myself has proven invaluable.
It is important to remember Mac OS X runs atop UNIX. So all of these technologies install easily on Mac OS X. Furthermore, Apache and PHP are included by default. In the end, you only install MySQL then simply turn everything on.
First, open Terminal and switch to
root
to avoid permission issues while running these commands.Enable Apache on Mac OS X
Note: Prior to Mountain Lion this was an option for Web Sharing in System Prefrences → Sharing.
Verify It works! by accessing http://localhost
Enable PHP for Apache
OS X Mavericks Update: You will need to rerun the steps in this section after upgrading an existing install to Mac OS X Mavericks.
First, make a backup of the default Apache configuration. This is good practice and serves as a comparison against future versions of Mac OS X.
Now edit the Apache configuration. Feel free to use TextEdit if you are not familiar with vi.
Uncomment the following line (remove
#
):Restart Apache:
Install MySQL
- Download the MySQL DMG for Mac OS X
- Install MySQL
- Install Preference Pane
- Open System Preferences → MySQL
- Ensure the MySQL Server is running
- Optionally, you can enable MySQL to start automatically. I do.
The README also suggests creating aliases for
mysql
and mysqladmin
. However there are other commands that are helpful such as mysqldump
. Instead, I updated my path to include /usr/local/mysql/bin
.Note: You will need to open a new Terminal window or run the command above for your path to update.
I also run
mysql_secure_installation
. While this isn't necessary, it's good practice.Connect PHP and MySQL
You need to ensure PHP and MySQL can communicate with one another. There are several options to do so. I do the following:
Creating VirtualHosts
You could stop here. PHP, MySQL, and Apache are all running. However, all of your sites would have URLs like http://localhost/somesite/ pointing to /Library/WebServer/Documents/somesite. Not ideal for a local development environment.
OS X Mavericks Update: You will need to rerun the steps below to uncomment the vhost
Include
after upgrading an existing install to Mac OS X Mavericks.To run sites individually you need to enable VirtualHosts. To do so, we'll edit the Apache Configuration again.
Uncomment the following line:
Now Apache will load httpd-vhosts.conf. Let's edit this file.
Here is an example of VirtualHosts I've created.
The first
VirtualHost
points to /Library/WebServer/Documents
. The first VirtualHost
is important as it behaves like the default Apache configuration and used when no others match.The second
VirtualHost
points to my dev workspace and I can access it directly from http://jason.local. For ease of development, I also configured some custom logs.Note: I use the extension local. This avoids conflicts with any real extensions and serves as a reminder I'm in my local environment.
Restart Apache:
In order to access http://jason.local, you need to edit your hosts file.
Homebrew On Mac
Add the following line to the bottom:
I run the following to clear the local DNS cache:
Now you can access http://jason.local.
Note: You will need to create a new
VirtualHost
and edit your hosts file each time you make a new local site.A note about permissions
You may receive 403 Forbidden when you visit your local site. This is likely a permissions issue. Simply put, the Apache user (
_www
) needs to have access to read, and sometimes write, your web directory.If you are not familiar with permissions, read more. For now though, the easiest thing to do is ensure your web directory has permissions of
755
. You can change permissions with the command:In my case, all my files were under my local
~/Documents
directory. Which by default is only readable by me. So I had to change permissions for my web directory all the way up to ~/Documents
to resolve the 403 Forbidden issue.Note: There are many ways to solve permission issues. I have provided this as the easiest solution, not the best.
Install PHPMyAdmin
Homebrew Mac Download
Unless you want to administer MySQL from the command line, I recommend installing PHPMyAdmin. I won't go into the details. Read the installation guide for more information. I install utility applications in the default directory. That way I can access them under, in this case, http://localhost/phpmyadmin.
Mysql Mac Brew
Closing
Mac Homebrew Tutorial
A local development environment is a mandatory part of the Software Development Process. Given the ease at which you can install Apache, PHP, and MySQL on Mac OS X there really is no excuse.
Install Homebrew In Mac
Find this interesting? Let's continue the conversation on Twitter.