-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-create-mysql
142 lines (110 loc) · 3.5 KB
/
docker-create-mysql
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/bash
LANG=C
cmdname=`basename $0`
newtmpdir=`mktemp -d /tmp/${cmdname}.XXXXXX`
buf_pool_size=$((`free -g |grep 'Mem:' |awk '{print$2}'` / 3)) #calculate innodb_buffer_pool_size 1/3 of the total memory
user=`whoami`
OS=`uname`
[ "${user}" != "root" ] && echo "sudo ${cmdname}" && exit 1;
function cleanup () {
rm -rf "${newtmpdir}"
}
docker_bin=`which docker`;
if [ "$docker_bin" == "" ]
then
echo "Docker not installed. Proceed with install? (CTRL+C to Abort)"
echo "or run install manually: apt-get -y install docker-ce"
read
apt-get -y install docker-ce
fi
function usage() {
cat <<EOF
Usage: ${cmdname}
Interactive script to download and setup mysql-server in docker.
Will ask for mysql PORT and VERSION, and will create new service
with automatic startup across reboots.
-h --help : Help (this screen).
EOF
}
trap 'cleanup' EXIT
trap 'cleanup' SIGTERM
case "$1" in
-h|--help)
usage
exit
;;
esac
echo "Please enter database PORT (eg 3306), CTRL+C to abort: "
read docker_port
[ "${docker_port}" == "" ] && echo "Error: Empty port!" && exit 1
if lsof -i:${docker_port} |grep -q LISTEN
then
echo "ERROR: Port ${docker_port} allready in use!"
lsof -i:${docker_port} |grep LISTEN
exit 1
fi
echo "Please enter database VERSION (eg 5.5.60), CTRL+C to abort: "
read docker_mysqlversion
[ "${docker_mysqlversion}" == "" ] && echo "Error: Empty VERSION!" && exit 1
docker_superuser=root
docker_servicename="mysql-${docker_port}"
docker_pass=`tr -dc "a-zA-Z0-9" < /dev/urandom | head -c 10`
data_path="/var/lib/${docker_servicename}/data"
mkdir -p ${data_path}
echo "created: ${data_path}"
confd_path="/var/lib/${docker_servicename}/conf.d"
mkdir -p ${confd_path}
echo "created: ${confd_path}"
binlog_path="/backup/${docker_servicename}/binlogs"
mkdir -p ${binlog_path}
chmod -R 777 ${binlog_path}
echo "created: ${binlog_path}"
cat > ${confd_path}/options.cnf <<EOF
[mysql]
max_allowed_packet = 512M
[mysqld]
#skip-grant-tables
#server-id = 1
#log_bin = /var/log/mysql/mysql-bin.log
#relay_log = /var/log/mysql/mysqld-relay-bin
#expire_logs_days = 10
max_allowed_packet = 512M
innodb_buffer_pool_size = 8G
innodb_log_buffer_size = 512M
innodb_flush_log_at_trx_commit = 2
innodb_flush_method = O_DIRECT
innodb_file_per_table = 1
slave-skip-errors = 1062
[mysqldump]
max_allowed_packet = 512M
EOF
docker run --restart unless-stopped -d --name ${docker_servicename} -e MYSQL_ROOT_PASSWORD=${docker_pass} -e MYSQL_USER=${docker_superuser} -e MYSQL_PASSWORD=${docker_pass} -v "/etc/timezone:/etc/timezone:ro" -v "/etc/localtime:/etc/localtime:ro" -v ${binlog_path}:/var/log/mysql -v ${confd_path}:/etc/mysql/conf.d -v ${data_path}:/var/lib/mysql -p ${docker_port}:3306 mysql:${docker_mysqlversion}
cat >> ~/.my.cnf <<EOF
[client]
host = 127.0.0.1
port = ${docker_port}
user = ${docker_superuser}
password = ${docker_pass}
EOF
chmod 400 ~/.my.cnf
cat <<EOF
OK - new MySQL server version ${docker_mysqlversion} created:
host = 127.0.0.1
port = ${docker_port}
user = ${docker_superuser}
password = ${docker_pass} (saved to /root/.my.cnf)
config = ${confd_path}/options.cnf
binlogs = ${binlog_path}
data = ${data_path}
This information is copied to /etc/info.
EOF
cat >> /etc/info <<EOF
MySQL server version ${docker_mysqlversion} information:
host = 127.0.0.1
port = ${docker_port}
user = ${docker_superuser}
password = /root/.my.cnf
config = ${confd_path}/options.cnf
binlogs = ${binlog_path}
data = ${data_path}
EOF