Ubuntu 22.04에 MySQL 설치하기

MySQL은 강력한 오픈 소스 관계형 데이터베이스 관리 시스템(RDBMS)으로, 다양한 응용 프로그램에서 데이터를 저장하고 관리하는 데 사용된다. 이 글에서는 Ubuntu 22.04에 MySQL을 설치하는 방법에 대해 알아본다.

MySQL 서버 설치

sudo apt update
sudo apt install mysql-server

설치 후 버전 체크를 해본다.

$ mysql --version
mysql  Ver 8.0.35-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))

MySQL 보안 설정

설치 후에는 MySQL 서버를 안전하게 구성해야 한다. 다음 명령어를 통해 MySQL 보안 스크립트를 실행한다.

sudo mysql_secure_installation

이 스크립트는 여러 보안 옵션을 설정하며, 루트 암호를 재설정하고 익명 사용자를 삭제하는 등의 작업을 수행한다.

$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.

Skipping password set for root as authentication with auth_socket is used by default.
If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.

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!

MySQL 서비스 관리

다음은 MySQL 서버를 시작, 중지 및 재시작하는 방법이다.

sudo systemctl start mysql     # MySQL 서버 시작
sudo systemctl stop mysql      # MySQL 서버 중지
sudo systemctl restart mysql   # MySQL 서버 재시작
sudo systemctl status mysql    # MySQL 서버 상태 확인

MySQL에 접속

설치된 MySQL 서버에 로그인한다.

mysql -u root -p
$ sudo mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
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>

MySQL 사용자 및 데이터베이스 생성

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

-- 모든 권한 부여 (관리자 권한)
GRANT ALL PRIVILEGES ON *.* TO '새사용자'@'localhost' WITH GRANT OPTION;

-- 변경사항 적용
FLUSH PRIVILEGES;

-- 새로운 데이터베이스 생성
CREATE DATABASE 새데이터베이스;

-- 생성한 데이터베이스 사용
USE 새데이터베이스;

참고 사이트

답글 남기기