How to install MySQL on Mac

우선 여러분의 컴퓨터에 homebrew가 설치되어있어야합니다. homebrew에 대한 설치 및 사용방법은 여기를 클릭해서 숙지한뒤 다시 돌아와 MySQL설치를 진행해주시기 바랍니다.

homebrew를 이용해서 MySQL을 설치합니다.

brew install mysql

brew service를 이용해서 MySQL server 실행합니다.

$ brew services start mysql
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Enumerating objects: 1656, done.
remote: Counting objects: 100% (535/535), done.
remote: Compressing objects: 100% (392/392), done.
remote: Total 1656 (delta 229), reused 354 (delta 130), pack-reused 1121
Receiving objects: 100% (1656/1656), 481.42 KiB | 6.78 MiB/s, done.
Resolving deltas: 100% (705/705), done.
Tapped 1 command (44 files, 616.3KB).
==> Successfully started `mysql` (label: homebrew.mxcl.mysql)

MySQL server를 띄운상태에서 mysql_secure_installation라는 명령어를 실행할건데요. 이 명령어는 MySQL의 보안을 강화하는 명령어로써 다양한 옵션을 제공합니다. 여기서는 기본옵션으로 간단하게 실행만 할거에요. mysql_secure_installation에 대한 자세한 사항은 여기를 클릭해서 알아볼수 있습니다.

$ mysql_secure_installation
Securing the MySQL server deployment.

Enter password for user root:

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: y
There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.

New password:

Re-enter new password:

Estimated strength of the password: 50
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

보안을 강화한 설정을 서버에 적용하기 위해서는 서버를 다시 실행해야합니다. 일단 brew service를 이용해서 MySQL을 종료합니다.

$ brew services stop mysql
Stopping `mysql`... (might take a while)
==> Successfully stopped `mysql` (label: homebrew.mxcl.mysql)

brew services를 이용해서 서버를 띄우면 MySQL서버가 daemon mode로 뜨게 되는데요. 프로그램이 daemon mode로 떠있으면 컴퓨터가 재시작할때마다 자동으로 background에 MySQL을 띄워주는 거에요. 그런데 저는 컴퓨터가 꺼지면 MySQL도 꺼지고 제가 수동으로 띄우기 전까지는 자동으로 뜨지 않았으면 좋겠어요. 그래서 이번에는 mysql.server를 이용해서 띄워볼게요.

$ mysql.server start
Starting MySQL
. SUCCESS!

mysql.server를 이용해서 띄우면 컴퓨터가 꺼지거나 mysql.server stop을 실행할때까지 MySQL서버가 계속 실행되게 됩니다.

$ mysql.server stop
Shutting down MySQL
. SUCCESS!

다시 mysql.server start를 실행한뒤, 이번에는 MySQL에 접속을 해볼게요.

$ 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.27 Homebrew

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>

이번시간에는 이렇게 Local환경에 MySQL서버를 띄우는 방법을 배워보았습니다. 도움이 되셨기를 바래요. 바이!