본문 바로가기
# Coding/SQL

MySQL 기초 - MySQL 설치 및 초기 설정

by hxodoo.cookie 2023. 11. 28.

 

MySQL은 오픈 소스 기반의 관계형 데이터베이스 관리 시스템(RDBMS)으로, 다양한 웹 응용 프로그램 및 서버에서 데이터 저장 및 관리에 널리 사용됩니다. 이 글에서는 MySQL을 설치하고 초기 설정하는 방법을 자세하게 알아보겠습니다. 

 

 

 

MySQL 설치 및 초기설정

 

 

 

MySQL 설치 (macOS)

macOS 환경에서 MySQL설치방법은 MySQL Community Server dmg파일을 다운로드하여 설치하는 방법과 Homebrew로 MySQL 패키지 다운로드하여 설치하는 방법 두 가지가 있습니다.

 

이 글에서는 Homebrew를 사용하여 MySQL 설치하는 방법을 알아보겠습니다.

 

 

 

1. Homebrew를 이용한 MySQL 설치 방법

 

Homebrew 설치

먼저, macOS에서 Homebrew가 설치되어 있지 않다면 터미널에서 다음 명령어로 Homebrew를 설치합니다.

 

 

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

 

 

 

Homebrew가 이미 설치되어있다면, 버전 확인 후 업데이트를 진행합니다.

 

 

# Homebrew 버전 확인
brew -v

# Homebrew 업데이트
brew update

 

 

 

MySQL 설치

Homebrew를 사용하여 MySQL을 간편하게 설치합니다.

 

 

# MySQL 패키지 최신버전 설치 
brew install mysql

# MySQL 패키지 5.6버전 설치 
brew install mysql@5.6

 

 

 


2. MySQL 설치 확인 방법

 

 

MySQL이 제대로 설치되었는지 확인하려면 터미널에서 다음 명령어를 실행합니다.

 

 

# brew list를 통해 설치된 항목을 조회
brew list

 

 

brew list에 mysql이 있으면 설치 완료.

 

 

 

# mysql 버전 확인
mysql --version

 

 


3. MySQL 삭제 방법

 

 

만약 MySQL을 삭제하고 싶다면 Homebrew를 통해 간단하게 제거할 수 있습니다.

 

 

brew remove mysql

 

 

 

 


MySQL 초기 설정 방법

 

1. 환경변수 추가

 

MySQL 실행 파일에 빠르게 접근하기 위해 환경변수를 추가합니다.

 

 

# 환경 변수 추가
echo 'export PATH="/usr/local/opt/mysql/bin:$PATH"' >> ~/.zshrc  # or ~/.bashrc

# 환경 변수 적용
source ~/.zshrc  # or source ~/.bashrc

 

 

 


2. MySQL 서버 실행

 

MySQL 서버 시작

 

# MySQL 서버 실행
mysql.server start

Starting MySQL
 SUCCESS!

 

 

 

MySQL 서버 중지

 

# MySQL 서버 중지
mysql.server stop

Shutting down MySQL
 SUCCESS!

 

 

 


3. MySQL 보안 강화를 위한 설정

 

기본적인 보안을 강화 

MySQL 비밀번호 설정 및 기타 보안 설정을 수행하기 위해서는 mysql_secure_installation 명령어를 실행해야 합니다.

 

 

# MySQL 기본적인 보안을 강화 설정을 하기 위해서 `mysql_secure_installation` 명령어를 입력
mysql_secure_installation

 

 

 

mysql_secure_installation

mysql_secure_installation: [ERROR] unknown variable 'default-character-set=utf8'.
Securing the MySQL server deployment.
Enter password for user root:

 

 

 

 

비밀번호 복잡성 유무

 

비밀번호를 복잡하게 설정할 것인지, 쉬운 비밀번호를 사용할 것인지에 대한 설정 y/n

 

Y: 복잡한 비밀번호 설정
N: 쉬운 비밀번호 설정

 

 

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: No

 

 

 

 

비밀번호 설정

 

New password:

Re-enter new password:

 

 

 

 

사용자 설정 

 

익명의 사용자 제거에 대한 설정 y/n

 

Y : 접속하는 경우 mysql -u root처럼 -u 옵션 필요
N : 접속하는 경우 mysql처럼 -u 옵션 필요 없음

 

 

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) : Yes

 

 

 

 

▶ 원격 접속 설정

 

다른 IP에서 root 계정으로 원격접속을 허용하는지에 대한 설정 y/n

 

Y: 원격접속 불가능
N: 원격접속 가능

 

 

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) : No

 

 

 

 

TEST 데이터베이스 설정

 

TEST 데이터베이스에 대한 설정 y/n

 

Y: Test 데이터베이스 제거
N: Test 데이터베이스 유지

 

 

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) : yes

- Dropping test database...
Success.

 - Removing privileges on test database...
Success.

 

 

 

 

▶ 변경된 권한을 테이블에 적용여부 설정

 

변경된 권한을 테이블에 적용하는 것에 대한 설정 y/n

 

Y: 적용
N: 미적용

 

 

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) : yes

 

 

 

 

▶ 설정 완료

 

All done!

 

 

All done! 이 뜨면 모든 설정 완료.

설정을 다 끝냈다면 아래 명령어로 mysql을 재시작해줍니다.

 

 

sudo /etc/init.d/mysql start

 

 

 


4. 사용자 설정

 

새로운 사용자 생성

 

CREATE USER '새로운사용자'@'localhost' IDENTIFIED BY '비밀번호';

 

 

 

권한 부여

 

GRANT ALL PRIVILEGES ON *.* TO '새로운사용자'@'localhost';
FLUSH PRIVILEGES;

 

 

 


5. MySQL DB 로그인, 로그아웃

 

MySQL 로그인

 

# 서버 실행 후 
mysql -u 사용자이름 -p
Enter password:

 

 

 

정상적으로 로그인이 되면 mysql> 나옵니다.

 

 

mysql -u root -p
Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.19 Homebrew

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>

 

 

MySQL 로그아웃

 

mysql> exit or quit
Bye

 

 

 


6.  MySQL 실행 절차

 

# MySQL 서버 시작 :
% mysql.server start

# MySQL DB 로그인 :
% mysql -u root -p
Enter password: 0000

# MySQL DB 로그아웃 :
mysql> exit 또는 quit

# MySQL 서버 종료 :
% mysql.server stop

 

 

 

 

 

 


지금까지 Homebrew를 이용해 MySQL을 손쉽게 설치하고 초기 설정하는 방법을 알아보았습니다. 환경변수 추가 및 보안 설정을 통해 안정적이고 보안이 강화된 MySQL 환경을 구축하여 안정적이고 효율적인 데이터 관리를 할 수 있습니다.