본문 바로가기
프로그래밍/DevOps

Amazon Linux2 MySQL 8 Setup

by 개미뚠뚠이 2023. 1. 25.
728x90

설치

wget을 이용해 다운로드 & 압축해제

MySQL 최신버전 확인

https://downloads.mysql.com/archives/community/

 

MySQL :: Download MySQL Community Server (Archived Versions)

Please note that these are old versions. New releases will have recent bug fixes and features! To download the latest release of MySQL Community Server, please visit MySQL Downloads. MySQL open source software is provided under the GPL License.

downloads.mysql.com

MySQL 8.0.31 x86, 64-bit 의 경우

$ wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.31-1.el7.x86_64.rpm-bundle.tar
$ tar xvf mysql-8.0.31-1.el7.x86_64.rpm-bundle.tar

MySQL 8.0.31 ARM, 64-bit 의 경우

$ wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.31-1.el7.aarch64.rpm-bundle.tar
$ tar xvf mysql-8.0.31-1.el7.aarch64.rpm-bundle.tar

yum을 이용해 설치

$ sudo yum install mysql-community-*.rpm

systemctl을 이용해 MySQL 실행 및 확인

$ sudo systemctl start mysqld
$ systemctl status mysqld

설치파일 삭제

설치를 하면 다음과 같은 rpm 파일들이 생긴다.

mysql-8.0.31-1.el7.aarch64.rpm-bundle.tar                 mysql-community-icu-data-files-8.0.31-1.el7.aarch64.rpm
mysql-community-client-8.0.31-1.el7.aarch64.rpm           mysql-community-libs-8.0.31-1.el7.aarch64.rpm
mysql-community-client-plugins-8.0.31-1.el7.aarch64.rpm   mysql-community-libs-compat-8.0.31-1.el7.aarch64.rpm
mysql-community-common-8.0.31-1.el7.aarch64.rpm           mysql-community-server-8.0.31-1.el7.aarch64.rpm
mysql-community-debuginfo-8.0.31-1.el7.aarch64.rpm        mysql-community-server-debug-8.0.31-1.el7.aarch64.rpm
mysql-community-devel-8.0.31-1.el7.aarch64.rpm            mysql-community-test-8.0.31-1.el7.aarch64.rpm
mysql-community-embedded-compat-8.0.31-1.el7.aarch64.rpm

이제 쓸 일이 없으니 다음 명령어로 지운다.

$ rm mysql*

환경설정

Port 변경

기본 포트는 해킹의 위협에 노출되기 때문에 포트는 랜덤한 숫자로 변경해주는 것이 좋다.

환경설정 파일 위치 확인

$ mysql --verbose --help | grep my.cnf
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf

컴퓨터 환경에 따라 다를 수 있다. 가장 앞에 있는 /etc/my.cnf 를 수정해주면 된다.

$ sudo vi /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

log 파일의 위치를 알 수 있다.

아래에 다음와 같은 내용을 추가한 뒤 저장한다.

bind-address=0.0.0.0
mysqlx-bind-address=0.0.0.0
port=12345

bind-address = 0.0.0.0 은 모든 IP에 대해 접속을 허용한다는 뜻이고, port = 12345 는 12345 port를 사용하겠다는 의미이다.

character set encoding 변경

한글이 안 깨지게 하려면 character set을 utf8mb4로 변경해야 한다.

[client]
default-character-set=utf8mb4

[mysql]
default-character-set=utf8mb4

[mysqldump]
default-character-set=utf8mb4

[mysqld]
skip-character-set-client-handshake
init_connect="SET collation_connection = utf8mb4_unicode_ci"
init_connect="SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci"

character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci

max_heap_table_size, tmp_table_size 변경

Memory Engine을 사용한다면 max_heap_table_size를 키워야 많은 데이터를 저장할 수 있다.

tmp_table_size를 키워놓으면 GROUP BY 등 복잡한 연산의 성능이 좋아진다.

현재 적용된 설정은 다음의 방법으로 확인할 수 있다.

mysql> SELECT @@GLOBAL.max_heap_table_size, @@GLOBAL.tmp_table_size;
+------------------------------+-------------------------+
| @@GLOBAL.max_heap_table_size | @@GLOBAL.tmp_table_size |
+------------------------------+-------------------------+
|                     16777216 |                16777216 |
+------------------------------+-------------------------+
1 row in set (0.00 sec)

다시 /etc/my.cnf 파일을 수정하여 설정을 변경한다. 필자는 Memory Engine을 주로 사용하기 때문에 좀 넉넉히 잡았는데, 512메가바이트를 사용한다면 512M을 입력하면 된다.

$ sudo vi /etc/my.cnf
max_heap_table_size=32G
tmp_table_size=1G
mysql> SELECT @@GLOBAL.max_heap_table_size, @@GLOBAL.tmp_table_size;
+------------------------------+-------------------------+
| @@GLOBAL.max_heap_table_size | @@GLOBAL.tmp_table_size |
+------------------------------+-------------------------+
|                  34359738368 |              1073741824 |
+------------------------------+-------------------------+
1 row in set (0.00 sec)

MySQL 재시작

$ sudo systemctl restart mysqld
$ systemctl status mysqld

접속

임시비밀번호 찾기

$ sudo cat /var/log/mysqld.log | grep 'temporary password'
2023-01-25T06:04:40.471559Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: xxxxxxxx

xxxxxxxx에 해당하는 임시비밀번호로 접속한다.

root 계정 비밀번호 변경

$ mysql -u root -p
Enter password:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';

데이터베이스 & 사용자 계정 생성

데이터베이스 생성

mysql> CREATE DATABASE new_database;

사용자 계정 생성

mysql> CREATE USER 'new_user'@'%' IDENTIFIED BY 'new_password';
mysql> GRANT ALL PRIVILEGES ON new_database.* to 'new_user'@'%';
mysql> FLUSH PRIVILEGES;

local 에서만 접속을 허락하고 싶으면 'new_user'@'localhost' 라고 쓰면 되고, ' new_user'@'%' 라고 쓰면 모든 IP에서 접속을 허용한다.

new_database.* 의 의미는 new_database 가 가지는 모든 테이블에 대해 권한을 주겠다는 의미이다.

참고한 페이지

https://daedamee.tistory.com/entry/MySQL-%EC%9B%90%ED%95%98%EB%8A%94-%EB%B2%84%EC%A0%84%EC%9C%BC%EB%A1%9C-%ED%8C%A8%ED%82%A4%EC%A7%80-%EC%84%A4%EC%B9%98With-Amazon-Linux

 

MySQL 원하는 버전으로 패키지 설치(With Amazon Linux)

안녕하세요, 대담이입니다. 다시 블로그를 시작하려고 오랜만에 글을 써내려가기 시작합니다 :) 먼저, 다시 시작하는 마음으로 Linux에 MySQL을 설치하는 방법을 다시 소개해드리려고 합니다. 최신

daedamee.tistory.com

https://zionh.tistory.com/31

 

[linux] MySQL 포트 변경하기

자신의 운영체제에 MySQL을 설치하고 실행하면 기본적으로 3306의 포트번호가 할당된다. 하지만 여러개의 mysql db를 관리해야 하는 시점이 오면 mysql 서버에 여러개의 포트를 할당해야 한다. 예를

zionh.tistory.com

https://inma.tistory.com/100

 

[MySQL] MySQL character set encoding 변경 (utf8, utf8mb4)

MySQL encoding 변경 (utf8, utf8mb4)MySQL 기본 패키지는 character set 이 latin1 로 설정되어있습니다. character set 을 utf8 로 설정하여 한글 및 다른 언어가 께지는 것을 처리해야합니다. 때로는 utf8mb4 로 설정

inma.tistory.com

https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=reinstate10&logNo=220448007258 

 

MySQL Memory table (메모리 테이블) 크기 변경

MySQL Memory table (메모리 테이블) 크기 변경 방법을 정리한다. 1. MySQL에서 메모리 테이블 크...

blog.naver.com

https://nirsa.tistory.com/84

 

[MySQL] access denied for user 'root'@'localhost' 해결 방법 (Centos7)

Centos7 버전에 MySQL을 설치하고 mysql 명령으로 진입하려하면 access denied for user 'root'@'localhost' 에러가 발생 합니다. 어느 버전부터 이렇게 바꼇는지는 모르겠지만 mysql 설치 후 root 계정의 패스워드

nirsa.tistory.com

https://swpfun.tistory.com/701

 

MySQL 8.0 초기 세팅(root 패스워드 변경, DB 생성, 사용자 추가까지)

얼마전 우분투 21.04 버전에서 MySQL 8.0 버전의 MySQL-SERVER 를 설치했습니다. MySQL을 설치하고 나면 첫 계정의 패스워드는 공백으로 접속이 가능했습니다. root 계정에는 높은 권한을 가지고 있는 계정

swpfun.tistory.com

 

728x90

댓글