-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-mysql-script.sh
More file actions
95 lines (77 loc) · 2.86 KB
/
docker-mysql-script.sh
File metadata and controls
95 lines (77 loc) · 2.86 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
95
#!/bin/bash
sudo docker pull mysql:latest
# Set Variables
unset ContainerName
while [ -z ${ContainerName} ]; do
read -p 'Enter container name: ' ContainerName
done
read -p 'Enter local port ([3306]): ' LocalPort
LocalPort=${LocalPort:-3306}
read -p 'Enter container port ([3306]): ' ContainerPort
ContainerPort=${ContainerPort:-3306}
read -p 'Enter bind host ([127.0.0.1]): ' BindHost
BindHost=${BindHost:-127.0.0.1}
unset RootUserPassword
while [ -z ${RootUserPassword} ]; do
read -s -p 'Enter root password: ' RootUserPassword
done
echo
unset SetDatabaseOnInitialize
while [ -z ${SetDatabaseOnInitialize} ]; do
read -p 'Do you want to create database and user credentials ([Y]/N): ' SetDatabaseOnInitialize
SetDatabaseOnInitialize=${SetDatabaseOnInitialize:-Y}
done
if [[ "$SetDatabaseOnInitialize" = "Y" ]]; then
unset DatabaseName
while [ -z ${DatabaseName} ]; do
read -p 'Enter database name: ' DatabaseName
done
unset DatabaseUser
while [ -z ${DatabaseUser} ]; do
read -p 'Enter database user: ' DatabaseUser
done
unset DatabasePassword
while [ -z ${DatabasePassword} ]; do
read -s -p 'Enter database password: ' DatabasePassword
done
echo
fi
unset PersistConfig
while [ -z ${PersistConfig} ]; do
read -p 'Do you want to persist configuration to local storage - /etc/docker/'${ContainerName}'/my.cnf ([Y]/N): ' PersistConfig
PersistConfig=${PersistConfig:-Y}
done
if [[ "$PersistConfig" = "Y" ]]; then
sudo mkdir -p /etc/docker/${ContainerName}
sudo touch /etc/docker/${ContainerName}/my.cnf
sudo tee -a /etc/docker/${ContainerName}/my.cnf <<EOF
innodb_buffer_pool_size = 200M
query_cache_size = 0
thread_pool_size = 24
bind-address = ${BindHost}
#mysql_bind_host =
validate_password_policy = MEDIUM
max_connections = 50
#innodb_file_per_table =
innodb_io_capacity = 100
character_set_server = utf8mb4
collation_server = utf8mb4_0900_ai_ci
log_bin = 1
EOF
fi
unset PersistData
while [ -z ${PersistData} ]; do
read -p 'Do you want to persist data to local storage - ./'${ContainerName}'-data ([Y]/N): ' PersistData
PersistData=${PersistData:-Y}
done
if [[ "$PersistData" = "Y" ]]; then
sudo docker volume create ${ContainerName}-data
fi
sudo docker run \
--name "$ContainerName" \
-e MYSQL_ROOT_PASSWORD="$RootUserPassword" \
$([ "$SetDatabaseOnInitialize" == "Y" ] && echo "-e MYSQL_DATABASE=${DatabaseName} -e MYSQL_USER=${DatabaseUser} -e MYSQL_PASSWORD=${DatabasePassword} " || echo "") \
-p "$LocalPort":"$ContainerPort" \
$([ "$PersisConfig" == "Y" ] && echo "-v /etc/docker/${ContainerName}:/etc/mysql/conf.d" || echo "") \
$([ "$PersistData" == "Y" ] && echo "-v ${ContainerName}-data:/var/lib/mysql" || echo "") \
-d mysql