How to Install MySQL and Create a Sample Database  | Towards Data Science

Data scientists and analyst are expected to be able to write and execute complex queries in SQL. If you’re just getting started with SQL or are looking for a sandbox to test queries, then this guide is for you.
There are some great resources for SQL such as HackerRank, LeetCode, and W3Schools, but I think one of the best ways to gain proficiency is to practice with your own database with your SQL editor of choice.
In this guide, we’ll be walking through the following steps:
Installing MySQL on macOS
Adding the MySQL shell path
Creating a user account
Creating a sample database with employee data
Writing SQL queries
MySQL is the most popular Open Source SQL database management system and is developed by Oracle Corporation. The 2020 Developer Survey by Stack Overflow confirms this claim in terms of popularity as seen below.
We’ll be installing MySQL Community Server 8.0.x using a native package which is located inside a disk image (.dmg).
Download the .dmg version from here (find DMG Archive). This version will initialize the data directory and create the MySQL grant tables.
After clicking the Download button, you’ll be taken to a page where you’ll be asked to ‘Login’ or ‘Sign Up’ for a free account. You can bypass by clicking No thanks, just start my download.
Go to the downloaded file, right click and Open.
Follow the instructions until you reach Configuration.
For Configuration, select Use Strong Password Encryption which is the default. Click here to read more about MySQL passwords.
Enter a password for the root user. The root account is the default superuser account that has all privileges in all of your MySQL databases.
MySQL is now installed. If you open System Preferences, you should see MySQL in your panel as seen below.
The MySQL preference pane enables you to start, stop, and control automated startup during boot of your MySQL installation.
Open it and clickStart MySQL Server if an instance is not already running. The green dots indicate that the server is running.
I personally leave this box unchecked to save memory Start MySQL when your computer starts up.
Just remember to start the server after rebooting.
The shell path for a user in macOS is a set of paths in the filing system whereby the user has permissions to use certain applications, commands and programs without the need to specify the full path to that command or program in the Terminal.
The following steps will enable us to enter the command mysql in any working directory in the command line (Terminal).
Note that zsh (Z shell) is the default shell for macOS Catalina. If you’re on a different version, you can try using the bash command below.
Open Terminal (⌘+Space, type in Terminal)Once you’re in Terminal, type cd to go to the home directoryIf you’re using zsh, type nano .zshrcIf you’re using bash, type nano .bash_profileCopy and paste these two aliases:
Open Terminal (⌘+Space, type in Terminal)
Once you’re in Terminal, type cd to go to the home directory
If you’re using zsh, type nano .zshrcIf you’re using bash, type nano .bash_profile
Copy and paste these two aliases:
alias mysql=/usr/local/mysql/bin/mysqlalias mysqladmin=/usr/local/mysql/bin/mysqladmin
5. Save the file control + O, confirm with Enter, and control + X to exit.6. Quit (⌘+Q)Terminal and reopen it
To test the server, enter the following command (you’ll need to enter the password you created when installing MySQL):
mysqladmin -u root -p version
You may not want to be using the root account all the time. You can create various accounts and grant different levels of privileges. Here are the steps:
Log in as the root user:
mysql -u root -p
In the following command, replace userand password with your username and password of choice. I recommend you create an account using the same name as your macOS system username.
CREATE USER ‘user’@‘localhost’ IDENTIFIED BY ‘root-password’;
The following statement will grant all privileges to a user account on all databases. Replace user with the username you chose. Use quotes (‘’).
GRANT ALL ON *.* TO ‘user’@‘localhost’ WITH GRANT OPTION;
Try logging in with the newly created user. First, type QUIT to end the current session and log in with the new credentials. For example:
mysql -u miguel -p
Tip: Because ‘miguel’ is also the same name as my system username, I can simply type mysql -p an omit the -u miguel part.
TypeQUIT, but stay in Terminal and move on to the next section.
The Employees sample database was developed by Patrick Crews and Giuseppe Maxia and contains 4 million records. It contains fake employee data such as salaries, names, job titles, etc. Here is the schema:
First, go to the Employees database on GitHub to download the repo.
Download the repo by clicking on Code 👉 Download ZIP.
In Terminal, change to the directory where you saved the file. In my case:cd Downloads
Unzip the file by running this: unzip test_db-master.zipIn case that doesn’t work, you can manually open the file test_db-master.zip in Finder.
Change directory to the unzipped folder:cd test_db-master
Now you’re ready to install the database. Type in the following command (replace user with your own username).
mysql -u user -p < employees.sql
To test the installation, run the following command (replace user).
mysql -u ‘user’ -p < test_employees_md5.sql
The notebook below contains several simple queries to get you started.
You can also view the notebook on Jupyter Nbviewer:
nbviewer.jupyter.org
If you’re interested in running SQL queries in Jupyter, check out my guide on how to do it:
medium.com
Please let me know if you have any questions or comments. Thanks!