[MySQL]: Install MySQL on Mac

SQL

10/02/2019


Installation Process

Download MySQL

Download MySQL Community Server from MySQL download & install the package.
When installing, remember the password!

Start the server

Open System Preference --> MySQL --> Start MySQL Server

download

Enable mysql command in terminal

Open .bash_profile with the following

BASH
$ vim ~/.bash_profile
# OR
$ open ~/.bash_profile

Add the following in .bash_profile

BASH
export PATH=${PATH}:/usr/local/mysql/bin/

Then, reopen the terminal to use mysql command

In terminal, run:

BASH
$ mysql -u root -p

Then, type in the password you've set during installation.

TIP: Consider creating alias for command from this post

Try show databases

TEXT
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)

If it doesn't work, run the following

BASH
ALTER USER 'root'@localhost' IDENTIFIED BY 'NEWPASSWORD';

Change mysql_native_password for Node.js mysql connection

BASH
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'

root: user name
localhost: your URL
password: your password

Set sql_mode='';

BASH
SET @@global.sql_mode='';

Avoids some errors instead of showing warnings

Setting a new password

In MySQL Terminal, type in the following:

BASH
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'NEW_USER_PASSWORD';
mysql> FLUSH PRIVILEGES;

Change root to other user name when needed.
FLUSH PRIVILEGES tells the server to reload the grant tables.

If ALTER USER doesn't work, modify the user table with

BASH
mysql> UPDATE mysql.user SET authentication_string = PASSWORD('NEW_USER_PASSWORD')
WHERE User = 'user-name' AND Host = 'localhost';
FLUSH PRIVILEGES;

Lost mysql password?

Use FLUSH PRIVILEGES when you're logged into MySQL terminal. Otherwise:

BASH
$ mysql -u root
mysql> USE mysql;
mysql> UPDATE user SET password=PASSWORD("NEW_USER_PASSWORD") WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> quit

WRITTEN BY

Keeping a record