Simple MySQL Initiation Script
一个简单的 MySQL 初始化脚本,用于配置虚机内部的 MySQL 服务,参考了 OpenStack
Trove
项目里的代码。
基本原理:
- 脚本放置与镜像中,基于该镜像创建的虚机启动的时候会运行该脚本
- 镜像中 MySQL 服务默认已安装好,同时已授权用户 os_admin 拥有超级用户权限
- MySQL 配置项通过
nova
的 config-driver 获取 - MySQL 的 datadir 跑在
cinder
创建的 volume 中,因此虚机会挂载好一块 volume - 通过参数 is_master 判断是否需要搭建主从,如果是从则需要额外配置
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
#!/usr/bin/env python
import json
import logging
import os
import signal
import subprocess
import sys
import time
import paramiko
import pexpect
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='/var/log/mysql_replication.log',
filemode='a')
basedir = ''
datadir = ''
innodb_buffer_pool_size = ''
max_connections = 100
port = 3306
pid_file = ''
log_error = ''
is_master = True
master_host = ''
master_port = 3306
master_user = 'mysql'
master_password = 'mysql1234'
MYSQL_DIR = '/mysql/mysql-5.6.21'
MYSQL_BIN_DIR = '/mysql/mysql-5.6.21/bin'
MASTER_MYSQL_SUPERUSER = 'os_admin'
MASTER_MYSQL_SUPERUSER_PASSWORD = 'EnzwtvvED76fBR4a6u86tRjNsKYtfMTRVApW'
REPLICATION_USER = 'reple'
REPLICATION_PASSWORD = 'replsel0919'
MYCNF_REPLMASTER = "/etc/mysql/conf.d/0replication.cnf"
CONFIG = """
[mysqld]
basedir = %s
datadir = %s
innodb_buffer_pool_size = %s
max_connections = %s
port = %s
pid-file = %s
log-error = %s
sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
!includedir /etc/mysql/conf.d/
"""
MASTER_CONFIG = """
[mysqld]
server-id = 10001
log_bin = %s/mysql-bin.log
"""
SLAVE_CONFIG = """
[mysqld]
server-id = 20001
log_bin = %s/mysql-bin.log
relay_log = %s/mysql-relay-bin.log
read_only = true
slave-skip-errors = 1049
"""
config = ''
overrides = ''
master_config = ''
slave_config = ''
TMP_MYCNF = "/tmp/my.cnf.tmp"
MYSQL_CONFIG = "/etc/my.cnf"
MYCNF_OVERRIDES = "/etc/mysql/conf.d/overrides.cnf"
MYCNF_OVERRIDES_TMP = "/tmp/overrides.cnf.tmp"
_ssh = None
def ssh_connect(host, user, password):
global _ssh
_ssh = paramiko.SSHClient()
_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
_ssh.connect(host, username=user, password=password)
def _read_paramimko_stream(recv_func):
result = ''
buf = recv_func(1024)
while buf != '':
result += buf
buf = recv_func(1024)
return result
def _execute_remote_command(cmd):
global _ssh
chan = _ssh.get_transport().open_session()
chan.exec_command(cmd)
stdout = _read_paramimko_stream(chan.recv)
ret_code = chan.recv_exit_status()
if ret_code:
print 'Remote run command Error: %s' % str(cmd)
return ret_code, stdout
class TimeoutError(Exception):
pass
class Timeout(object):
def __init__(self, seconds=1, error_message='Timeout'):
self.seconds = seconds
self.error_message = error_message
def handle_timeout(self, signum, frame):
raise TimeoutError(self.error_message)
def __enter__(self):
signal.signal(signal.SIGALRM, self.handle_timeout)
signal.alarm(self.seconds)
def __exit__(self, type, value, traceback):
signal.alarm(0)
def _execute_command(cmd, timeout=30):
with Timeout(timeout):
process = subprocess.Popen(cmd, shell=True,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE)
output, error = process.communicate()
return output, error
def enable_as_master():
_execute_remote_command('echo "%s" > %s' %
(master_config, MYCNF_REPLMASTER))
_execute_remote_command('/etc/init.d/mysql restart')
_execute_remote_command("%s -u %s -p%s -e '%s'" %
(MYSQL_BIN_DIR + '/mysql', MASTER_MYSQL_SUPERUSER, MASTER_MYSQL_SUPERUSER_PASSWORD,
'grant USAGE ON *.* to "%s"@"%%"' % REPLICATION_USER))
_execute_remote_command("%s -u %s -p%s -e '%s'" %
(MYSQL_BIN_DIR + '/mysql', MASTER_MYSQL_SUPERUSER, MASTER_MYSQL_SUPERUSER_PASSWORD,
'grant REPLICATION SLAVE ON *.* to "%s"@"%%" IDENTIFIED BY "%s"' % (REPLICATION_USER, REPLICATION_PASSWORD)))
def enable_as_slave():
_execute_command('echo "%s" > %s' %
(slave_config, MYCNF_REPLMASTER))
logging.debug('write slave config'')
stop_mysql()
start_mysql()
logging.debug('restart mysql ')
change_master_cmd = ("CHANGE MASTER TO MASTER_HOST='%(host)s', "
"MASTER_PORT=%(port)s, "
"MASTER_USER='%(user)s', "
"MASTER_PASSWORD='%(password)s', "
"MASTER_HEARTBEAT_PERIOD=10 " %
#"MASTER_LOG_FILE='%(log_file)s', "
#"MASTER_LOG_POS=%(log_pos)s" %
{
'host': master_host,
'port': master_port,
'user': REPLICATION_USER,
'password': REPLICATION_PASSWORD,
#'log_file': logging_config['log_file'],
#'log_pos': logging_config['log_position']
})
_execute_command('%s/mysql -e "%s"' % (MYSQL_BIN_DIR.rstrip('/'), change_master_cmd))
logging.debug('mysql change master cmd')
_execute_command('%s/mysql < %s' % (MYSQL_BIN_DIR.rstrip('/'), '/tmp/mysqldump.log'))
logging.debug('mysqldump cmd')
_execute_command('%s/mysql -e "start slave"' % (MYSQL_BIN_DIR.rstrip('/')))
logging.debug('mysql start slave cmd')
def get_snapshot():
dump_cmd = ("%s -u%s -p%s "
"--default-character-set=utf8 "
"--opt --extended-insert=false "
"--triggers -R --single-transaction "
"--master-data=1 "
"--all-databases "
"> /tmp/mysqldump.log" % ( MYSQL_BIN_DIR + '/mysqldump', MASTER_MYSQL_SUPERUSER, MASTER_MYSQL_SUPERUSER_PASSWORD))
rcode, rst = _execute_remote_command(dump_cmd)
if rcode:
print 'Remote mysqldump error'
ftp = _ssh.open_sftp()
try:
ftp.get('/tmp/mysqldump.log', '/tmp/mysqldump.log')
except IOError:
print 'Remote mysqldump.log not exist'
finally:
ftp.close()
def get_db_status():
out, err = _execute_command("%s/mysqladmin ping" % (MYSQL_BIN_DIR.rstrip('/')))
if err:
return 'shutdown'
else:
return 'running'
def wait_for_real_status_to_change_to(status, max_time):
WAIT_TIME = 3
waited_time = 0
while waited_time < max_time:
time.sleep(WAIT_TIME)
waited_time += WAIT_TIME
actual_status = get_db_status()
if actual_status == status:
return True
return False
def start_mysql():
try:
_execute_command("service mysql start")
except:
pass
if not wait_for_real_status_to_change_to('running', 30):
print "Start up of MySQL failed."
def stop_mysql():
try:
_execute_command("service mysql stop")
except:
pass
if not wait_for_real_status_to_change_to('shutdown', 30):
print "Error stopping MySQL."
def unmount_device(device_path):
mount_points = []
try:
cmd = "grep %s /etc/mtab | awk '{print $2}'" % device_path
stdout, stderr = _execute_command(cmd)
mount_points = stdout.strip().split('\n')
except:
pass
for mnt in mount_points:
if os.path.exists(mnt):
cmd = "umount %s" % mnt
child = pexpect.spawn(cmd)
child.expect(pexpect.EOF)
def format_device(device_path):
# check device status
check_cmd = "blockdev --getsize64 %s" % device_path
stdout, stderr = _execute_command(check_cmd)
if stderr:
print 'Error getting device status'
# format
cmd = "mkfs -t ext3 -m 5 %s" % device_path
child = pexpect.spawn(cmd, timeout=180)
child.expect(pexpect.EOF)
# check formatted
cmd = "dumpe2fs %s" % device_path
child = pexpect.spawn(cmd)
try:
i = child.expect(['has_journal', 'Wrong magic number'])
if i == 0:
return
raise
except pexpect.EOF:
raise
child.expect(pexpect.EOF)
def mount_device(device_path, mount_point):
if not os.path.exists(mount_point):
_execute_command("mkdir -p %s" % mount_point)
cmd = "mount %s %s" % (device_path, mount_point)
child = pexpect.spawn(cmd)
child.expect(pexpect.EOF)
def update_owner(user, group, path):
cmd = "chown -R %s:%s %s" % (user, group, path)
_execute_command(cmd)
def migrate_data(old_datadir, new_datadir):
cmd = "cp -R %s/* %s" % (old_datadir.rstrip('/'), new_datadir)
_execute_command(cmd)
def write_cnf(config_contents, overrides):
try:
with open(TMP_MYCNF, 'w') as t:
t.write(config_contents)
_execute_command("mv %s %s" % (TMP_MYCNF, MYSQL_CONFIG))
except Exception:
os.unlink(TMP_MYCNF)
raise
if overrides:
write_config_overrides(overrides)
def write_config_overrides(overrides):
with open(MYCNF_OVERRIDES_TMP, 'w') as overrides:
overrides.write(overrideValues)
_execute_command("mv %s %s" % (MYCNF_OVERRIDES_TMP, MYCNF_OVERRIDES))
_execute_command("chmod 0644 %s" % MYCNF_OVERRIDES)
def start_master():
stop_mysql()
unmount_device('/dev/vdb')
format_device('/dev/vdb')
logging.debug('format device successful')
mount_device('/dev/vdb', '/mydata/')
logging.debug('mount device successful')
migrate_data('/mysql/mysql-5.6.21/data/', datadir)
logging.debug('migrate data successful')
update_owner('mysql', 'mysql', datadir)
write_cnf(config, overrides)
logging.debug('write cnf successful')
start_mysql()
logging.debug('start master successful')
def start_slave():
start_master()
ssh_connect(master_host, master_user, master_password)
logging.debug('ssh connect successful')
enable_as_master()
logging.debug('remote enable as master successful')
get_snapshot()
logging.debug('get snapshot successful')
enable_as_slave()
logging.debug('enable as slaver successful')
def read_config(faker=False):
if faker:
meta = {'basedir': '/mysql/mysql-5.6.21',
'datadir': '/mydata/',
'innodb_buffer_pool_size': '4G',
'max_connections': 800,
'master_ip': '',
'master_port': 3306,
'master_machine_root_password': 'KeyTone123',
'pid-file': '/mydata/ucoasdb.pid',
'log-error': '/mydata/ucoasdb.err',
'is_master': True
}
else:
config_driver = '/dev/disk/by-label/config-2'
if not os.path.exists(config_driver):
logging.warning('config driver file does not exist')
raise
mount_point = '/mnt/config'
mount_device(config_driver, mount_point)
with open('/mnt/config/openstack/latest/meta_data.json', 'r') as f:
json_config = f.read()
unmount_device('/dev/disk/by-label/config-2')
dict_config = json.loads(json_config)
meta = dict_config['meta']
logging.debug('config meta: %s' % str(meta))
global basedir, datadir, innodb_buffer_pool_size, max_connections, port, \
pid_file, log_error, is_master, config, overrides, master_config, \
slave_config, master_host, master_port, master_user, master_password
basedir = meta.get('basedir', '/mysql/mysql-5.6.21')
datadir = meta.get('datadir', '/mydata/')
innodb_buffer_pool_size = meta.get('innodb_buffer_pool_size', '4G')
max_connections = meta.get('max_connections', 800)
port = meta.get('port', 3306)
pid_file = meta.get('pid-file', '/mydata/ucoasdb.pid')
log_error = meta.get('log-error', '/mydata/ucoasdb.err')
is_master = meta.get('is_master', True)
is_master = True if is_master in ('True', 'true', True) else False
config = CONFIG % (basedir, datadir, innodb_buffer_pool_size,
max_connections, port, pid_file, log_error)
overrides = None
master_config = MASTER_CONFIG % datadir.rstrip('/')
slave_config = SLAVE_CONFIG % (datadir.rstrip('/'), datadir.rstrip('/'))
master_host = meta.get('master_ip')
master_port = meta.get('master_port')
master_user = 'mysql'
master_password = 'mysql1234'
def delete_me():
_execute_command("mv %s %s" % ('/root/mysql_replication.py', '/root/.mysql_replication.py'))
if __name__ == '__main__':
try:
read_config(faker=False)
except:
logging.warning('Read config file failed.')
sys.exit()
if is_master:
logging.debug('start as master')
start_master()
else:
logging.debug('start as slaver')
start_slave()
delete_me()
This post is licensed under CC BY 4.0 by the author.