-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmysql-ramdisk.py
169 lines (142 loc) · 6.57 KB
/
mysql-ramdisk.py
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
#!/usr/bin/env python
"""
A helper program for managing the birth, life, and death of a MySQL ramdisk.
Thank you Wayne Moore at http://kotega.com/ for the following article:
"Running MySQL in a ramdisk on OS X"
http://kotega.com/blog/2010/apr/12/mysql-ramdisk-osx/
"""
import platform
from optparse import OptionGroup, OptionParser
from subprocess import Popen, PIPE, call
# Overrideable defaults for ramdisk:
RAMDISK_SIZE = 128 # default to 128 MB. override with -s option
RAMDISK_PATH = '/dev/disk3' # overide with -p option
def main():
RAMDISK_SIZE = 128 # default to 128 MB. override with -s option
RAMDISK_PATH = '/dev/disk3' # overide with -p option
if is_linux():
RAMDISK_PATH = '/mnt/ramdisk'
parser = OptionParser()
# Option group for killing ramdisk:
group_kill_ramdisk = OptionGroup(parser, 'The death of a ramdisk',
'Short for `hdiutil detach /dev/disk1`'
' with some extra handling and default'
' location of %s.' % RAMDISK_PATH)
group_kill_ramdisk.add_option('-k', '--kill-ramdisk', action='store_true',
dest='kill_ramdisk')
group_kill_ramdisk.add_option('-p', '--path-to-ramdisk', type='string',
default=RAMDISK_PATH,
dest='path_to_ramdisk')
parser.add_option_group(group_kill_ramdisk)
# Option group for creating ramdisk (and maybe loading it up with mysql):
group_create_ramdisk = OptionGroup(parser, 'The birth of a ramdisk',
'Creates a ramdisk, installs,'
' and starts MySQL')
group_create_ramdisk.add_option('-c', '--create-ramdisk',
action='store_true',
dest='create_ramdisk')
group_create_ramdisk.add_option('-s', '--ramdisk-size',
default=RAMDISK_SIZE,
type='int', dest='ramdisk_size')
group_create_ramdisk.add_option('-a', '--disable-apparmor',
action='store_true',
dest='apparmor')
# I group installing and starting the mysql db here out of convenience.
# TODO create an option for Solr here too.
group_create_ramdisk.add_option('-m', '--with-mysql',
action='store_true',
dest='with_mysql')
parser.add_option_group(group_create_ramdisk)
(options, args) = parser.parse_args()
if options.create_ramdisk:
create_ramdisk(calc_ramdisk_size(options.ramdisk_size),
options.path_to_ramdisk)
if options.with_mysql:
if options.apparmor:
disable_apparmor()
install_db(options.path_to_ramdisk)
start_db(options.path_to_ramdisk)
elif options.kill_ramdisk:
kill_ramdisk(options.path_to_ramdisk)
def is_linux():
" Return true if os type is linux, and false otherwise. "
return platform.system() == 'Linux'
def disable_apparmor():
if is_linux():
call(['sudo aa-complain mysqld'], shell=True)
def calc_ramdisk_size(num_megabytes):
return num_megabytes * 1048576 / 512 # MB * MiB/KB;
def create_ramdisk(ramdisk_size, disk_path=RAMDISK_PATH):
print('Creating ramdisk... at %s' % disk_path)
if is_linux():
size_in_mb = int(ramdisk_size * 512 / 1048576)
# TODO: What if dir already exists? Destroy data?
call(['sudo mkdir -p %s' % disk_path], shell=True)
call(['sudo mount -t tmpfs -o size=%sm tmpfs %s' % (size_in_mb, disk_path)],
shell=True)
else:
disk_path = Popen('hdiutil attach -nomount ram://%s' % ramdisk_size,
stdout=PIPE, shell=True).communicate()[0]
Popen('diskutil eraseVolume HFS+ ramdisk %s' % disk_path, stdout=PIPE,
shell=True).communicate()
print('Done creating ramdisk: %s' % disk_path)
def kill_ramdisk(path_to_ramdisk):
# TODO make sure that MySQL isn't running first!
if is_linux():
# TODO:
# make this use path_to_ramdisk.
print("Unmounting ramdisk '%s'." % path_to_ramdisk)
# Nonzero return codes are bad. Wouldn't want to accidently remove rm anything
# other than the ramdisk.
if call(['sudo umount %s' % path_to_ramdisk], shell=True):
raise Exception("Unmounting of ramdisk at '%s' failed." \
% path_to_ramdisk)
print("Deleting ramdisk '%s'." % path_to_ramdisk)
call(['sudo rm -rf %s' % path_to_ramdisk], shell=True)
else:
print(Popen(('hdiutil detach %s' % path_to_ramdisk), stdout=PIPE,
shell=True).communicate()[0])
def install_db(path_to_ramdisk=None):
# TODO support other dbs?
print('Installing new db...')
if is_linux():
call(['/usr/bin/mysql_install_db '
'--user=mysql '
'--basedir=/usr '
'--datadir=%s' % path_to_ramdisk], shell=True)
call(['sudo chmod 777 -R %s' % path_to_ramdisk], shell=True)
else:
p = Popen('/usr/local/mysql/scripts/mysql_install_db '
'--user=mysql '
'--basedir=/usr/local/mysql '
'--datadir=/Volumes/ramdisk', shell=True)
p.communicate()
print('Done installing db.' )
def start_db(path_to_ramdisk=None):
# TODO support other dbs?
# TODO make it more configurable?
print('Starting db...')
if is_linux():
call(['sudo mysqld '
'--basedir=/usr '
'--datadir=%s '
'--user=mysql '
'--log-error=%s/mysql.ramdisk.err '
'--pid-file=%s/mysql.ramdisk.pid '
'--port=3308 '
'--socket=/tmp/mysql.ramdisk.sock &' % ((path_to_ramdisk,) * 3)],
stdout=PIPE, shell=True)
print("To log into mysql use: 'msyql --socket=/tmp/mysql.ramdisk.sock [OPTIONS]'")
else:
p = Popen('/usr/local/mysql/bin/mysqld '
'--basedir=/usr/local/mysql '
'--datadir=/Volumes/ramdisk '
'--user=mysql '
'--log-error=/Volumes/ramdisk/mysql.ramdisk.err '
'--pid-file=/Volumes/ramdisk/mysql.ramdisk.pid '
'--port=3308 '
'--socket=/tmp/mysql.sock &', stdout=PIPE, shell=True)
p.communicate()
print('Done starting db.')
if __name__ == "__main__":
main()