-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: DB EC2 MySQL data volume 분리 #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,12 +15,61 @@ DB_ROOT_USER_SQL="$(mysql_escape "$DB_ROOT_USER")" | |
| DB_ROOT_PASS_SQL="$(mysql_escape "$DB_ROOT_PASS")" | ||
|
|
||
| command -v docker >/dev/null | ||
| systemctl enable --now docker | ||
| command -v blkid >/dev/null | ||
| command -v mkfs.ext4 >/dev/null | ||
| command -v mountpoint >/dev/null | ||
|
|
||
| systemctl enable docker | ||
|
|
||
| DATA_VOLUME_ID="${db_data_volume_id}" | ||
| DATA_VOLUME_SERIAL="$(printf '%s' "$DATA_VOLUME_ID" | tr -d '-')" | ||
| DATA_VOLUME_DEVICE="/dev/disk/by-id/nvme-Amazon_Elastic_Block_Store_$DATA_VOLUME_SERIAL" | ||
| DATA_VOLUME_MOUNT="/mnt/mysql-data" | ||
| MYSQL_DATA_DIR="$DATA_VOLUME_MOUNT/mysql" | ||
|
|
||
| for i in $(seq 1 120); do | ||
| if [ -e "$DATA_VOLUME_DEVICE" ]; then | ||
| break | ||
| fi | ||
| sleep 2 | ||
| done | ||
|
|
||
| if [ ! -e "$DATA_VOLUME_DEVICE" ]; then | ||
| echo "Data volume $DATA_VOLUME_ID was not attached within 240 seconds." >&2 | ||
| ls -la /dev/disk/by-id >&2 || true | ||
| exit 1 | ||
| fi | ||
|
|
||
| if ! blkid "$DATA_VOLUME_DEVICE" >/dev/null 2>&1; then | ||
| mkfs.ext4 -F "$DATA_VOLUME_DEVICE" | ||
| fi | ||
|
|
||
| mkdir -p "$DATA_VOLUME_MOUNT" | ||
| DATA_VOLUME_UUID="$(blkid -s UUID -o value "$DATA_VOLUME_DEVICE")" | ||
|
|
||
| awk -v mount="$DATA_VOLUME_MOUNT" '$2 != mount { print }' /etc/fstab > /etc/fstab.tmp | ||
| printf 'UUID=%s %s ext4 defaults,nofail 0 2\n' "$DATA_VOLUME_UUID" "$DATA_VOLUME_MOUNT" >> /etc/fstab.tmp | ||
| mv /etc/fstab.tmp /etc/fstab | ||
|
Comment on lines
+50
to
+52
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 현재 이때 EBS mount가 실패하거나 지연되면
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 지적해주신 부분 감사합니다! mount 의존성을 추가하는 방식이 가장 안정적일 것 같아서 해당 내용 반영했습니다! |
||
|
|
||
| mountpoint -q "$DATA_VOLUME_MOUNT" || mount "$DATA_VOLUME_MOUNT" | ||
| mountpoint -q "$DATA_VOLUME_MOUNT" || { | ||
| echo "Failed to mount data volume $DATA_VOLUME_ID at $DATA_VOLUME_MOUNT." >&2 | ||
| exit 1 | ||
| } | ||
|
|
||
| mkdir -p /etc/systemd/system/docker.service.d | ||
| cat > /etc/systemd/system/docker.service.d/10-require-mysql-data.conf <<EOF | ||
| [Unit] | ||
| RequiresMountsFor=$DATA_VOLUME_MOUNT | ||
| After=local-fs.target | ||
| EOF | ||
| systemctl daemon-reload | ||
| systemctl restart docker | ||
| docker image inspect mysql:8.4.8 >/dev/null | ||
|
|
||
| mkdir -p /var/lib/mysql | ||
| chown -R 999:999 /var/lib/mysql | ||
| chmod 750 /var/lib/mysql | ||
| mkdir -p "$MYSQL_DATA_DIR" | ||
| chown -R 999:999 "$MYSQL_DATA_DIR" | ||
| chmod 750 "$MYSQL_DATA_DIR" | ||
|
|
||
| mkdir -p /etc/mysql/conf.d | ||
| cat > /etc/mysql/conf.d/tuning.cnf <<'CNFEOF' | ||
|
|
@@ -34,7 +83,7 @@ docker run -d \ | |
| --name mysql-server \ | ||
| --restart always \ | ||
| -p 3306:3306 \ | ||
| -v /var/lib/mysql:/var/lib/mysql \ | ||
| -v "$MYSQL_DATA_DIR:/var/lib/mysql" \ | ||
| -v /etc/mysql/conf.d:/etc/mysql/conf.d \ | ||
| -e MYSQL_ROOT_PASSWORD="$DB_ROOT_PASS" \ | ||
| mysql:8.4.8 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.