Skip to content

Latest commit

ย 

History

History
1287 lines (954 loc) ยท 37.5 KB

AWS 3-Tier (Compile version).md

File metadata and controls

1287 lines (954 loc) ยท 37.5 KB

AWS 3-Tier (Compile version)

์‚ฌ์ง„๋“ค

Trouble

1. ๋ชฉ์ , ์„œ๋ฒ„ ์ •๋ณด

  • Apache - Tomcat - MySQL์„ Source๋กœ ์ปดํŒŒ์ผํ•˜์—ฌ ์„ค์น˜ํ•œ๋‹ค.
  • service๋ฅผ ๋“ฑ๋กํ•˜์—ฌ systemctl ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.
  • mod_jk ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ Apache-Tomcat์„ ์—ฐ๋™ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํŒŒ์•…ํ•œ๋‹ค.
  • JDBC Driver๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Tomcat-MySQL์„ ์—ฐ๋™ํ•ด๋ณธ๋‹ค.
  • MySQL Master - Slave ์„œ๋ฒ„ ๊ฐ„์˜ Replication ์—ฐ๋™์„ ํ•˜์—ฌ ๋ฐ์ดํ„ฐ์˜ ๊ฐ€์šฉ์„ฑ์„ ๋†’์ธ๋‹ค.

[1] Web Server

  • OS : Ubuntu 18.04
  • Instance Type : t2.micro
  • Apache 2.4.46
  • tomcat_connector : 1.2.48

[2] WAS Server

  • Instance Type : t2.micro
  • OS : Ubuntu 18.04
  • Tomcat : 9.0.41

[3] MySQL

  • Instance Type : t2.micro
  • OS : Ubuntu 18.04
  • MySQL : 5.7.31

2. Overview

3-Tier ์•„ํ‚คํ…์ฒ˜๋ž€?

Untitled

3. ์•„ํ‚คํ…์ณ

_(7)

4. ์ธํ”„๋ผ ๊ตฌ์„ฑ & ์‚ฌ์ „ ์ค€๋น„

์ธํ”„๋ผ ๊ตฌ์„ฑ

[1] VPC

: ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•œ ๋…ผ๋ฆฌ์ ์œผ๋กœ ๋ถ„๋ฆฌ๋œ ํด๋ผ์šฐ๋“œ ๋„คํŠธ์›Œํฌ

[2] Subnet

: VPC ๋‚ด์—์„œ IPv4 ์ฃผ์†Œ๊ฐ€ CIDR ๋ธ”๋ก์— ์˜ํ•ด ๋‚˜๋ˆ ์ง„ ์ฃผ์†Œ ๋‹จ์œ„

[3] Internet Gateway

: ์ธํ„ฐ๋„ท์— ์—ฐ๊ฒฐ๋œ Host ( Public Subnet์„ ์œ„ํ•œ NAT )

[4] NAT Gateway

: Private Subnet์˜ ์ธํ„ฐ๋„ท์„ ํ–ฅํ•œ Outbound ํŠธ๋ž˜ํ”ฝ ํ—ˆ์šฉ ( IPv4 ์ „์šฉ )

[5] Security Group

: ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•œ Inbound & Outbound ํŠธ๋ž˜ํ”ฝ ์ œ์–ด (๊ฐ€์ƒ ๋ฐฉํ™”๋ฒฝ)

[6] Routing Table

: Traffic์„ ์–ด๋””๋กœ ๋ณด๋‚ด์ค„ ์ง€ ์„ค์ •

[7] 3-Tier Public Routing Table

: ํ•ด๋‹น ๋ผ์šฐํŒ… ํ…Œ์ด๋ธ”์„ ๊ฐ€์ง„ Subnet๋“ค์€ IGW๋กœ ๊ฐˆ ๊ฒƒ์„ ์ •์˜

[8] 3-Tier Private Routing Table (์ดˆ๊ธฐ)

: ํ•ด๋‹น ๋ผ์šฐํŒ… ํ…Œ์ด๋ธ”์„ ๊ฐ€์ง„ Subnet๋“ค์€ NAT๋กœ ๊ฐˆ ๊ฒƒ์„ ์ •์˜

[9] 3-Tier Private Routing Table (DB Instance์—์„œ ํ•„์š”ํ•œ ํŒจํ‚ค์ง€, ํŒŒ์ผ ์„ค์น˜ ํ›„)

: DB ์ธ์Šคํ„ด์Šค๊ฐ€ ์žˆ๋Š” Subnet์€ ๋‚ด๋ถ€์—์„œ๋งŒ ํ†ต์‹ ํ•  ๊ฒƒ์„ ์ •์˜

[10] EC2 Instance

: ๊ฐ€์ƒ ์ปดํ“จํŒ… ํ™˜๊ฒฝ

[11] Load Balancer

: AWS์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ถ€ํ•˜ ๋ถ„์‚ฐ ์žฅ์น˜

ssh-key ์˜ฎ๊ธฐ๊ธฐ ( Local โ†’ Bastion )

  • Bastion โ†’ web, was, db ์ธ์Šคํ„ด์Šค๋กœ ssh ์›๊ฒฉ ์ ‘์†์„ ํ•˜๊ธฐ ์œ„ํ•ด ssh key๋ฅผ ์˜ฎ๊น€
scp -i .\3tier-bastion-key.pem .\3tier-web-key.pem ubuntu@<<Bastion IP>>:/home/ubuntu

AWS%203-Tier%20(Compile%20version)%205f39c15c87124248abb7a7759a717d15/Untitled%201.png

Bastion โ†’ Web, Was, DB Instance ์ ‘์†

### Bastion -> WEB
### 3tier-was-key๋กœ ์ ‘์†์‹œ ๋ถˆํ—ˆ, 3tier-web-key๋กœ ์ ‘์†์‹œ ์„ฑ๊ณต
ubuntu@ip-10-0-1-41:~/.ssh$ ssh -i 3tier-was-key.pem [email protected]
[email protected]: Permission denied (publickey).

ubuntu@ip-10-0-1-41:~/.ssh$ ssh -i 3tier-web-key.pem [email protected]
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 5.4.0-1029-aws x86_64)

### Bastion -> WAS
ubuntu@ip-10-0-1-41:~/.ssh$ ssh -i 3tier-was-key.pem [email protected]
The authenticity of host '10.0.5.115 (10.0.5.115)' can't be established.
ECDSA key fingerprint is SHA256:q7eHQcdu530g9oOAkew8cJjnnRtd/P1AezXpWAMxK68.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.5.115' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 5.4.0-1029-aws x86_64)

### Bastion -> DB
ubuntu@ip-10-0-1-41:~/.ssh$ ssh -i 3tier-db-key.pem [email protected]
The authenticity of host '10.0.7.165 (10.0.7.165)' can't be established.
ECDSA key fingerprint is SHA256:VB2YaUg2m5J6KLv3VCbxo+T51Xvqdfci+BFN0Y+5RtE.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.7.165' (ECDSA) to the list of known hosts.

ssh-key ๊ถŒํ•œ 400 ๋ณ€๊ฒฝ (๋ณด์•ˆ ๊ณ ๋ ค)

ubuntu@ip-10-0-1-41:~$ chmod 400 *

ubuntu@ip-10-0-1-41:~/.ssh$ ls -al
total 32
drwx------ 2 ubuntu ubuntu 4096 Jan 19 08:31 .
drwxr-xr-x 5 ubuntu ubuntu 4096 Jan 19 08:31 ..
-r-------- 1 ubuntu ubuntu 1704 Jan 19 08:30 3tier-bastion-key.pem
-r-------- 1 ubuntu ubuntu 1700 Jan 19 08:30 3tier-db-key.pem
-r-------- 1 ubuntu ubuntu 1700 Jan 19 08:30 3tier-was-key.pem
-r-------- 1 ubuntu ubuntu 1704 Jan 19 08:30 3tier-web-key.pem
-rw------- 1 ubuntu ubuntu  399 Jan 19 07:59 authorized_keys
-rw-r--r-- 1 ubuntu ubuntu  222 Jan 19 08:26 known_hosts

5. ์ธ์Šคํ„ด์Šค(web, was, db) ์„ค์น˜

[1] WEB Server

  1. ์ปดํŒŒ์ผ ์„ค์น˜๋ฅผ ์œ„ํ•ด ํ•„์š”ํ•œ ํŒจํ‚ค์ง€ ์„ค์น˜
  2. PCRE, APR, APR-util, HTTPD ์†Œ์Šค ํŒŒ์ผ ๋ฐ›์•„์˜ค๊ธฐ
  3. ์†Œ์Šค ํŒŒ์ผ ํ™˜๊ฒฝ ์„ค์ • ( ./configure )
  4. ์†Œ์ŠคํŒŒ์ผ ์ปดํŒŒ์ผ ( make, make install )
  5. systemd ์„œ๋น„์Šค ํŒŒ์ผ ์ƒ์„ฑ ๋ฐ ๋“ฑ๋ก
  6. Test

1) ํ•„์š”ํ•œ ํŒจํ‚ค์ง€๋“ค ์„ค์น˜ ๋ชฉ๋ก

sudo su -

# Install the necessary packages
apt-get update -y
apt-get install build-essential -y
apt-get install libexpat1-dev -y

2) PCRE, APR, APR-util ์„ค์น˜

cd /usr/local/src
wget https://ftp.pcre.org/pub/pcre/pcre-$PCRE_VERSION.tar.gz
wget https://downloads.apache.org//apr/apr-$APR_VERSION.tar.gz
wget https://downloads.apache.org//apr/apr-util-$APRUTIL_VERSION.tar.gz
wget https://downloads.apache.org//httpd/httpd-$HTTPD_VERSION.tar.gz

## 1. apr
cd /usr/local/src/apr-$APR_VERSION
./configure --prefix=/usr/local/apr
make; make install

## 2. apr-util
cd /usr/local/src/apr-util-$APRUTIL_VERSION
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make; make install

## 3. PCRE
cd /usr/local/src/pcre-$PCRE_VERSION
./configure --prefix=/usr/local/pcre
make; make install

## 4. HTTPD
cd /usr/local/src/httpd-$HTTPD_VERSION
./configure --prefix=/usr/local/apache \
--enable-module=so --enable-rewrite --enable-so \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
--with-pcre=/usr/local/pcre \
--enable-mods-shared=all

make; make install

4) Apache ์‹คํ–‰

## ์‹คํ–‰ : httpd -k start
## ์ข…๋ฃŒ : httpd -k stop

/usr/local/apache2.4/bin/httpd -k start

root@ip-10-0-3-171:/usr/local/apache2.4# ./bin/httpd -k restart

root@ip-10-0-3-171:/usr/local/apache2.4# ps -ef | grep httpd
root     31522     1  0 00:41 ?        00:00:00 ./bin/httpd -k start
daemon   31627 31522  0 00:42 ?        00:00:00 ./bin/httpd -k start
daemon   31628 31522  0 00:42 ?        00:00:00 ./bin/httpd -k start
daemon   31629 31522  0 00:42 ?        00:00:00 ./bin/httpd -k start
root     31712  1879  0 00:42 pts/0    00:00:00 grep --color=auto httpd

root@ip-10-0-3-171:/usr/local/apache2.4# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      872/systemd-resolve 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1184/sshd           
tcp6       0      0 :::22                   :::*                    LISTEN      1184/sshd           
tcp6       0      0 :::80                   :::*                    LISTEN      31522/./bin/httpd

root@ip-10-0-3-171:/usr/local/apache2.4# curl localhost
<html><body><h1>It works!</h1></body></html>

5) systemd ์„œ๋น„์Šค ํŒŒ์ผ ์ƒ์„ฑ ๋ฐ ๋“ฑ๋ก

vim /usr/lib/systemd/system/httpd.service 
[Unit]
Description=apache 
After=network.target syslog.target
 
[Service] 
Type=forking 
User=root 
Group=root 
ExecStart=/usr/local/apache2.4/bin/apachectl start 
ExecStop=/usr/local/apache2.4/bin/apachectl stop 
RestartSec=10 
Restart=always 

[Install] 
WantedBy=multi-user.target

root@ip-10-0-3-171:/usr/local/apache2.4# systemctl restart httpd

root@ip-10-0-3-171:/usr/local/apache2.4# systemctl status httpd
โ— httpd.service - apache
   Loaded: loaded (/lib/systemd/system/httpd.service; disabled; vendor preset: enabled)
   Active: active (running) since Mon 2021-01-25 00:47:50 UTC; 1s ago
  Process: 32511 ExecStop=/usr/local/apache2.4/bin/apachectl stop (code=exited, status=0/SUCCESS)
  Process: 32514 ExecStart=/usr/local/apache2.4/bin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 32527 (httpd)
    Tasks: 82 (limit: 1140)
   CGroup: /system.slice/httpd.service
           โ”œโ”€32527 /usr/local/apache2.4/bin/httpd -k start
           โ”œโ”€32528 /usr/local/apache2.4/bin/httpd -k start
           โ”œโ”€32529 /usr/local/apache2.4/bin/httpd -k start
           โ””โ”€32530 /usr/local/apache2.4/bin/httpd -k start

Jan 25 00:47:50 ip-10-0-3-171 systemd[1]: Stopped apache.
Jan 25 00:47:50 ip-10-0-3-171 systemd[1]: Starting apache...
Jan 25 00:47:50 ip-10-0-3-171 systemd[1]: Started apache.

root@ip-10-0-3-171:/usr/local/apache2.4# systemctl stop httpd

root@ip-10-0-3-171:/usr/local/apache2.4# systemctl status httpd
โ— httpd.service - apache
   Loaded: loaded (/lib/systemd/system/httpd.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

Jan 25 00:47:29 ip-10-0-3-171 systemd[1]: Started apache.
Jan 25 00:47:50 ip-10-0-3-171 systemd[1]: Stopping apache...
Jan 25 00:47:50 ip-10-0-3-171 systemd[1]: Stopped apache.
Jan 25 00:47:50 ip-10-0-3-171 systemd[1]: Starting apache...
Jan 25 00:47:50 ip-10-0-3-171 systemd[1]: Started apache.
Jan 25 00:47:59 ip-10-0-3-171 systemd[1]: Stopping apache...
Jan 25 00:47:59 ip-10-0-3-171 systemd[1]: Stopped apache.
Jan 25 00:47:59 ip-10-0-3-171 systemd[1]: /lib/systemd/system/httpd.service:13: Unknown lvalue 'Umask' in section 'Service'
Jan 25 00:47:59 ip-10-0-3-171 systemd[1]: /lib/systemd/system/httpd.service:13: Unknown lvalue 'Umask' in section 'Service'
Jan 25 00:48:00 ip-10-0-3-171 systemd[1]: /lib/systemd/system/httpd.service:13: Unknown lvalue 'Umask' in section 'Service'

[2] WAS (Web Application Server)

  1. Java Runtime ( JRE 11 ) ์„ค์น˜
  2. /etc/profile ๋ชจ๋“  ์‚ฌ์šฉ์ž ๋Œ€์ƒ ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋“ฑ๋ก
  3. Tomcat Group, User ์ƒ์„ฑ
  4. Apache-Tomcat ์„ค์น˜
  5. Tomcat ์„œ๋น„์Šค ํŒŒ์ผ ์ƒ์„ฑ ๋ฐ ๋“ฑ๋ก
  6. Test

1) Java Runtime ์„ค์น˜, ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์„ค์ •

  • jdk ์„ค์น˜
# sudo su -๋ฅผ ์•ˆํ•  ๊ฒฝ์šฐ ๊ถŒํ•œ ์„ค์ •์„ ์ž˜ ํ•ด์ฃผ์–ด์•ผ ํ•จ
sudo su -

sudo apt-get update -y
sudo apt-get install openjdk-11-jre-headless -y
  • ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋“ฑ๋ก
    • /usr/bin/java ๊ฒฝ๋กœ์— Symbolic Link ๊ฒฝ๋กœ๊ฐ€ ๊ฑธ๋ ค์žˆ์œผ๋ฏ€๋กœ ์‹ค์ œ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์•„ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ๋“ฑ๋กํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
    • ์‹ค์ œ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์•˜์œผ๋ฉด 3๊ฐ€์ง€ (JAVA_HOME, PATH, CLASSPATH)๋ฅผ ๋“ฑ๋ก
root@ip-10-0-5-115:/home/ubuntu# readlink -f /usr/bin/java
/usr/lib/jvm/java-11-openjdk-amd64/bin/java

## JAVA_HOME, CATALINA_HOME, PATH ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋“ฑ๋ก

sudo bash -c 'cat >> /etc/profile' << EOF
# 1.  JAVA_HOME DIR
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/

# 2. TOMCAT SERVER HOME DIR
CATALINA_HOME=/usr/local/tomcat

# 3. binary
PATH=$PATH:/usr/lib/jvm/java-11-openjdk-amd64/bin

export JAVA_HOME CATALINA_HOME PATH
EOF

## source ๋ช…๋ น์–ด๋กœ ์ ์šฉ
source /etc/profile

echo $JAVA_HOME
echo $CATALINA_HOME
echo $PATH

root@ip-10-0-5-206:~# java -version
openjdk version "11.0.9.1" 2020-11-04
OpenJDK Runtime Environment (build 11.0.9.1+1-Ubuntu-0ubuntu1.18.04)
OpenJDK 64-Bit Server VM (build 11.0.9.1+1-Ubuntu-0ubuntu1.18.04, mixed mode, sharing)

2) Tomcat User ์ƒ์„ฑ & Apache Tomcat ์„ค์น˜

sudo groupadd tomcat

sudo useradd -s `which nologin | sed -n 1p` -g tomcat -d /usr/local/tomcat tomcat

cd /usr/local/src

sudo wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.41/bin/apache-tomcat-9.0.41.tar.gz

sudo tar -zxvf apache-tomcat-9.0.41.tar.gz

sudo mv apache-tomcat-9.0.41 /usr/local/tomcat

3) Permission ์„ค์ •

cd /usr/local

sudo chmod -R 755 tomcat

sudo chown -R tomcat:tomcat tomcat

4) Systemd Unit file ์„ค์ •

cat << EOF >> /etc/systemd/system/tomcat.service

[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
Type=forking
User=tomcat
Group=tomcat
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
SuccessExitStatus=143
Restart=always
RestartSec=10
UMask=0007

[Install]
WantedBy=multi-user.target

EOF

## ๋Œ€๋ชฌ ์žฌ์‹œ์ž‘
systemctl daemon-reload 

systemctl restart tomcat

systemctl status tomcat

โ— tomcat.service - Apache Tomcat Web Application Container
   Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: enabled)
   Active: active (running) since Mon 2021-01-25 01:05:07 UTC; 2s ago
  Process: 4318 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)

## netstat -ntlp

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      882/systemd-resolve 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1181/sshd           
tcp6       0      0 :::8080                 :::*                    LISTEN      4339/java           
tcp6       0      0 :::22                   :::*                    LISTEN      1181/sshd           
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      4339/java

5) ์ ‘์† Test

## ์ ‘์† Test 
curl localhost:8080

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Apache Tomcat/9.0.41</title>
        <link href="favicon.ico" rel="icon" type="image/x-icon" />
        <link href="tomcat.css" rel="stylesheet" type="text/css" />
    </head>
..
..
..
..

[3] Database (MySQL)

  1. ์ปดํŒŒ์ผ ์„ค์น˜๋ฅผ ์œ„ํ•ด ํ•„์š”ํ•œ ํŒจํ‚ค์ง€ ์„ค์น˜
  2. User, Group ์ƒ์„ฑ
  3. Source ํŒŒ์ผ Download ๋ฐ ์••์ถ• ํ•ด์ œ
  4. cmake ์ˆ˜ํ–‰ ํ›„ make ๋นŒ๋“œ
  5. ๋””๋ ‰ํ„ฐ๋ฆฌ ์ƒ์„ฑ ๋ฐ ๊ถŒํ•œ ๋ณ€๊ฒฝ
  6. my.cnf ์ƒ์„ฑ ๋ฐ DB ์ƒ์„ฑ ํ›„ ์ดˆ๊ธฐ ์„ค์ •
  7. DB ์„œ๋น„์Šค ๋“ฑ๋ก ๋ฐ ์‹œ์ž‘
  8. Test

1) Source Compile ์„ค์น˜๋ฅผ ์œ„ํ•ด ํ•„์š”ํ•œ Package ์„ค์น˜

sudo apt install build-essential bison \
gcc g++ libncurses5-dev libxml2-dev openssl \
libssl-dev curl libcurl4-openssl-dev libjpeg-dev \
libpng-dev libfreetype6-dev libsasl2-dev \
autoconf libncurses5-dev libtirpc-dev \
ncurses* cmake-gui cmake -y
  • cmake : make์˜ ์†Œ์Šค๊ด€๋ฆฌ ๋ฌธ์ œ์ ์„ ๊ฐœ์„ ์‹œ์ผœ ์ƒˆ๋กญ๊ฒŒ ๋‚˜์˜จ ๋นŒ๋“œ ํ”„๋ ˆ์ž„์›Œํฌ ํ”„๋กœ๊ทธ๋žจ

    ( centos 7.x ์ด์ƒ ์ง€์›)

  • make : ์ง์ ‘ ์†Œ์Šค๋ฅผ ๋นŒ๋“œํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ (์†Œ์Šค๋ฅผ ๋นŒ๋“œํ•˜์—ฌ ๊ธฐ๊ณ„์–ด๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” ํ•ต์‹ฌ์ ์ธ ์—ญํ• , GNU make 3.75 ์ด์ƒ ๋ฒ„์ „์„ ๊ฐ•๋ ฅํžˆ ๊ถŒ๊ณ )

  • ncurses library : ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ํ…์ŠคํŠธ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ„ฐ๋ฏธ๋„ ๋…๋ฆฝ ๋ฐฉ์‹์œผ๋กœ ๊ธฐ๋กํ•  ์ˆ˜ ์žˆ๋„๋ก APU๋ฅผ ์ œ๊ณตํ•˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

  • boost : ๊ฐ์ข… ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ชจ์•„๋‘” C++ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

2) ์œ ์ € ๋ฐ ๊ทธ๋ฃน ์ƒ์„ฑ

  • nologin : ์œ ์ €์˜ ๋กœ๊ทธ์ธ์‰˜ ์‚ฌ์šฉ ๋ชปํ•˜๊ฒŒ ํ•จ ( mysql์ด๋ผ๋Š” ์œ ์ €๋Š” mysql ๋ฐ๋ชฌ์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ์œ ์ €์ผ ๋ฟ, ๋ณด์•ˆ ๊ฐ•ํ™” ์ธก๋ฉด)
root@ip-10-0-7-218:~# which nologin
/usr/sbin/nologin

root@ip-10-0-7-218:~# useradd -s /usr/sbin/nologin -g mysql mysql

3) ํŒŒ์ผ ๋‹ค์šด ๋ฐ ์••์ถ• ํ•ด์ œ

cd /usr/local/src

wget https://downloads.mysql.com/archives/get/p/23/file/mysql-boost-5.7.31.tar.gz

tar zxvf mysql-boost-5.7.31.tar.gz

cd mysql-5.7.31

4) cmake ์ˆ˜ํ–‰

sudo cmake \
'-DCMAKE_INSTALL_PREFIX=/usr/local/mysql5.7' \
'-DINSTALL_SBINDIR=/usr/local/mysql5.7/bin' \
'-DINSTALL_BINDIR=/usr/local/mysql5.7/bin' \
'-DMYSQL_DATADIR=/usr/local/mysql5.7/data' \
'-DINSTALL_SCRIPTDIR=/usr/local/mysql5.7/bin' \
'-DWITH_INNOBASE_STORAGE_ENGINE=1' \
'-DWITH_PARTITION_STORAGE_ENGINE=1' \
'-DSYSCONFDIR=/usr/local/mysql5.7/etc' \
'-DDEFAULT_CHARSET=utf8mb4' \
'-DDEFAULT_COLLATION=utf8mb4_general_ci' \
'-DWITH_EXTRA_CHARSETS=all' \
'-DENABLED_LOCAL_INFILE=1' \
'-DMYSQL_TCP_PORT=3306' \
'-DMYSQL_UNIX_ADDR=/tmp/mysql.sock' \
'-DCURSES_LIBRARY=/usr/lib/x86_64-linux-gnu/libncurses.so' \
'-DCURSES_INCLUDE_PATH=/usr/include' \
'-DDOWNLOAD_BOOST=1' \
'-DWITH_BOOST=./boost' \
'-DWITH_ARCHIVE_STORAGE_ENGINE=1' \
'-DWITH_BLACKHOLE_STORAGE_ENGINE=1' \
'-DWITH_PERFSCHEMA_STORAGE_ENGINE=1' \
'-DWITH_FEDERATED_STORAGE_ENGINE=1'

make; make install

5) ๋””๋ ‰ํ„ฐ๋ฆฌ ์ƒ์„ฑ ๋ฐ ๊ถŒํ•œ ๋ณ€๊ฒฝ

cd /usr/local/mysql5.7
mkdir logs tmp data
touch /usr/local/mysql5.7/logs/mysqld_safe.err

cd /usr/local/
chown -R mysql:mysql mysql5.7

## ํผ๋ฏธ์…˜ ๋ณ€๊ฒฝ
chown -R mysql:mysql /usr/local/mysql5.7

## ์‹ฌ๋ณผ๋ฆญ ๋งํฌ ์‚ฌ์šฉ
cd /usr/local
ln -s mysql5.7  mysql

## MySQL ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋“ฑ๋ก
root@ip-10-0-7-218:/usr/local# cat /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf

root@ip-10-0-7-218:/usr/local# ldconfig

include /usr/local/mysql/lib

## ํ™˜๊ฒฝ๋ณ€์ˆ˜์— PATH ์ถ”๊ฐ€
vi /etc/profile
export PATH=$PATH:/usr/local/mysql/bin

source /etc/profile

6) my.cnf ์ƒ์„ฑ

vim /etc/my.cnf

[mysql]
no-auto-rehash
show-warnings
prompt=\u@\h:\d_\R:\m:\\s>
pager="less -n -i -F -X -E"

[mysqld]
server-id=1
port = 3306
bind-address = 0.0.0.0
basedir = /usr/local/mysql
datadir= /usr/local/mysql/data
tmpdir=/usr/local/mysql/data
socket=/tmp/mysql.sock
user=mysql
skip_name_resolve
#timestamp
explicit_defaults_for_timestamp = TRUE

### MyISAM Spectific options
key_buffer_size = 100M

### INNODB Spectific options
default-storage-engine = InnoDB
innodb_buffer_pool_size = 384M

#User Table Datafile
innodb_data_home_dir = /usr/local/mysql/data/
innodb_data_file_path = ib_system:100M:autoextend
innodb_file_per_table=ON
innodb_log_buffer_size = 8M
innodb_log_files_in_group = 3
innodb_log_file_size=200M
innodb_log_files_in_group=4
#innodb_log_group_home_dir = /usr/local/mysql/data/redologs
#innodb_undo_directory = /usr/local/mysql/data/undologs
innodb_undo_tablespaces = 1

### Connection
back_log = 100
max_connections = 1000
max_connect_errors = 1000
wait_timeout= 60

### log
# Error Log
log_error=/usr/local/mysql/logs/mysqld.err
log-output=FILE
general_log=0
slow-query-log=0
long_query_time = 5
# 5 sec
slow_query_log_file = /usr/local/mysql/logs/slow_query.log
pid-file=/usr/local/mysql/tmp/mysqld.pid

###chracterset
character-set-client-handshake=OFF
skip-character-set-client-handshake
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci

[mysqld_safe]
log_error=/usr/local/mysql/logs/mysqld_safe.err
pid-file=/usr/local/mysql/tmp/mysqld.pid

7) DB ์ƒ์„ฑ ๋ฐ ROOT ํŒจ์Šค์›Œ๋“œ ๋ณ€๊ฒฝ

### 1. DB ์ƒ์„ฑ
cd /usr/local/mysql/bin
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

root@ip-10-0-7-218:/usr/local/mysql/bin# ls -al /usr/local/mysql/data/
total 931908
drwxr-xr-x  5 mysql mysql      4096 Jan 25 04:21 .
drwxr-xr-x 13 mysql mysql      4096 Jan 25 04:17 ..
-rw-r-----  1 mysql mysql        56 Jan 25 04:21 auto.cnf
-rw-------  1 mysql mysql      1680 Jan 25 04:21 ca-key.pem
-rw-r--r--  1 mysql mysql      1112 Jan 25 04:21 ca.pem
-rw-r--r--  1 mysql mysql      1112 Jan 25 04:21 client-cert.pem
-rw-------  1 mysql mysql      1680 Jan 25 04:21 client-key.pem
-rw-r-----  1 mysql mysql       436 Jan 25 04:21 ib_buffer_pool
-rw-r-----  1 mysql mysql 209715200 Jan 25 04:21 ib_logfile0
-rw-r-----  1 mysql mysql 209715200 Jan 25 04:21 ib_logfile1
-rw-r-----  1 mysql mysql 209715200 Jan 25 04:21 ib_logfile2
-rw-r-----  1 mysql mysql 209715200 Jan 25 04:21 ib_logfile3
-rw-r-----  1 mysql mysql 104857600 Jan 25 04:21 ib_system
drwxr-x---  2 mysql mysql      4096 Jan 25 04:21 mysql
drwxr-x---  2 mysql mysql      4096 Jan 25 04:21 performance_schema
-rw-------  1 mysql mysql      1676 Jan 25 04:21 private_key.pem
-rw-r--r--  1 mysql mysql       452 Jan 25 04:21 public_key.pem
-rw-r--r--  1 mysql mysql      1112 Jan 25 04:21 server-cert.pem
-rw-------  1 mysql mysql      1680 Jan 25 04:21 server-key.pem
drwxr-x---  2 mysql mysql     12288 Jan 25 04:21 sys
-rw-r-----  1 mysql mysql  10485760 Jan 25 04:21 undo001

### 2. MySQL DB ๊ธฐ๋™
/usr/local/mysql/bin/mysqld_safe &

### ์ž„์‹œ ROOT ํŒจ์Šค์›Œ๋“œ ํ™•์ธ
root@ip-10-0-7-218:/usr/local/mysql/bin# cd ../logs/
root@ip-10-0-7-218:/usr/local/mysql/logs# cat mysqld.err | grep generated
2021-01-25T04:21:28.256460Z 1 [Note] A temporary password is generated for root@localhost: /j0ercg;h9l

/j0ercg;h9lD

###
root@ip-10-0-7-218:/usr/local/mysql/bin# ./mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.31

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

root@localhost:(none)_04:23:09>

### ALTER USER 'root'@'localhost' IDENTIFIED BY 'ํŒจ์Šค์›Œ๋“œ';

root@localhost:(none)_04:23:13>ALTER USER 'root'@'localhost' IDENTIFIED BY 'dkagh1.'; 
Query OK, 0 rows affected (0.00 sec)

root@localhost:(none)_04:24:01>COMMIT;
Query OK, 0 rows affected (0.00 sec)

root@localhost:(none)_04:24:03>FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

root@localhost:(none)_04:24:07>exit 
Bye

8) DB ์„œ๋น„์Šค ๋“ฑ๋ก ๋ฐ ์‹œ์ž‘

  • DB ์ค‘์ง€ (์„œ๋น„์Šค ๋“ฑ๋ก์„ ์œ„ํ•ด์„œ)
root@ip-10-0-7-218:/usr/local/mysql/bin# ./mysqladmin -u root -p shutdown
Enter password: 
2021-01-25T04:24:43.508787Z mysqld_safe mysqld from pid file /usr/local/mysql/tmp/mysqld.pid ended
[1]+  Done                    mysqld_safe
  • Service ์ƒ์„ฑ
vim /lib/systemd/system/mysqld.service

[Unit]
Description=Mysql Community Server
After=syslog.target
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop

[Install]
WantedBy=multi-user.target
  • ๋Œ€๋ชฌ reload ๋ฐ DB ์‹œ์ž‘
systemctl daemon-reload

systemctl enable mysqld.service

systemctl restart mysqld

root@ip-10-0-7-218:/usr/local/mysql/bin# systemctl status mysqld
โ— mysqld.service - Mysql Community Server
   Loaded: loaded (/lib/systemd/system/mysqld.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2021-01-25 04:25:36 UTC; 3s ago
  Process: 11302 ExecStart=/usr/local/mysql/support-files/mysql.server start (code=exited, status=0/SUCCESS)
 Main PID: 11322 (mysqld_safe)
    Tasks: 28 (limit: 2348)
   CGroup: /system.slice/mysqld.service
           โ”œโ”€11322 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/tmp/mysql
           โ””โ”€11882 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local

Jan 25 04:25:35 ip-10-0-7-218 systemd[1]: Starting Mysql Community Server...
Jan 25 04:25:35 ip-10-0-7-218 mysql.server[11302]: Starting MySQL
Jan 25 04:25:36 ip-10-0-7-218 mysql.server[11302]: . *
Jan 25 04:25:36 ip-10-0-7-218 systemd[1]: Started Mysql Community Server.
  • test
root@ip-10-0-7-218:/usr/local/mysql/bin# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.31 Source distribution

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

root@localhost:(none)_04:26:14>status
--------------
mysql  Ver 14.14 Distrib 5.7.31, for Linux (x86_64) using  EditLine wrapper

Connection id:		2
Current database:	
Current user:		root@localhost
SSL:			Not in use
Current pager:		less -n -i -F -X -E
Using outfile:		''
Using delimiter:	;
Server version:		5.7.31 Source distribution
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	utf8mb4
Db     characterset:	utf8mb4
Client characterset:	utf8mb4
Conn.  characterset:	utf8mb4
UNIX socket:		/tmp/mysql.sock
Uptime:			40 sec

Threads: 1  Questions: 6  Slow queries: 0  Opens: 108  Flush tables: 1  Open tables: 101  Queries per second avg: 0.150
--------------

6. Apache - Tomcat ์—ฐ๋™

[1] mod_jk

  • ์ฃผ์˜์‚ฌํ•ญ : NLB DNS ์ฃผ์†Œ๊ฐ€ ๊ธธ๋ฉด workers.properties๊ฐ€ ์ธ์‹์„ ํ•˜์ง€ ๋ชปํ•œ๋‹ค. NLB ์ฃผ์†Œ๋Š” ์งง๊ฒŒ ์œ ์ง€ํ•œ๋‹ค.

Architecture

Untitled 2

Apache

  1. mod_jk ์„ค์น˜
cd /usr/local/src

wget http://apache.tt.co.kr/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.48-src.tar.gz

tar zxvf tomcat-connectors-1.2.48-src.tar.gz

apt-get install apache2-dev

which apxs
/usr/bin/apxs

cd tomcat-connectors-1.2.48-src/native/

./configure --with-apxs=/usr/bin/apxs

make; make install

mv apache-2.0/mod_jk.so /usr/local/apache2.4/modules/
  1. Apache ์„ค์ • ํŒŒ์ผ ์ˆ˜์ •

  2. httpd.conf์— mod_jk.conf ์ถ”๊ฐ€

  3. mod_jk.conf ํŒŒ์ผ ์„ค์ •

  4. workers.properties ํŒŒ์ผ ์„ค์ •

  5. uriworkermap.properties ํŒŒ์ผ ์„ค์ •

## 1. httpd.conf ์ˆ˜์ •

root@ip-10-0-3-171:/usr/local/apache2.4/conf# tail httpd.conf 
# Note: The following must must be present to support
#       starting without SSL on platforms with no /dev/random equivalent
#       but a statically compiled-in mod_ssl.
#
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>

## ๋งจ ์•„๋ž˜์— ์•„๋ž˜ ๋‚ด์šฉ ์ถ”๊ฐ€!
Include conf/mod_jk.conf

## 2. mod_jk.conf ํŒŒ์ผ ์ถ”๊ฐ€

root@ip-10-0-3-171:/usr/local/apache2.4/conf# cat mod_jk.conf 
LoadModule    jk_module  modules/mod_jk.so

JkWorkersFile conf/workers.properties
JkShmFile     logs/mod_jk.shm
JkLogFile     logs/mod_jk.log
JkLogLevel    info
JKMountFile conf/uriworkermap.properties
JKLogStampFormat "[%a %b %d %H:%M:%s %Y]"
JKRequestLogFormat "%w %V %T"

## 3. workers.properties ํŒŒ์ผ ์ถ”๊ฐ€
root@ip-10-0-3-171:/usr/local/apache2.4/conf# cat workers.properties 
##### workers.properties ##
worker.list=worker1
worker.worker1.type=ajp13
worker.worker1.host=w-lb-c44f0579003043f8.elb.ap-northeast-2.amazonaws.com
worker.worker1.port=8009

## 4. uriworkermap.properties ํŒŒ์ผ ์ถ”๊ฐ€
root@ip-10-0-3-171:/usr/local/apache2.4/conf# cat uriworkermap.properties 
/*=worker1
!/*.html=worker1

root@ip-10-0-3-171:/usr/local/apache2.4/conf# ls
extra  httpd.conf  magic  mime.types  mod_jk.conf  original  uriworkermap.properties  workers.properties
  1. tomcat ์„ค์ • ํŒŒ์ผ (server.xml) ์ˆ˜์ •
  • mod_jk protocol port ๊ฐœ๋ฐฉ
vim server.xml

..
..

<Connector protocol="AJP/1.3"
               address="0.0.0.0"
               port="8009"
               secretRequired="false"
               URIEncoding="UTF-8"
               redirectPort="8443" />

Untitled 3

  1. Test
  • Browser์—์„œ external_alb ์ถœ๋ ฅ

Untitled 4

  • Browser์—์„œ external_alb/index.jsp ์ถœ๋ ฅ

Untitled 5


2) mod_proxy

  • mod_proxy๋Š” 8080 Port๋ฅผ ํ†ตํ•ด ์—ฐ๋™ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

[1] MySQL

root@localhost:(none)_00:01:15>CREATE DATABASE test_db default character set utf8; 
Query OK, 1 row affected (0.00 sec)

root@localhost:(none)_00:03:59>CREATE USER 'hello_user'@'%' IDENTIFIED BY 'passw0rd!'; 
Query OK, 0 rows affected (0.00 sec)

root@localhost:(none)_00:04:34>GRANT ALL PRIVILEGES ON test_db.* to 'hello_user'@'%' IDENTIFIED BY 'passw0rd!'; 
Query OK, 0 rows affected, 1 warning (0.00 sec)

Warning (Code 1287): Using GRANT statement to modify existing user's properties other than privileges is deprecated and will be removed in future release. Use ALTER USER statement for this operation.
root@localhost:(none)_00:05:09>FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

root@localhost:(none)_00:05:42>exit 
Bye
ubuntu@ip-10-0-7-113:~$ mysql -u hello_user -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.31-log Source distribution

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

hello_user@localhost:(none)_00:05:54>SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test_db            |
+--------------------+
2 rows in set (0.00 sec)

hello_user@localhost:(none)_00:07:03>USE test_db; 
Database changed
hello_user@localhost:test_db_00:07:15>SHOW TABLES;
Empty set (0.00 sec)

hello_user@localhost:test_db_00:07:23>CREATE TABLE test_table(name varchar(30)); 
Query OK, 0 rows affected (0.02 sec)

hello_user@localhost:test_db_00:07:44>insert into test_table values('test_value'); 
Query OK, 1 row affected (0.01 sec)

hello_user@localhost:test_db_00:08:16>select * from test_table; 
+------------+
| name       |
+------------+
| test_value |
+------------+
1 row in set (0.00 sec)

hello_user@localhost:test_db_00:08:22>commit;
Query OK, 0 rows affected (0.00 sec)

[2] Tomcat

  • JDBC Driver ์„ค์น˜
cd /usr/local/src

wget https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.48.tar.gz

## tar ์••์ถ• ํ•ด์ œ
tar xzf mysql-connector-java-5.1.48.tar.gz

cd /usr/local/src/mysql-connector-java-5.1.48

cp mysql-connector-java-5.1.48-bin.jar $JAVA_HOME/lib

vim /etc/profile

..
..
## CLASSPATH ์ถ”๊ฐ€
CLASSPATH=$JAVA_HOME/lib/mysql-connector-java-5.1.48-bin.jar

export JAVA_HOME CATALINA_HOME PATH CLASSPATH

source /etc/profile

Untitled 8

  • test.jsp ์ƒ์„ฑ
<%@ page language="java" contentType="text/html; charset=UTF-8"
       pageEncoding="UTF-8" import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>DB Connection Test</title>
</head>
<body>
       <%
              String DB_URL = "jdbc:mysql://10.0.7.113:3306/test_db";
              String DB_USER = "hello_user";
              String DB_PASSWORD = "passw0rd!";
              Connection conn;
              Statement stmt;
              PreparedStatement ps;
              ResultSet rs;
              try {
                     Class.forName("com.mysql.jdbc.Driver");
                     conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
                     stmt = conn.createStatement();

                    /* SQL ์ฒ˜๋ฆฌ ์ฝ”๋“œ ์ถ”๊ฐ€ ๋ถ€๋ถ„ */

                     conn.close();
                     out.println("MySQL JDBC Driver Connection Test Success!!!");

/* ์˜ˆ์™ธ ์ฒ˜๋ฆฌ  */
              } catch (Exception e) {
                     out.println(e.getMessage());
              }
       %>
</body>
</html>

Untitled 9

Untitled 10

8. MySQL Replication

[1] Architecture

  • Master ์„œ๋ฒ„์˜ ์žฅ์• ๊ฐ€ ์ƒ๊ฒผ์„ ๊ฒฝ์šฐ Slave ์„œ๋ฒ„๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

[2] ์›๋ฆฌ

  1. Master : my.cnf์—์„œ server-id๋ฅผ 1๋กœ ์„ค์ •
  2. Master : mysql ๋ฐ๋ชฌ์„ ์žฌ์‹œ์ž‘ ํ›„ Master ์„œ๋ฒ„ ์ •๋ณด๋ฅผ ํ™•์ธํ•œ๋‹ค.
  3. Slave : my.cnf์—์„œ server-id๋ฅผ 2๋กœ ์„ค์ •, ๋ณต์ œํ•  db๋ฅผ ์„ค์ • (๋ชจ๋“  db๋ฅผ ๋ณต์ œํ•  ๊ฒฝ์šฐ ์ง€์ •ํ•ด์ฃผ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.)
  4. Slave : Master์˜ Endpoint, Port, user์˜ password, user_id, ip, log_position, log_file ๋“ฑ์„ ๊ธฐ์ž…ํ•ด์ค€๋‹ค.
  5. MySQL ๋ฐ๋ชฌ์„ ์žฌ์‹œ์ž‘ ํ›„ start slave; ๋ช…๋ น์–ด๋ฅผ ๊ธฐ์ž…ํ•ด์ค€๋‹ค.
  6. ๋ณต์ œ๊ฐ€ ๋˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

[3] ์„ค์ • ๋ฐฉ๋ฒ•

1) Master DB ์„ค์ •

  • mysql -u root -p๋กœ ์ ‘์† ํ›„ db ์ƒ์„ฑ, ๊ณ„์ • ์ƒ์„ฑ, ๊ถŒํ•œ ๋ถ€์—ฌ, ..
## DB ์ƒ์„ฑ
mysql> create database repl_db default character set utf8;

## ๊ณ„์ •์ƒ์„ฑ
mysql> create user user1@'%' identified by 'test123';

##๊ถŒํ•œ๋ถ€์—ฌ
mysql> grant all privileges on repl_db.* to user1@'%' identified by 'test123';

## ๋ฆฌํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ณ„์ •์ƒ์„ฑ
mysql> grant replication slave on *.* to 'repl_user'@'%' identified by 'test456'

## my.cnf ์„ค์ •

vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin
server-id=1
..

## mysqld ์žฌ์‹œ์ž‘
systemctl restart mysqld

## Master ์ •๋ณด ํ™•์ธ
root@localhost:(none)_07:36:13>show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

## File : MySQL ๋กœ๊ทธํŒŒ์ผ
## Position : ๋กœ๊ทธ ํŒŒ์ผ๋‚ด ์ฝ์„ ์œ„์น˜
## Binlog_Do_DB : ๋ฐ”์ด๋„ˆ๋ฆฌ(Binary)๋กœ๊ทธ ํŒŒ์ผ(๋ณ€๊ฒฝ๋œ ์ด๋ฒคํŠธ ์ •๋ณด๊ฐ€ ์Œ“์ด๋Š” ํŒŒ์ผ)
## Binlog_Ignore_DB : ๋ณต์ œ ์ œ์™ธ ์ •๋ณด

2) Slave ์„œ๋ฒ„

  • DB ์ƒ์„ฑ
## mysql -u root -p๋กœ ์ ‘์†
## DB ์ƒ์„ฑ
mysql> create database repl_db default character set utf8;

## ๊ณ„์ • ์ƒ์„ฑ
mysql> create user user1@'%' identified by 'test123';

## ๊ถŒํ•œ ๋ถ€์—ฌ
mysql> grant all privileges on repl_db.* to user1@'%' identified by 'test123';

## my.cnf ์„ค์ •
# vi /etc/my.cnf

[mysqld]
server-id=2
replicate-do-db='repl_db'

## Master ์„œ๋ฒ„๋กœ ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ์„ค์ •
## mysql -u root -p๋กœ ์ ‘์†

mysql> stop slave;

mysql> change master to
master_host='10.0.7.113',
master_user='repl_user',
master_password='test456',
master_log_file='mysql-bin.000003',
master_log_pos=154;

mysql> start slave;

# MASTER_HOST : Mster ์„œ๋ฒ„ IP ์ž…๋ ฅ
# MASTER_USER : ๋ฆฌํ”Œ๋ฆฌ์ผ€์ด์…˜ ID
# MASTER_PASSWORD : ๋ฆฌํ”Œ๋ฆฌ์ผ€์ด์…˜ PW
# MASTER_LOG_FILE : MASTER STATUS ๋กœ๊ทธํŒŒ์ผ๋ช…
# MASTER_LOG_POS : MASTER STATUS์—์„œ position ๊ฐ’

# Slave_IO_State, Slave_IO_Running, Slave_SQL_Running์ด
# ์•„๋ž˜์™€ ๊ฐ™์ด ๋– ์•ผ ์ •์ƒ์ด๋‹ค.
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.7.113
                  Master_User: repl_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 154
               Relay_Log_File: ip-10-0-8-237-relay-bin.000003
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: repl_db
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table:
..
..
..

## mysql ์žฌ์‹œ์ž‘

systemctl restart mysqld
  • Test

  • Master์—์„œ repl_db๋กœ ์ ‘์†ํ•˜์—ฌ tables๋ฅผ ํ™•์ธ ํ›„ student_db๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

    ํ›„์— show tables; ๋ฅผ ํ•ด๋ดค์„ ๋•Œ student_db๊ฐ€ ์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

root@localhost:(none)_07:36:20>use repl_db;
Database changed
root@localhost:repl_db_07:40:02>show tables;
Empty set (0.00 sec)

root@localhost:repl_db_07:40:04>CREATE TABLE `student_tb` (
    ->     `sno` int(11) NOT NULL,
    ->     `name` char(10) DEFAULT NULL,
    ->     `det` char(20) DEFAULT NULL,
    ->     `addr` char(80) DEFAULT NULL,
    ->     `tel` char(20) DEFAULT NULL,
    ->     PRIMARY KEY (`sno`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.03 sec)

root@localhost:repl_db_07:40:06>show tables;
+-------------------+
| Tables_in_repl_db |
+-------------------+
| student_tb        |
+-------------------+
1 row in set (0.01 sec)
  • Slave์—์„œ repl_db;์— ์ ‘์†ํ•˜์—ฌ ์ฒ˜์Œ์— tables๋ฅผ ์กฐํšŒํ–ˆ์„ ๋•Œ ์•„๋ฌด๊ฒƒ๋„ ์—†์ง€๋งŒ Master์—์„œ table์„ ์ƒ์„ฑํ•œ ์ดํ›„ ์„ฑ๊ณต์ ์œผ๋กœ ๋ณต์ œ๊ฐ€ ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
root@localhost:(none)_07:39:23>use repl_db;
Database changed
root@localhost:repl_db_07:39:49>show tables;
Empty set (0.00 sec)

root@localhost:repl_db_07:39:50>show tables;
+-------------------+
| Tables_in_repl_db |
+-------------------+
| student_tb        |
+-------------------+
1 row in set (0.00 sec)
  • Master์˜ Bin Log File ํ˜น์€ Position ๊ฐ’์ด ๋ฐ”๋€Œ๋Š” ๊ฒฝ์šฐ

    SHOW MASTER STATUS; ๋กœ ๋ฐ”๋€ ๊ฐ’์„ ํ™•์ธํ•œ๋‹ค.

Untitled 11

  • SLAVE์— ์ ‘์†ํ•˜์—ฌ ๋ฐ”๋€ ๊ฐ’๋“ค์„ ์ˆ˜์ •ํ•ด์ค€๋‹ค.
> stop slave;

> change master to
master_host='10.0.7.113',
master_user='repl_user',
master_password='test456',
master_log_file='mysql-bin.000008',
master_log_pos=154;

> start slave;

Untitled 12

  • Master์˜ Bin_log ํŒŒ์ผ or Position ๊ฐ’์ด ์ˆ˜์ •๋  ๊ฒฝ์šฐ
mysql> stop slave; 

mysql> change master to \
master_host='10.0.7.113', \
master_user='repl_user', \
master_password='test456', \
master_log_file='mysql-bin.000003', \
master_log_pos=154;

mysql> start slave;
  • Slave_IO_State๊ฐ€ Waiting for master to send event๋กœ ๋ฐ”๋€Œ๊ฒŒ ๋œ๋‹ค.

Untitled 13