forked from miracle2k/linuxutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysqld-ram.sh
executable file
·109 lines (90 loc) · 3.09 KB
/
mysqld-ram.sh
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
#!/usr/bin/env bash
# Sets up and runs a MySQL instance using a ramdisk.
#
# This can dramatically speed up things like running tests (The
# Django test suite takes 455 secs with this, 7843 secs without
# it on my machine - that's almost factor 20).
#
# Written and tested on Ubuntu Karmic.
#
# TODO: Possible things could be even faster; options to consider:
# * DELAY_KEY_WRITE
# * Disable logging
# * Disable binary logging
# * Possibly small memory caches in MySQL help performance here.
set -o nounset
set -o errexit
# You probably want to bind to somewhere non-default so you
# don't have to shutdown your standard MySQL instance.
# TODO: Make those options.
BIND_SOCKET=/var/run/mysqld/mysqld-ram.sock
BIND_HOST=127.0.0.1
BIND_PORT=3307
DATA_DIR=/var/lib/mysql-ram
PID_FILE=/var/run/mysqld/mysqld-ram.pid
USER=mysql
GROUP=mysql
MYSQL_APPARMOR_PROFILE=/etc/apparmor.d/usr.sbin.mysqld
get_bind_args() {
# pass "server" for use with mysqld
host_option='--host'
if [ ${1?"get_bind_args() needs one argument"} = "server" ]; then
host_option="--bind";
fi
args=""
if [ -n "${BIND_SOCKET:+x}" ]; then args="$args --socket=${BIND_SOCKET}"; fi
if [ -n "${BIND_HOST:+x}" ]; then args="$args $host_option=${BIND_HOST}"; fi
if [ -n "${BIND_PORT:+x}" ]; then args="$args --port=${BIND_PORT}"; fi
echo $args
}
# If not yet done, setup a ram disk in the data directory.
if [ ! -d $DATA_DIR ]; then
echo "Creating directory at $DATA_DIR"
mkdir $DATA_DIR
chown -R $USER:$GROUP $DATA_DIR
fi
# We're now going to do stuff we don't want to be persistent,
# so make sure we are going to properly cleanup.
cleanup() {
# Run without errexit, we want to do as much cleanup
# as possible.
set +e
# Unmount ramdisk
if mountpoint -q $DATA_DIR; then
echo "Unmounting ramdisk..."
umount $DATA_DIR
fi
set -e
exit
}
trap cleanup INT TERM EXIT
# If the ram disk is not yet mounted, do so now.
if ! mountpoint -q $DATA_DIR; then
echo "Mounting ramdisk at $DATA_DIR"
mount -t tmpfs none $DATA_DIR
fi
# If AppArmor protects MySQL, it'll have to stop doing that
# for the time being.
if [ -f $MYSQL_APPARMOR_PROFILE ]; then
echo "Disabling AppArmor..."
apparmor_parser -R $MYSQL_APPARMOR_PROFILE
fi
# Setup the new mysql data directory
mysql_install_db --user $USER --datadir=$DATA_DIR > /dev/null
# Run mysqld; we need to workaround it not reacting to CTRL+C.
# Let's setup traps to shut it down ourselves.
trap '/usr/bin/mysqladmin $(get_bind_args client) refresh & wait' 1 # HUP
trap '/usr/bin/mysqladmin $(get_bind_args client) shutdown & wait' 2 3 15 # INT QUIT and TERM
# Run MySQL in the background.
mysqld $(get_bind_args server) --datadir="$DATA_DIR" --pid-file="$PID_FILE" --console &
# Enable apparmor again right away; it's enough that we
# started up the mysqld without the profile.
if [ -f $MYSQL_APPARMOR_PROFILE ]; then
echo "Re-enabling AppArmor..."
apparmor_parser -a $MYSQL_APPARMOR_PROFILE
fi
# Wait for the MySQL background process to end.
wait
# Call cleanup manually
trap - INT TERM EXIT
cleanup