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
|
#!/bin/sh
# This script makes Maatkit sandbox servers for mk-test-env.
# It's a "low level" script that is not usually called directly.
# Exit 0 means everything was successful, else exit 1 on problems.
die() {
echo $1 >&2
exit 1
}
make_sandbox() {
# Make the sandbox dir and extract the base files.
mkdir /tmp/$port
if [ $? -ne 0 ]; then
die "mkdir /tmp/$port failed"
fi
cp $PERCONA_TOOLKIT_BRANCH/sandbox/servers/$version/my.sandbox.cnf /tmp/$port
tar xzf $PERCONA_TOOLKIT_BRANCH/sandbox/servers/$version/data.tar.gz -C /tmp/$port
for script in `ls $PERCONA_TOOLKIT_BRANCH/sandbox/servers/*`; do
if [ -f $script ]; then
cp $script /tmp/$port
fi
done
for file in `grep -rl PORT /tmp/$port`; do
sed -e "s/PORT/$port/g" -i.bak $file
# Use ! instead of / because the replacment has / (it's a directory)
sed -e "s!PERCONA_TOOLKIT_SANDBOX!$PERCONA_TOOLKIT_SANDBOX!g" -i.bak $file
done
rm /tmp/$port/*.bak >/dev/null 2>&1
if [ -n "$BINLOG_FORMAT" ]; then
echo "binlog-format=$BINLOG_FORMAT" >> /tmp/$port/my.sandbox.cnf
fi
if [ -n "$SLAVE_EXEC_MODE" ]; then
echo "slave_exec_mode=$SLAVE_EXEC_MODE" >> /tmp/$port/my.sandbox.cnf
fi
if [ -n "$SQL_MODE" ]; then
echo "sql-mode=$SQL_MODE" >> /tmp/$port/my.sandbox.cnf
fi
if [ -n "$GENLOG" ]; then
echo "log=genlog" >> /tmp/$port/my.sandbox.cnf
fi
if [ -n "$SKIP_INNODB" ]; then
echo "skip-innodb" >> /tmp/$port/my.sandbox.cnf
fi
# Start the sandbox and check that it has InnoDB.
/tmp/$port/start
if [ $? -eq 0 ]; then
if [ -z "$SKIP_INNODB" ]; then
/tmp/$port/use -e 'SHOW /*!40100 ENGINE*/ INNODB STATUS' | grep 'INNODB MONITOR OUTPUT' >/dev/null 2>&1
# grep exits 0 if lines are found
if [ $? -ne 0 ]; then
echo "****** WARNING sandbox doesn't have a working InnoDB! ******" >&2
cat /tmp/$port/data/mysqld.log >&2
exit 1
fi
fi
else
echo "Sandbox $type $port failed to start." >&2
cat /tmp/$port/data/mysqld.log >&2
exit 1
fi
return 0
}
# ###########################################################################
# Sanity check the cmd line options.
# ###########################################################################
if [ $# -lt 2 ]; then
die "Usage: start-sandbox master|slave|master-master port [master port]"
fi
type=$1 # master, slave or master-master
port=$2 # sandbox port number, e.g. 12345
master_port=$3 # master port if slave or master-master
if [ "$type" != "master" ] && [ "$type" != "slave" ] && [ "$type" != "master-master" ]; then
die "Invalid sandbox type: $type. Valid types are master, slave, and master-master."
fi
if [ $port -le 1024 ]; then
die "Invalid port: $port. Ports must be > 1024."
fi
if [ "$type" = "slave" -o "$type" = "master-master" ] && [ -z "$master_port" ]; then
die "No master port given for the $type."
fi
# If creating a slave, the master must exist first. Not true for creating
# a master-master though.
if [ "$type" = "slave" ] && [ ! -d "/tmp/$master_port" ]; then
die "Master sandbox does not exist: /tmp/$master_port"
fi
# ###########################################################################
# Sanity check the environment.
# ###########################################################################
if [ -z "$PERCONA_TOOLKIT_BRANCH" ]; then
die "PERCONA_TOOLKIT_BRANCH environment variable is not set."
fi
if [ ! -d "$PERCONA_TOOLKIT_BRANCH" ]; then
die "Invalid PERCONA_TOOLKIT_BRANCH directory: $PERCONA_TOOLKIT_BRANCH"
fi
cd $PERCONA_TOOLKIT_BRANCH/sandbox
# This script is usually called by mk-test-env which discovers and
# sets PERCONA_TOOLKIT_SANDBOX. If this script is called directly,
# then the caller is reponsible for setting PERCONA_TOOLKIT_SANDBOX.
# PERCONA_TOOLKIT_SANDBOX points to a base directory containing the
# MySQL executables like PERCONA_TOOLKIT_SANDBOX/bin/mysqld_safe.
if [ -z "$PERCONA_TOOLKIT_SANDBOX" ]; then
PERCONA_TOOLKIT_SANDBOX=`./mk-test-env checkconfig | grep PERCONA_TOOLKIT_SANDBOX | cut -d= -f2 | awk '{print $1}'`
if [ -z "$PERCONA_TOOLKIT_SANDBOX" ]; then
die "PERCONA_TOOLKIT_SANDBOX environment variable is not set."
fi
fi
# ###########################################################################
# Get server version.
# ###########################################################################
if [ -x "$PERCONA_TOOLKIT_SANDBOX/bin/mysqld" ]; then
mysqld="$PERCONA_TOOLKIT_SANDBOX/bin/mysqld"
elif [ -x "$PERCONA_TOOLKIT_SANDBOX/sbin/mysqld" ]; then
mysqld="$PERCONA_TOOLKIT_SANDBOX/sbin/mysqld"
elif [ -x "$PERCONA_TOOLKIT_SANDBOX/libexec/mysqld" ]; then
mysqld="$PERCONA_TOOLKIT_SANDBOX/libexec/mysqld"
else
die "Cannot find executable mysqld in $PERCONA_TOOLKIT_SANDBOX/bin, $PERCONA_TOOLKIT_SANDBOX/sbin or $PERCONA_TOOLKIT_SANDBOX/libexec."
fi
version=`$mysqld -V | awk '{print $3}' | cut -d. -f 1,2`;
if [ ! -d "$PERCONA_TOOLKIT_BRANCH/sandbox/servers/$version" ]; then
die "$PERCONA_TOOLKIT_BRANCH/sandbox/servers/$version does not exist."
fi
# ###########################################################################
# Start and configure the sandbox server.
# ###########################################################################
PIDFILE="/tmp/$port/data/mysql_sandbox$port.pid"
if [ -f $PIDFILE ]; then
echo "Sandbox $port already started (found pid file $PIDFILE)"
else
make_sandbox
# If the sandbox is a slave, start the slave.
if [ "$type" = "slave" ]; then
/tmp/$port/use -e "change master to master_host='127.0.0.1', master_user='msandbox', master_password='msandbox', master_port=$master_port"
/tmp/$port/use -e "start slave"
/tmp/$port/use -e "set global read_only=1"
fi
# If the sandbox is a master-master, start the second master and slave the
# two together.
if [ "$type" = "master-master" ]; then
mm1_port=$port
mm2_port=$master_port
port=$master_port # make_sandbox uses $port
make_sandbox
# Slave mm2 -> mm1
/tmp/$mm2_port/use -e "change master to master_host='127.0.0.1', master_log_file='mysql-bin.000001', master_log_pos=0, master_user='msandbox', master_password='msandbox', master_port=$mm1_port"
/tmp/$mm2_port/use -e "start slave"
# Slave mm1 -> mm2
/tmp/$mm1_port/use -e "change master to master_host='127.0.0.1', master_log_file='mysql-bin.000001', master_log_pos=0, master_user='msandbox', master_password='msandbox', master_port=$mm2_port"
/tmp/$mm1_port/use -e "start slave"
fi
fi
exit $?
|