This short tutorial shows how to install MySQL on a Mac using Homebrew and how to verify it is running using Sequel Pro.
Install Homebrew
Homebrew is a package manager for Mac and serves a similar purpose to apt-get in Linux.
If you already have Homebrew, you can skip this section. If you are not sure, run the following command.
1 | which brew |
If you have Homebrew it will print something similar to /usr/local/bin/brew. If you do not have Homebrew it will print nothing.
Run the following command in a terminal window to install Homebrew.
1 | /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" |
Install MySQL
Update Homebrew to ensure you get the latest packages
1 | brew update |
Run this command to install MySQL
1 | brew install mysql |
Config MySQL
command not found
Type “mysql” command, you will meet the error:
1 | mysql -V |
1 | cd ~ |
Insert for .bash_profile
1 | export PATH=${PATH}:/usr/local/Cellar/mysql/8.0.12/bin |
Press “esc” and input “:wq” to save and quite.
Then
1 | source ~/.bash_profile |
Then config zsh
1 | vim ~/.zshrc |
接着我执行 vim ~/.zshrc
and insert source ~/.bash_profile
并在其中插入 source ~/.bash_profile
(纠结,我是一行英文一行中文的写呢?还是中英文分开成两篇呢?)
ERROR 2002 (HY000):
Type “mysql” command, you will meet the error:
1 | mysql -V |
The reason is that you’ll need to start MySQL before you can use the mysql command on your terminal.
1 | brew services start mysql |
Then mysql work
1 | mysql -V |
Set password
Set a rudimentary password for MySQL locally, there is no need to worry about security because it will only be accessible from your machine (I used “administrator”).
1 | mysqladmin -u root password 'administrator' |
Once installed and running, MySQL is accessible to any app running locally on your machine.
Then you can use the password you set previously to login MySQl:
1 | mysql -u root -p |
You can use the command to show the databases.
1 | mysql> SHOW DATABASES; |
Verify MySQL is running by Sequel Pro
I use Sequel Pro to manage MySQL because it has a pretty great UI. In case you are wondering, I am not actually affiliated with it in anyway.
Download and install Sequel Pro and open it. The database to connect to is on localhost so set “host” to “127.0.0.1”. Username is “root” and password the same as chosen in the previous step, for me that’s “administrator”.
If the connection does not work, make sure you modified your password correctly in the previous step and that the mysql service is still running.
Error 1
If you meet this error:
Find out which port MySQL is running on, run the following in MySql client:
1 | mysql> SHOW GLOBAL VARIABLES LIKE 'PORT'; |
Make sure that you input the correct port number (3306 or 3307).
Error 2
If you meet this error:
Error 2 - set ‘Use legacy password’
For MAC OS
- Open MySQL from System Preferences > MySQL > Initialize Database >
- Type your new password.
- Choose ‘Use legacy password’
- Start the Server again.
- Now connect the MySQL Workbench
Error 2 - install MySQL in System Preferences
If you don’t have MySQL in System Preferences,
you can set up MySQL in your System Preferences by following this: dev.mysql.com/doc/refman/5.7/en/osx-installation-prefpane.html
We can not install the MySQL Server again, because we already install it by brew. But you have to config the MySQL Preference Pane.
If we install the MySQL Server again, the MySQL Preference Pane config:
modify .bash_profile
1 | export PATH=${PATH}:/usr/local/mysql/bin |
Press “esc” and input “:wq” to save and quite.
Then
1 | source ~/.bash_profile |
Remeber to choose ‘Use legacy password’:
But i still do not solve it yet.
Connect and you will be presented with an empty MySQL.
At this point, you might want to add your first database. After successfully connecting, click Database > Add Database and name it, I chose the name “node_shoppingcart” (the MySQL naming convention is snake_case).
MySQL is ready to go!
If you are interested in using MySQL with Node.js, check out the setting up Node.js with a mysql tutorial.
Reference links: