How to Install MySQL on Mac

How to Install MySQL on Mac

21/02/2021 Off By dat09

# Summary

How to Install MySQL on Mac & Configure MySQL in Terminal.

## 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.

After downloaded the MySQL .dmg file, then let’s start install on your MAC(very easy to install a package in Mac, i don’t show here).

Start mySQL server

ba_dat@devk2x ~ % sudo mysql.server start

command not found: mysql.server

## Fix “command not found: mysql.server” when start mysql in the first time

ba_dat@devk2x ~ % vim /etc/paths

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

/usr/local/mysql-8.0.24-macos11-x86_64/bin
/usr/local/mysql-8.0.24-macos11-x86_64/support-files

Save this file and restart mysql in next step

## Start mysql

ba_dat@devk2x ~ % sudo mysql.server start
Password:
Starting MySQL

## Access Root

ba_dat@devk2x ~ % mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.24 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

## Create New Database

mysql> CREATE DATABASE devk2xdb;
Query OK, 1 row affected (0.01 sec)

mysql> show DATABASES;
+--------------------+
| Database           |
+--------------------+
| devk2xdb           |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

## Create example table

Create table script

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;

Execute script in terminal

mysql> use devk2xdb;
Database changed
mysql> 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;
Query OK, 0 rows affected, 3 warnings (0.00 sec)

## Verify the table in the database

mysql> show TABLES;
+--------------------+
| Tables_in_devk2xdb |
+--------------------+
| Users              |
+--------------------+
1 row in set (0.00 sec)

Conclusion

Ở trên là các bước cài đặt mysql bằng file .dmg trên MacOS, ngoài ra bạn có thể cài bằng cách sử dụng brew ( brew install ..) .

Hiện tại đang sử dụng user root để tạo databases, bài sau mình sẽ hướng dẫn các bạn database trên một account khác user root.

Hy vọng bài viết giúp ích cho các bạn chưa cài mysql bao giờ, cảm ơn mọi người đã theo dõi các bài viết trên blog devk2x .