forked from aliyun/ros-templates
-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy-mysql-based-on-ebs.yml
443 lines (351 loc) · 11.3 KB
/
deploy-mysql-based-on-ebs.yml
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
ROSTemplateFormatVersion: '2015-09-01'
Description:
zh-cn: 基于EBS ESSD部署高性能的MySQL服务
en: Deploy high-performance MySQL services based on EBS ESSD.
Parameters:
MySQLPassword:
Default: ''
NoEcho: true
Type: String
Label:
zh-cn: 数据库密码
en: Mysql Password
EcsType:
Default: ecs.g6.large
AssociationProperty: 'ALIYUN::ECS::Instance::InstanceType'
AssociationPropertyMetadata:
SystemDiskCategory: cloud_essd
InstanceChargeType: PostPaid
ZoneId: '${ZoneId}'
Type: String
Label:
zh-cn: ECS 实例规格
en: InstanceType
ZoneId:
Default: ''
AssociationProperty: 'ALIYUN::ECS::Instance::ZoneId'
Type: String
Label:
zh-cn: 可用区
en: Availability Zone
EcsPassword:
Type: String
Description:
zh-cn: >-
服务器登录密码,长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/
中的特殊符号)
en: >-
Server login password, Length 8-30, must contain three(Capital letters,
lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special
symbol in)
Default: null
MinLength: 8
Label:
zh-cn: 实例密码
en: Instance Password
AllowedPattern: '^[a-zA-Z0-9-\(\)\`\~\!\@\#\$\%\^\&\*\_\-\+\=\|\{\}\[\]\:\;\<\>\,\.\?\/]*$'
MaxLength: 30
AssociationProperty: 'ALIYUN::ECS::Instance::Password'
ConstraintDescription:
zh-cn: '长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;''<>,.?/ 中的特殊符号)'
en: >-
Length 8-30, must contain three(Capital letters, lowercase letters,
numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in)
Outputs:
EcsLoginAddress:
Description:
zh-cn: Ecs登陆地址。
en: Ecs login address.
Value:
'Fn::Sub':
- >-
https://ecs-workbench.aliyun.com/?from=EcsConsole&instanceType=ecs®ionId=${Region}&instanceId=${InstanceId}
- InstanceId:
Ref: EcsInstance
Region:
Ref: 'ALIYUN::Region'
EcsInstanceId:
Description: ECS实例ID
Value:
'Fn::GetAtt':
- EcsInstance
- InstanceId
EcsInstanceUser:
Description: ECS实例初始用户
Value: root
EcsInstancePublicIp:
Description: ECS实例公网IP
Value:
'Fn::GetAtt':
- EcsInstance
- PublicIp
MysqlUser:
Description: MySQL初始用户
Value: root
DataDisk:
Description: 数据盘ID
Value:
'Fn::GetAtt':
- DataDisk
- DiskId
Resources:
DataDiskAttachment:
Type: 'ALIYUN::ECS::DiskAttachment'
Properties:
InstanceId:
Ref: EcsInstance
DiskId:
Ref: DataDisk
DependsOn:
- EcsInstance
- DataDisk
SecurityGroup:
Type: 'ALIYUN::ECS::SecurityGroup'
Properties:
SecurityGroupIngress:
- Priority: 1
PortRange: '-1/-1'
NicType: intranet
SourceCidrIp: 0.0.0.0/0
IpProtocol: all
VpcId:
Ref: VPC
SecurityGroupEgress:
- Priority: 1
PortRange: '-1/-1'
DestCidrIp: 0.0.0.0/0
NicType: intranet
IpProtocol: all
VPC:
Type: 'ALIYUN::ECS::VPC'
Properties:
VpcName:
'Fn::Join':
- '-'
- - StackId
- Ref: 'ALIYUN::StackId'
CidrBlock: 192.168.0.0/16
WriteCommand:
Type: 'ALIYUN::ECS::RunCommand'
Properties:
Type: RunShellScript
CommandContent:
'Fn::Join':
- ''
- - >
echo '
# 1. Get data disk label
DEV_LABEL=vdb
# 2. Check if data disk exists
if [ ! -e /dev/${DEV_LABEL} ]; then
echo "[ERROR] /dev/${DEV_LABEL} not exist. Please check if data disk label is vdb."
exit
fi
# 3. Set MySQL home
MYSQL_HOME="/root/${DEV_LABEL}"
if [ ! -d ${MYSQL_HOME} ]; then
if mkdir -p ${MYSQL_HOME}; then
echo "[INFO] Create directory ${MYSQL_HOME} successfully."
else
echo "[ERROR] Failed to create directory ${MYSQL_HOME}."
exit
fi
fi
echo "[INFO] Using /dev/${DEV_LABEL} as the MySQL data home."
# 4. Prevent virtio io split
echo 256 >/sys/block/${DEV_LABEL}/queue/max_sectors_kb
echo "[INFO] Set max sectors kb successfully."
# 5. mkfs with bigalloc enabled
umount /dev/${DEV_LABEL}
echo y | mkfs.ext4 -O bigalloc -C 16k /dev/${DEV_LABEL}
echo "[INFO] mkfs with bigalloc enabled successfully."
# 6. Install MySQL
cd /tmp/
if wget -N
http://mirrors.cloud.aliyuncs.com/mysql/MySQL-8.0/mysql-8.0.27-1.el7.x86_64.rpm-bundle.tar
-O mysql.tar ; then
echo "[INFO] Download mysql rpm bundle successfully."
else
echo "[ERROR] Failed to download mysql rpm bundle."
exit
fi
tar -xf mysql.tar
echo "[INFO] Extract mysql rpm bundle successfully."
if yum install -y
mysql-community-{server,client,common,libs,devel}-*; then
echo "[INFO] Install mysql successfully."
else
echo "[ERROR] Failed to install mysql."
exit
fi
# 7. Create MySQL directories
mount /dev/${DEV_LABEL} ${MYSQL_HOME}
mkdir -p ${MYSQL_HOME}/data/dbs
mkdir -p ${MYSQL_HOME}/data/mysql
mkdir -p ${MYSQL_HOME}/log/mysql
mkdir -p ${MYSQL_HOME}/log/redo
echo "[INFO] Create MySQL directories successfully."
# 8. Add mysql group and user
systemctl stop mysqld
groupadd mysql
useradd -g mysql mysql
chown -R mysql:mysql ${MYSQL_HOME}
echo "[INFO] Add mysql group and user successfully."
# 9. Turn off doublewrite
cat <<EOF | echo "[mysqld]
port = #{MYSQL_PORT}
back_log = 3000
datadir = ${MYSQL_HOME}/data/dbs
log-bin = ${MYSQL_HOME}/log/mysql/mysql-bin.log
log-error = ${MYSQL_HOME}/log/mysql/master-error.log
innodb_data_home_dir = ${MYSQL_HOME}/data/mysql/
innodb_log_group_home_dir = ${MYSQL_HOME}/log/redo/
innodb_doublewrite_dir = ${MYSQL_HOME}/doublewrite
character_set_server = utf8
sync_binlog = 1000
innodb_flush_log_at_trx_commit = 2
innodb_doublewrite = 0
innodb_flush_method=O_DIRECT
default_authentication_plugin = mysql_native_password"
>/etc/my.cnf
EOF
# 10. Init MySQL SQL
cat <<EOF | echo "ALTER USER 'root'@'localhost' IDENTIFIED WITH
mysql_native_password BY '\'#{MYSQL_PASSWORD}\'';
create user root@'\'%\'' identified by '\'#{MYSQL_PASSWORD}\'';
grant all privileges on *.* to root@'\'%\'';
grant all privileges on *.* to root@'\'%\'' with grant option;
" >${MYSQL_HOME}/prepare.sql
EOF
# 11. Start MySQL
killall -9 mysqld
mysqld --defaults-file=/etc/my.cnf --initialize
mysqld --user=root --daemonize
mysql -uroot -p"$(cat ${MYSQL_HOME}/log/mysql/master-error.log |
grep root | grep "temporary password" | awk '\'{print \$13}\'' |
xargs -n 1 echo)" --connect-expired-password
<${MYSQL_HOME}/prepare.sql
echo '[INFO] Prepare MySQL successfully.'
' > /tmp/prepare_mysql.sh
echo '#!/bin/bash
# chkconfig: 345 20 80
# description: EBS MySQL 16K Auto Start Script
start() {
DEV_LABEL=vdb
MYSQL_HOME=/root/${DEV_LABEL}
echo 256 >/sys/block/vdb/queue/max_sectors_kb
umount /dev/${DEV_LABEL}
mount /dev/${DEV_LABEL} ${MYSQL_HOME}
mysqld --user=root --daemonize
}
stop() {
pgrep mysqld | xargs kill
echo "Stopping mysqld..."
}
restart() {
stop
sleep 2
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0
' > /etc/rc.d/init.d/ebs_mysql_16k_auto_start.sh
cd /etc/rc.d/init.d
chmod +x /etc/rc.d/init.d/ebs_mysql_16k_auto_start.sh
sudo chkconfig --add ebs_mysql_16k_auto_start.sh
sudo chkconfig ebs_mysql_16k_auto_start.sh on
Sync: true
InstanceIds:
- Ref: EcsInstance
DependsOn:
- DataDiskAttachment
VSwitch:
Type: 'ALIYUN::ECS::VSwitch'
Properties:
VSwitchName:
Ref: 'ALIYUN::StackName'
VpcId:
Ref: VPC
CidrBlock: 192.168.0.0/24
ZoneId:
Ref: ZoneId
RunCommand:
Type: 'ALIYUN::ECS::RunCommand'
Properties:
Type: RunShellScript
CommandContent:
'Fn::Sub':
- >
#!/bin/sh
sed -i "s/#{MYSQL_PASSWORD}/${MySQLPassword}/g;
s/#{MYSQL_PORT}/3306/g;" /tmp/prepare_mysql.sh
sh /tmp/prepare_mysql.sh | tee ~/prepare_mysql.log
- {}
Sync: true
InstanceIds:
- Ref: EcsInstance
Timeout: 600
DependsOn:
- WriteCommand
EcsInstance:
Type: 'ALIYUN::ECS::Instance'
Properties:
SystemDiskCategory: cloud_essd
VpcId:
Ref: VPC
InternetMaxBandwidthOut: 100
SecurityGroupId:
Ref: SecurityGroup
SystemDiskSize: 40
ImageId: CentOS7_9
AllocatePublicIP: true
VSwitchId:
Ref: VSwitch
InternetChargeType: PayByTraffic
IoOptimized: optimized
Password:
Ref: EcsPassword
InstanceType:
Ref: EcsType
ZoneId:
Ref: ZoneId
DataDisk:
Type: 'ALIYUN::ECS::Disk'
Properties:
DiskCategory: cloud_essd
ZoneId:
Ref: ZoneId
PerformanceLevel: PL0
Size: 100
DependsOn:
- EcsInstance
Metadata:
'ALIYUN::ROS::Interface':
ParameterGroups:
- Parameters:
- ZoneId
- EcsType
- EcsPassword
Label:
zh-cn: 基础配置
en: Basic Configuration
- Parameters:
- MySQLPassword
Label:
zh-cn: 数据库配置
en: DataBase Configuration
TemplateTags:
- 'acs:solution:基于EBS部署高性能的MySQL服务'