Install MongoDB, MySQL, and Postgres using Homebrew

Install MySQL, PostgreSQL, or MongoDB in less than five mins using Homebrew Services.

Install MongoDB, MySQL, and Postgres using Homebrew

Step 0 - Install homebrew

Homebrew is a package manager that simplifies all database installs and management on a Mac.

=> Learn more on how to install Homebrew <=


Install Services

Note: brew search <package name> is your friend. If you want to know the latest package version, "brew search "can help.

Example: MariaDB

Search for the latest supported package.

brew search mariadb

Install package.

brew install mariadb

Example: MySQL

Find the latest supported package within Homebrew.

brew search mysql

In this example, install mysql@5.7.

brew install mysql57

Start service.

brew services start mysql57

Time to create a symlink.

brew link --force mysql@5.7

If done correctly, the terminal will tell you to run something like this.

Note: Your path will likely be personalized for you.

Do not blindly copy this.
echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> /Users/<My User Name>/.bash_profile

Test that things are looking good.

which mysql

Example: Postgres

brew search postgres
brew install postgres

Create a" postgres "user with these options.
"-d' "allows the user to create databases. "-P "prompts to enter a password. "-s' "states the new user will be a superuser.

createuser -d -s -P postgres

More options available within Docs

Start service.

brew services start postgresql

Example: MongoDB

Install the community-edition version of MongoDB.

brew tap mongodb/brew
brew install mongodb-community

Start service.

brew services start mongodb-community

Step 2 - Database management

Homebrew can download, install, and manage 3rd party libraries. Here are a few helpful commands for managing these locally-installed databases.

Read more about Homebrew Services.


YOU'RE DONE!


Troubleshooting

MySQL Errors

If you see this error, it can either mean that there are permission issues or things that did not install correctly.

MySQL socket error

Learn to Read the Error Logs

I generally suggest we first look at the error logs and see if we can better understand the problem.

The error log can be found here:

nano /usr/local/var/mysql/[look for your username].err 

Common MySQL Errors

I've seen a lot of errors in my lifetime, and when it comes to MySQL, here are a few more common ones.


Last Resort

If you can't make sense of the error log, one option is to uninstall MySQL and re-install everything.

Uninstall MySQL

brew uninstall mysql

# Clean things up
brew cleanup --force

# Remove any associated files or folders
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

# Remove any plists lingering around
rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

# Remove the MySQL folder with recursion and force
sudo rm -rf /usr/local/var/mysql

Reinstall MySQL

brew install mysql

# Check your installation
ps aux | grep mysql 

# Initialize the MySQl daemon 
mysqld --initialize --explicit_defaults_for_timestamp

# Start the MySQL server (don't use sudo)
mysql.server start 

This Stackoverflow Question offers more alternatives to uninstalling MySQL.