Setup: Install MySQL Server and Workbench
Install MySQL 8.4 with Homebrew, secure it with a root password, and connect to it from MySQL Workbench.
Novice · Operating System · MySQL · Updated
After this lesson you can
- Install, start, and secure a local MySQL server
- Connect to a database server from MySQL Workbench
macOS only. A database server stores data and answers questions about it. In this bootcamp you will use MySQL, version 8.4, a long-term support release. MySQL Workbench is the app you will use to look at your databases and run SQL.
Install MySQL Server
brew install mysql@8.4
Homebrew keeps versioned packages like this one off your path, so your terminal cannot find the mysql command yet. Add it to your terminal's settings file:
echo 'export PATH="$(brew --prefix)/opt/mysql@8.4/bin:$PATH"' >> ~/.zshrc
Close the Terminal window and open a new one.
Start MySQL
brew services start mysql@8.4
You should see Successfully started mysql@8.4. MySQL now runs in the background, and starts again every time you log in.
Set a root password
MySQL's main user is called root. Homebrew installs MySQL with no password for root, so give it one now:
mysql_secure_installation
Answer the questions like this:
| Question | Answer |
|---|---|
| Would you like to setup VALIDATE PASSWORD component? | n |
| New password / Re-enter new password | A password you will remember. Nothing appears while you type it. |
| Remove anonymous users? | y |
| Disallow root login remotely? | y |
| Remove test database and access to it? | y |
| Reload privilege tables now? | y |
This database only runs on your own computer, for practice. Choose a password you can remember and write it down. Do not reuse a password from your email or bank.
Check the server
Sign in to MySQL from the terminal. Enter your new root password when asked:
mysql -u root -p
The prompt changes to mysql>. Type this and press Return:
SELECT VERSION();
You should see a small table with a version that starts with 8.4. Type exit and press Return to leave MySQL.
Install MySQL Workbench
brew install --cask mysqlworkbench
Open MySQL Workbench from Applications. Under MySQL Connections, you should see a tile for your local server (for example, Local instance 3306).
If there is no tile, click the + beside MySQL Connections. Set Connection Name to Local, Hostname to 127.0.0.1, Port to 3306, and Username to root, then click OK.
Click the tile and enter your root password. A query tab opens. Type SELECT VERSION();, then click the lightning bolt button above it to run it. The version appears in the results below.
Common problems
| What you see | What to do |
|---|---|
zsh: command not found: mysql |
Open a new terminal window. If it still fails, check that the export PATH line is in ~/.zshrc (run code ~/.zshrc). |
Can't connect to local MySQL server through socket |
MySQL is not running. Run brew services start mysql@8.4, wait ten seconds, and try again. |
Access denied for user 'root'@'localhost' |
The password is wrong. Type it carefully: nothing shows while you type. |
| You forgot the root password | On a new install, the quickest fix is to start again. Run brew services stop mysql@8.4, brew uninstall mysql@8.4 and rm -rf "$(brew --prefix)/var/mysql", then repeat this exercise from Install MySQL Server. This deletes every database on the server. |