M1 Mac 환경에서 MySQL 사용 환경 구축하기

kindof

·

2023. 6. 3. 14:20

이번 글에서는 M1 Mac 환경에서 MySQL을 설치하고 서버를 실행하는 방법을 정리해보려고 합니다.

 

1. Homebrew로 MySQL 설치

Homebrew는 macOS 전용 패키지 관리자로, 커맨드라인 도구나 시스템 패키지들을 설치하는 데 편의를 제공합니다.
 
M1 환경에서 개발하시는 분이라면 Homebrew가 설치되어 있겠지만, 혹시 깔려있지 않으신 분은 아래 명령어로 설치하실 수 있습니다.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

 
설치가 완료되면 아래와 같이 /opt/homebrew 경로에 관련 파일들이 생성된 것을 볼 수 있습니다.

/opt/homebrew

Homebrew는 전용 디렉토리에 패키지를 설치하고 /opt/homebrew 위치로 심볼릭 링크를 연결합니다. 심볼릭 링크에 대한 설명은 아래에서 자세히 보여드리겠습니다.
 
이제 MySQL을 설치해보겠습니다.

$ brew install mysql

==> Downloading https://formulae.brew.sh/api/formula.jws.json

==> Downloading https://formulae.brew.sh/api/cask.jws.json

==> Fetching mysql
==> Downloading https://ghcr.io/v2/homebrew/core/mysql/manifests/8.0.33-1
Already downloaded: /Users/irteam/Library/Caches/Homebrew/downloads/aed959d935ddb5dc0feebda024f795378a4954a89919edaaac4370ce94d91c02--mysql-8.0.33-1.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/mysql/blobs/sha256:ade87b6d6130e02940c52c10a490b99449a4c9aaf9e10c89f32c63411f3a0283
Already downloaded: /Users/irteam/Library/Caches/Homebrew/downloads/11862e48cc2fa6a4cabb12663caaa00daa64ee1ccad532b3acc8fe513360b32b--mysql--8.0.33.arm64_monterey.bottle.1.tar.gz
==> Pouring mysql--8.0.33.arm64_monterey.bottle.1.tar.gz
==> /opt/homebrew/Cellar/mysql/8.0.33/bin/mysqld --initialize-insecure --user=irteam --basedir=/opt/homebrew/Cellar/mysql/8.0.33 --datadir=/opt/homebrew/var/mysql --tmpdir=/tmp
Last 15 lines from /Users/irteam/Library/Logs/Homebrew/mysql/post_install.01.mysqld:
2023-06-02 23:56:47 +0900

...

로그를 보면 MySQL이 /opt/homebrew/Cellar/mysql 경로에 설치되는 것을 볼 수 있습니다.
 
실제로 설치가 완료된 후 해당 경로를 들어가보면 아래와 같이 MySQL 유틸리티를 위한 디렉토리인 bin, 라이브러리 파일들이 저장된 lib 디렉토리 등이 생성되어 있습니다.

/opt/homebrew/Cellar/mysql

 

2. MySQL 서버 실행하기

MySQL 서버는 아래 명령어를 통해 간단하게 실행시킬 수 있습니다.

~/Desktop/projects/mysql > sudo mysql.server start

Password:
Starting MySQL
 SUCCESS!

 
그런데 MySQL 서버를 실행할 때는 반드시 sudo 권한이 필요합니다. 
 
우선, MySQL 서버의 데이터 디렉토리는 /opt/homebrew/var/mysql 하위에 위치하는데 해당 파일들에 대한 읽기/쓰기 권한을 가지기 위해서는 서버 실행 시 sudo 권한이 필요하기 때문입니다.
 
또한, 로컬에서 MySQL 서버의 포트는 기본적으로 3306에 바인딩됩니다. 이러한 작업을 수행하기 위해서는 네트워크 포트에 대한 액세스 권한이 필요하기 때문에 sudo 권한이 필요합니다.

$ sudo lsof -i :3306

Password:
COMMAND   PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
mysqld  81955 _mysql   20u  IPv4 0x2e855709bdaf9095      0t0  TCP localhost:mysql (LISTEN)

 

한편, brew를 통해 MySQL을 설치하면 아래와 같이 $PATH에 /opt/homebrew/bin이 자동으로 추가됩니다.

$PATH

 

그리고 /opt/homebrew/bin에 존재하는 실행 파일들의 심볼릭 링크를 확인해보면, 우리가 실제로 사용하고 있는 /opt/homebrew/Cellar 하위의 mysql 실행 파일들에 연결된 것을 확인할 수 있습니다.

Symbolic link

따라서, 어느 디렉토리에서도 우리는 mysql.server 라는 실행 파일에 직접 접근할 수 있습니다.

 

3. MySQL 서버 기본 설정

MySQL 서버 실행 이후에 mysql_secure_installation 명령어를 통해 기본 설정을 진행할 수 있습니다.

$ mysql_secure_installation

Securing the MySQL server deployment.

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: n
Please set the password for root here.

New password:

Re-enter new password:
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) : n

 ... skipping.
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!

복잡한 패스워드가 필수가 아니도록 설정하고, 실제 사용할 비밀번호를 지정했습니다. 그리고 익명 사용자를 삭제하고, 원격 접속 차단, 테스트용 DB를 삭제했습니다.
 
 

4. 서버 접속하기

MySQL 서버가 실행된 상태에서 아래 명령어 실행 후 기존에 설정한 패스워드를 입력하면 서버에 접속할 수 있습니다.

$ mysql -uroot -p --host=127.0.0.1 --port=3306

서버 접속

 

저는 반복되거나 자주 사용되는 CLI는 이렇게 따로 스크립트로 관리하는 편입니다.
그래서 매번 위와 같은 명령어를 사용하기 귀찮기 때문에 아래와 같이 간단한 실행파일을 하나 만들어서 사용할 수도 있겠습니다.

실행 파일로 간단하게 접속하기

필요한 내용의 스크립트를 위와 같은 방식으로 만들어서 사용하시는 것을 추천드립니다.
 

5. 정리

이번 글에서는 M1 환경에서 MySQL 설치하는 방법과 이 과정에서 알고 있으면 좋을 부가적인 몇 가지 내용에 대해 소개했습니다.
 
정말 간단한 개발환경 구축이지만 꼬이기 시작하면 한도 끝도 없는 작업이 개발환경 구축인 것 같습니다.
 
긴 글 읽어주셔서 감사합니다.