본문 바로가기

리눅스

[리눅스] CentOS 7에 MySQL 5.7 설치

728x90

CentOS 7에 MySQL 5.7 설치

테스트 환경

- DB : mysql 5.7.15

$ cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

$ getconf LONG_BIT
64

mysql 계정 생성

groupadd -g 27 mysql

useradd -m -c "MySQL Server" -d /usr/local/mysql -s /bin/false -g 27 -u 27 mysql

MySQL Community Server 다운로드 받기

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

압축 해제

tar xfz mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz

mysql-5.7.15-linux-glibc2.5-x86_64 디렉토리 이동 및 이름 변경

mv mysql-5.7.15-linux-glibc2.5-x86_64 /usr/local/mysql

etc, data, tmp, logs(log-error) 디렉토리 및 덤미 로그 파일 생성

mkdir -p {etc,data,tmp,logs}
chmod 750 logs

touch logs/mysqld.log

my.cnf 파일 위치 확인

mysqld --verbose --help | grep -A 1 'Default options'

$ /usr/local/mysql/bin/mysqld --verbose --help | grep -A 1 'Default options'
mysqld: Can't change dir to '/usr/local/mysql/data/' (Errcode: 2 - No such file or directory)
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf

my.conf 설정 파일 생성(my-default.cnf 복사)

cp support-files/my-default.cnf /usr/local/mysql/etc/my.cnf

my.cnf 추가 설정

cat <<EOF >> /usr/local/mysql/etc/my.cnf

###ADD
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port=3306
server_id=1
socket=/usr/local/mysql/tmp/mysql.sock

symbolic-links=0

[mysqld_safe]
log-error=/usr/local/mysql/logs/mysqld.log
pid-file=/usr/local/mysql/tmp/mysql.pid
EOF

mysql 디렉토리 소유자 변경

chown -R mysql.mysql /usr/local/mysql

DB 초기 기동(initialize)

bin/mysqld --defaults-file=/usr/local/mysql/etc/my.cnf --initialize --user=mysql
---output---
2022-03-03T13:14:27.349870Z 1 [Note] A temporary password is generated for root@localhost: KiseA4kx#!aW
bin/mysql_ssl_rsa_setup --defaults-file=/usr/local/mysql/etc/my.cnf

DB 서버 기동(mysqld_safe)

bin/mysqld_safe --defaults-file=/usr/local/mysql/etc/my.cnf --user=mysql &
---output---
2022-03-03T13:17:53.168123Z mysqld_safe Logging to '/usr/local/mysql/logs/mysqld.log'.
2022-03-03T13:17:53.255147Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

mysql 서비스 확인

ps -ef | grep -v grep | grep mysql

$ ps -ef | grep -v grep | grep mysql
root     10996  9902  0 22:17 pts/0    00:00:00 /bin/sh bin/mysqld_safe --defaults-file=/usr/local/mysql/etc/my.cnf --user=mysql
mysql    11193 10996  0 22:17 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/logs/mysqld.log --pid-file=/usr/local/mysql/tmp/mysql.pid --socket=/usr/local/mysql/tmp/mysql.sock --port=3306

mysql 접속(root 계정으로 접속)

bin/mysql --socket=/usr/local/mysql/tmp/mysql.sock -uroot -p'KiseA4kx#!aW'

$ bin/mysql --socket=/usr/local/mysql/tmp/mysql.sock -uroot -p'KiseA4kx#!aW'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.15

Copyright (c) 2000, 2016, 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>

ALTER USER 문을 사용하여 암호를 재설정

alter user 'root'@'localhost' identified by 'netpassword';

mysql> alter user 'root'@'localhost' identified by 'netpassword';
Query OK, 0 rows affected (0.00 sec)

DB 서버 중지

bin/mysqladmin -h127.0.0.1 -uroot -p shutdown

$ bin/mysqladmin -h127.0.0.1 -uroot -p shutdown
Enter password:
---output---
2022-03-03T13:57:29.210843Z 0 [Note] Shutting down plugin 'sha256_password'
2022-03-03T13:57:29.211060Z 0 [Note] Shutting down plugin 'mysql_native_password'
2022-03-03T13:57:29.211245Z 0 [Note] Shutting down plugin 'binlog'
2022-03-03T13:57:29.211748Z 0 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete

2022-03-03T13:57:29.315738Z mysqld_safe mysqld from pid file /usr/local/mysql/tmp/mysql.pid ended
728x90