Install MySQL on Mac & Configure MySQL in Terminal

Last Updated on by in Web Development
In this tutorial, we are going to learn how to install MySQL community server on Mac and setting up the MySQL in Terminal app.Mac OS doesn’t come along with the pre-configured version of MySQL, but this step by step guide will make the task easy for you. By the end of this tutorial, you will be able to set up the MySQL server on your Mac system for local development purposes.

Download MySQL Server

Before we start installing MySQL server on mac, we first download the latest version of MySQL from its official site.

Visit https://dev.mysql.com/downloads/mysql and choose the MySQL version that you want to download.

On this page, you will see list of operating systems under the “Select Operating System” dropdown choose the macOS from the list. Then, click on the download button for the MySQL .dmg file. You can download the other MySQL instances as per your operating system requirement.

Download MySQL Server

Install MySQL on Mac

You have to click on the MySQL file to install the MySQL on Mac system. You might get the warning “macOS cannot verify that this app is free from malware”.

To get rid from this issue got to System Preferences > Security & Privacy and click on the “Open Anyway” button. This will allow you to install the MySQL version in your system.

Once the MySQL installation is completed, go to System Preferences and click on the MySQL icon that you can see on the bottom left position.

Start MySQL on Mac

Here you can see the MySQL is already running and other configurations of MySQL. You can even stop the server by clicking on “stop MySQL server”. Well this is not the only way to start the server, we can even manually start the MySQL server via command line.

Install MySQL on Mac

Configure MySQL in Mac for Terminal App

To start the MySQL via terminal app, you need to use the following command.

mysql.server start

But this command will display the following error on your terminal screen.

“command not found: mysql.server”

To fix this issue, we have to define the MySQL path in the paths file. The easiest way to open the file is to type the following command in the terminal and provide the password.

sudo nano /etc/paths

Include one per line given below paths in the /etc/paths file.

/usr/local/mysql/bin

/usr/local/mysql/support-files

command not found: mysql.server

Next, type the command to start the MySQL server.

sudo mysql.server start

Here is the output we get when MySQL is started correctly.

sudo mysql.server start
Starting MySQL
. SUCCESS! 

We used `sudo` with mysql.server start otherwise it will throw permission errors.

Starting MySQL
./usr/local/mysql/bin/mysqld_safe: line 674: /usr/local/mysql/data/shivas-MBP.err: Permission denied
Logging to '/usr/local/mysql/data/shivas-MBP.err'.
/usr/local/mysql/bin/mysqld_safe: line 144: /usr/local/mysql/data/shivas-MBP.err: Permission denied
/usr/local/mysql/bin/mysqld_safe: line 199: /usr/local/mysql/data/shivas-MBP.err: Permission denied
/usr/local/mysql/bin/mysqld_safe: line 937: /usr/local/mysql/data/shivas-MBP.err: Permission denied
rm: /usr/local/mysql/data/shivas-MBP.pid.shutdown: Permission denied
/usr/local/mysql/bin/mysqld_safe: line 144: /usr/local/mysql/data/shivas-MBP.err: Permission denied
 ERROR! The server quit without updating PID file (/usr/local/mysql/data/shivas-MBP.pid).

Important MySQL Commands

Here are the important commands that are often used while working with MySQL.

Run the following command to stop mysql server:

sudo mysql.server stop

Type the below command to restart the MySQL server that is already running:

sudo mysql.server restart

To verify the current status of MySQL server:

mysql.server status

Access Root via Mac Terminal & Create Database

Now, we have reached to essential step of our tutorial. We are all set with the server configurations, now we have to access the MySQL root. When you run the following command it will ask for the password. So you have to type the password that you defined when installing the MySQL initially.

mysql -u root -p
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.20 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Now we will use the mysql query to create a new database.

mysql> CREATE DATABASE positrondb;

Query OK, 1 row affected (0.01 sec)

You can verify the newly created MySQL database.

SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| positrondb         |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

Use the newly created “positrondb” db by using the below command.

mysql> use positrondb

Database changed

Create a table in the MySQL database => ‘positrondb’.

CREATE TABLE IF NOT EXISTS `Users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(256) NOT NULL,
  `email` varchar(50),
  `age` int(11) NOT NULL,
  `mobile` VARCHAR(20),
  `created` datetime NOT NULL,
  PRIMARY KEY (`id`)
)ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=19;

Verify the table in the database.

mysql> show tables;

+----------------------+
| Tables_in_positrondb |
+----------------------+
| Users                |
+----------------------+

Summary

We have completed the MySQL tutorial, and In this tutorial, we learned how to install MySQL community server on Mac OS, How to access root user in the MySQL server, Create Database and Table.

I hope you liked this tutorial, dont forget to share it with others.