과제 : MySQL, Redis 자동설치 최종 스크립트

MySQL, Redis 스크립트 최종본

1. Local 환경 실행

1) MySQL 최종 script

  • install_mysql.sh파일 생성 : touch install_mysql.sh
  • install_mysql.sh파일 권한부여 : chmod +x install_mysql.sh
  • install_mysql.sh파일 시작 : sh install_mysql.sh 8.0.27
#!/bin/bash

# download wget
sudo yum -y install wget

# download libaio 
sudo yum -y install libaio

# remove mariadb(centos7 default db)
sudo yum -y erase mariadb* 

# get user input (version)
version=$1

# NHN CLOUD basically install under root(/), move to /home/centos
cd /home/centos

# download zip file 
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-$version-linux-glibc2.12-x86_64.tar.xz

# upzip file
tar -xvf mysql-$version-linux-glibc2.12-x86_64.tar.xz

# change name
mv mysql-$version-linux-glibc2.12-x86_64/ mysql

# go mysql file
cd mysql

# touch my.cnf
touch my.cnf

# set up my.cnf
# vi my.cnf
echo "[client]
port=3306
socket=/home/centos/mysql/mysql.sock
[mysqld]
socket=/home/centos/mysql/mysql.sock
basedir=/home/centos/mysql
datadir=/home/centos/mysql/data
user=centos
key_buffer_size=64M
max_allowed_packet=32M
#query_cache_size=32M
max_connections=2625
max_connect_errors=2000000
wait_timeout=60
explicit_defaults_for_timestamp = 1
pid-file=/home/centos/mysql/mysqld.pid
log-error=/home/centos/mysql/logs/mysqld.log
bulk_insert_buffer_size=0" > my.cnf

# make data,logs directory
mkdir data
mkdir logs

# give permission to centos
sudo chown -R centos:centos /home/centos/mysql

# initialize
/home/centos/mysql/bin/mysqld --defaults-file=/home/centos/mysql/my.cnf --initialize

# apply my.cnf
/home/centos/mysql/bin/mysqld --defaults-file=/home/centos/mysql/my.cnf &

# sleep for connect
sleep 3

# find rootpass
ROOTPASS="`sudo grep 'temporary password' /home/centos/mysql/logs/mysqld.log | sed 's/.*root@localhost: //'`"

# mysql start with rootpass
/home/centos/mysql/bin/mysql -uroot -p$ROOTPASS -S /home/centos/mysql/mysql.sock

virtual box 에서는 mysql 접속까지 한번에 진행되는데, NHN Cloud에서는 please use --connect-expired-password 오류가 남. 따로 임시비밀번호로 로그인하면 mysql 접속 됨. 이를 해결할 방법은 initialize 진행 시 -insecure를 해주는 방법밖에 없는지??

mysql 설치 완료되면 중간에 'OK' 뜸 => 확인되면 압축해제하는 형식으로


2) Redis 최종 script

  • install_redis.sh파일 생성 : touch install_redis.sh
  • install_redis.sh파일 권한부여 : chmod +x install_redis.sh
  • install_redis.sh파일 시작 : sh install_redis.sh 6.2.6
#!/bin/bash

# get user input (version)
echo $1

# download wget 
sudo yum -y install wget

# download gcc 
sudo yum -y install gcc

# alias
alias redis-cli='src/redis-cli'

# download zip file
wget https://download.redis.io/releases/redis-$1.tar.gz

# unzip file
tar xzf redis-$1.tar.gz

# cd redis file
cd redis-$1

# make comfile
make

# start redis-server (daemon)
# edit redis.conf
sed -i 's/daemonize no/daemonize yes/g' redis.conf
src/redis-server ./redis.conf 

# start alias redis-cli
redis-cli


2. NHN Cloud 환경 실행

스크립트를 NHN Cloud 인스턴스 생성 시 예약 스크립트에 넣어주세요.

1) MySQL 최종 script

#!/bin/bash

# download wget
sudo yum -y install wget

# download libaio 
sudo yum -y install libaio

# remove mariadb(centos7 default db)
sudo yum -y erase mariadb* 

# input version
version=8.0.27

# NHN CLOUD basically install under root(/), move to /home/centos
cd /home/centos

# download zip file 
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-$version-linux-glibc2.12-x86_64.tar.xz

# upzip file
tar -xvf mysql-$version-linux-glibc2.12-x86_64.tar.xz

# change name
mv mysql-$version-linux-glibc2.12-x86_64/ mysql

# go mysql file
cd mysql

# touch my.cnf
touch my.cnf

# set up my.cnf
# vi my.cnf
echo "[client]
port=3306
socket=/home/centos/mysql/mysql.sock
[mysqld]
socket=/home/centos/mysql/mysql.sock
basedir=/home/centos/mysql
datadir=/home/centos/mysql/data
user=centos
key_buffer_size=64M
max_allowed_packet=32M
#query_cache_size=32M
max_connections=2625
max_connect_errors=2000000
wait_timeout=60
explicit_defaults_for_timestamp = 1
pid-file=/home/centos/mysql/mysqld.pid
log-error=/home/centos/mysql/logs/mysqld.log
bulk_insert_buffer_size=0" > my.cnf

# make data,logs directory
mkdir data
mkdir logs

# set mysql.server route
cd /home/centos/mysql/support-files        
sed -i '1,50s|'datadir='|'datadir=/home/centos/mysql/data'|g' mysql.server     
sed -i '1,50s|'basedir='|'basedir=/home/centos/mysql'|g' mysql.server

# give permission to centos
sudo chown -R centos:centos /home/centos/mysql

# initialize
/home/centos/mysql/bin/mysqld --defaults-file=/home/centos/mysql/my.cnf --initialize

# apply my.cnf
/home/centos/mysql/bin/mysqld --defaults-file=/home/centos/mysql/my.cnf &

# start mysql.server
/home/centos/mysql/support-files/mysql.server start

# sleep for connect
# sleep 3

# find rootpass
ROOTPASS="`sudo grep 'temporary password' /home/centos/mysql/logs/mysqld.log | sed 's/.*root@localhost: //'`"

# echo rootpass
echo $ROOTPASS

# mysql start with rootpass
/home/centos/mysql/bin/mysql -uroot -p'cloud-init-output.log에서 확인한 임시비밀번호' -S /home/centos/mysql/mysql.sock

NHN CLOUD 실행 시 cloud-init-output.log에서 ROOTPASS 확인하고 마지막 명령어는 따로 실행해주어야 함

임시비밀번호를 확인하는 또다른 방법의 코드

ROOTPASS=`cat /var/log/mysqld.log | grep generated | awk -F\localhost:" "  {'print $2'}`

awk -F를 통해 localhost:" " 를 기준으로 앞에는 $1, 뒤에는 $2 로 구분지어줍니다.
임시비밀번호는 print $2 를 통해 출력가능

궁금점 🧐

NHN CLOUD에서 임시비밀번호를 사용해서 mysql 접속까지 한번에 실행해주는 건 불가능일까?


1-추가) MySQL 최종 script(임시비밀번호 없이 mysql 접속)

하지만 보안상 추천하진 않아요!

2) Redis 최종 script

#!/bin/bash

# input version
version=6.2.6

# download wget 
sudo yum -y install wget

# download gcc 
sudo yum -y install gcc

# alias
alias redis-cli='src/redis-cli'

# download zip file
wget https://download.redis.io/releases/redis-$version.tar.gz

# unzip file
tar xzf redis-$version.tar.gz

# cd redis file
cd redis-$version

# make comfile
make

# start redis-server (daemon)
# edit redis.conf
sed -i 's/daemonize no/daemonize yes/g' redis.conf
src/redis-server ./redis.conf 

# start alias redis-cli
redis-cli

NHN CLOUD 실행 시 마지막 명령어 src/redis-cli는 따로 실행해주어야 함

좋은 웹페이지 즐겨찾기