-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-and-run-db.sh
More file actions
executable file
·94 lines (79 loc) · 2.97 KB
/
create-and-run-db.sh
File metadata and controls
executable file
·94 lines (79 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env bash
set -euo pipefail
# -------- configurable ----------
CONTAINER_NAME="forge-health-monitor-mysql"
IMAGE="mysql:latest"
HOST_PORT="3366"
MYSQL_ROOT_PASSWORD="admin"
MYSQL_DATABASE="forgehealthmonitor"
# if you want a persistent named volume; set to "" to use an anonymous one
VOLUME_NAME="forge_health_monitor_mysql_data"
# --------------------------------
echo "==> Stop & remove previous container (if exists)"
if docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}\$"; then
docker rm -f -v "${CONTAINER_NAME}" >/dev/null || true
echo " removed ${CONTAINER_NAME} (+anonymous volumes)"
fi
docker run -d \
--name "${CONTAINER_NAME}" \
-e MYSQL_ROOT_PASSWORD="${MYSQL_ROOT_PASSWORD}" \
-e MYSQL_DATABASE="${MYSQL_DATABASE}" \
-p "${HOST_PORT}:3306" \
--restart always \
"${IMAGE}" >/dev/null
echo "==> Waiting for MySQL to be ready on port ${HOST_PORT} ..."
# health-check loop
for i in {1..60}; do
if docker exec "${CONTAINER_NAME}" mysqladmin ping -proot -p"${MYSQL_ROOT_PASSWORD}" --silent >/dev/null 2>&1; then
break
fi
sleep 20
done
# extra sanity check
docker exec -e MYSQL_PWD="${MYSQL_ROOT_PASSWORD}" -i "${CONTAINER_NAME}" \
mysql -uroot -e "SELECT VERSION();" >/dev/null
echo "==> Ensure database '${MYSQL_DATABASE}' exists"
docker exec -e MYSQL_PWD="${MYSQL_ROOT_PASSWORD}" -i "${CONTAINER_NAME}" \
mysql -uroot -e "CREATE DATABASE IF NOT EXISTS \`${MYSQL_DATABASE}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;"
# Double-check by a trivial query
docker exec -i "${CONTAINER_NAME}" \
mysql -uroot -p"${MYSQL_ROOT_PASSWORD}" -e "SELECT 1;" >/dev/null
echo "==> Creating table 'users' in database '${MYSQL_DATABASE}'"
docker exec -i "${CONTAINER_NAME}" \
mysql -uroot -p"${MYSQL_ROOT_PASSWORD}" "${MYSQL_DATABASE}" <<'SQL'
SET foreign_key_checks = 0;
create table records
(
id int auto_increment
primary key,
health_check_url varchar(255) not null,
last_updated_from_server datetime not null,
last_update datetime not null,
isAlive tinyint(1) not null,
created datetime not null,
created_server datetime not null,
user_id varchar(255) not null,
constraint records_г
unique (health_check_url, user_id)
);
create table register_numbers
(
id int auto_increment
primary key,
signed_code text not null,
account_id text not null,
expired_date datetime not null,
used tinyint(1) default 0 not null,
message text not null
);
create table tenant_info
(
id int auto_increment
primary key,
private_key text not null,
public_key text not null,
custom_server_public_key text null
);
SET foreign_key_checks = 1
SQL
echo "==> Done. MySQL is running on 127.0.0.1:${HOST_PORT}, DB=${MYSQL_DATABASE}"