SW/리눅스

Linux : Ubuntu 22.04 : MySQL 설치 방법, 예제, 명령어

얇은생각 2024. 1. 22. 07:30
반응형

MySQL은 가장 인기 있는 오픈 소스 관계형 데이터베이스 관리 시스템 중 하나입니다. 그것은 빠르고, 관리하기 쉽고, 확장 가능하며, 인기 있는 LAMP LEMP 스택의 필수적인 부분입니다. MySQL MacOS, Linux Windows를 포함한 모든 플랫폼에서 실행될 수 있습니다.

이 문서에서는 MySQL 버전 8.0 Ubuntu 22.04 머신에 설치하고 보안하는 방법에 대해 설명합니다. 완료되면 프로젝트에 사용할 수 있는 완벽한 기능의 데이터베이스 서버가 제공됩니다.

 

 

Linux : Ubuntu 22.04 : MySQL 설치 방법, 예제, 명령어

 

 

전제조건

이 가이드를 따르려면 sudo 권한을 가진 사용자로 로그인해야 합니다.

 

 

Ubuntu MySQL 설치

이 기사를 작성할 당시 Ubuntu 22.04 저장소에서 사용할 수 있는 MySQL의 최신 버전은 MySQL 버전 8.0입니다.

로컬 패키지 인덱스를 업데이트하는 것으로 시작합니다:

sudo apt update

 

 

다음을 입력하여 MySQL을 설치할 수 있습니다:

sudo apt install mysql-server

 

 

설치가 완료되면 MySQL 서비스가 자동으로 시작됩니다. MySQL 서버가 실행 중인지 확인하려면 다음을 입력하십시오:

출력에는 서비스가 활성화되고 실행 중임이 표시되어야 합니다:

sudo systemctl status mysql

# The output should show that the service is enabled and running:
# 
# ● mysql.service - MySQL Community Server
#      Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
#      Active: active (running) since Wed 2023-11-29 16:50:28 UTC; 49s ago
#     Process: 2238 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
#    Main PID: 2246 (mysqld)
#      Status: "Server is operational"
#       Tasks: 38 (limit: 2220)
#      Memory: 365.3M
#         CPU: 1.199s
#      CGroup: /system.slice/mysql.service
#              └─2246 /usr/sbin/mysqld
#      ...

 

 

어떤 이유로 서버가 시작되지 않을 경우 서버 로그를 확인하여 문제를 해결할 수 있습니다:

sudo journalctl -u mysql

 

 

 

MySQL 보안

MySQL 설치에는 데이터베이스 서버 보안을 개선할 수 있는 mysql_secure_install이라는 스크립트가 함께 제공됩니다.

인수 없이 스크립트를 호출합니다:

sudo mysql_secure_installation

 

 

MySQL 사용자의 암호 강도를 테스트하고 보안을 개선하는 데 사용되는 VALIDE Password PPLING을 구성하라는 메시지가 표시됩니다:

MySQL 서버 배포 보안 중입니다.

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

 

 

암호 유효성 검사 정책에는 낮음, 중간 및 강함의 세 가지 수준이 있습니다. 유효성 검사 암호 플러그인 또는 다음 단계로 이동할 다른 키를 설정하려면 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: 1

 

 

다음으로 익명 사용자를 제거하고 로컬 시스템에 대한 루트 사용자 액세스를 제한하며 테스트 데이터베이스를 제거하고 권한 테이블을 다시 로드해야 합니다. 모든 질문에 대해 y라고 대답해야 합니다.

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!

 

 

루트로 로그인

MySQL 서버 패키지의 종속성으로 설치된 MySQL 클라이언트 유틸리티를 사용하여 명령줄에서 MySQL 서버와 상호 작용할 수 있습니다.

MySQL 8.0에서는 루트 사용자가 기본적으로 auth_socket 플러그인에 의해 인증됩니다.

auth_socket 플러그인은 로컬 호스트에서 유닉스 소켓 파일을 통해 연결하는 사용자를 인증합니다. 이는 암호를 제공해서 루트로 인증할 수 없음을 의미합니다.

MySQL 서버에 루트 사용자로 로그인하려면 다음을 입력합니다:

아래와 같이 MySQL 셸이 제공됩니다:

sudo mysql

# You will be presented with the MySQL shell, as shown below:
# 
# Welcome to the MySQL monitor.  Commands end with ; or \g.
# Your MySQL connection id is 10
# Server version: 8.0.35-0ubuntu0.22.04.1 (Ubuntu)
# 
# Copyright (c) 2000, 2023, 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>

 

 

pphMyAdmin과 같은 외부 프로그램을 사용하여 MySQL 서버에 루트로 로그인하려면 두 가지 옵션이 있습니다.

첫 번째 옵션은 auth_socket에서 mysql_native_password로 인증 방법을 변경하는 것입니다.

다음 명령을 실행하여 이를 수행할 수 있습니다:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'very_strong_password';
FLUSH PRIVILEGES;

 

 

두 번째 권장 옵션은 모든 데이터베이스에 액세스할 수 있는 새 전용 관리 사용자를 만드는 것입니다:

GRANT ALL PRIVILEGES ON *.* TO 'administrator'@'localhost' IDENTIFIED BY 'very_strong_password';

 

 

결론

Ubuntu 22.04 MySQL을 설치하는 방법을 보여드렸습니다. 데이터베이스 서버가 가동 중이므로 다음 단계는 MySQL 사용자 계정과 데이터베이스를 관리하는 방법을 배우는 것입니다.

반응형