Rewrite find_my_cnf_file() so if a port is given only that port's cnf is returned, else nothing.

This commit is contained in:
Daniel Nichter
2012-10-24 11:17:34 -06:00
parent 15a2cf6c50
commit aacdd9db17
4 changed files with 63 additions and 35 deletions

View File

@@ -793,22 +793,21 @@ find_my_cnf_file() {
local port="${2:-""}"
local cnf_file=""
if test -n "$port" && grep -- "/mysqld.*--port=$port" "${file}" >/dev/null 2>&1 ; then
cnf_file="$(grep -- "/mysqld.*--port=$port" "${file}" \
| awk 'BEGIN{RS=" "; FS="=";} $1 ~ /--defaults-file/ { print $2; }' \
| head -n1)"
if [ "$port" ]; then
cnf_file="$(grep --max-count 1 "/mysqld.*--port=$port" "$file" \
| awk 'BEGIN{RS=" "; FS="=";} $1 ~ /--defaults-file/ { print $2; }')"
else
cnf_file="$(grep '/mysqld' "${file}" \
| awk 'BEGIN{RS=" "; FS="=";} $1 ~ /--defaults-file/ { print $2; }' \
| head -n1)"
cnf_file="$(grep --max-count 1 '/mysqld' "$file" \
| awk 'BEGIN{RS=" "; FS="=";} $1 ~ /--defaults-file/ { print $2; }')"
fi
if [ ! -n "${cnf_file}" ]; then
cnf_file="/etc/my.cnf";
if [ ! -e "${cnf_file}" ]; then
cnf_file="/etc/mysql/my.cnf";
fi
if [ ! -e "${cnf_file}" ]; then
if [ -z "$cnf_file" ]; then
if [ -e "/etc/my.cnf" ]; then
cnf_file="/etc/my.cnf"
elif [ -e "/etc/mysql/my.cnf" ]; then
cnf_file="/etc/mysql/my.cnf"
elif [ -e "/var/db/mysql/my.cnf" ]; then
cnf_file="/var/db/mysql/my.cnf";
fi
fi

View File

@@ -60,23 +60,24 @@ find_my_cnf_file() {
local port="${2:-""}"
local cnf_file=""
if test -n "$port" && grep -- "/mysqld.*--port=$port" "${file}" >/dev/null 2>&1 ; then
cnf_file="$(grep -- "/mysqld.*--port=$port" "${file}" \
| awk 'BEGIN{RS=" "; FS="=";} $1 ~ /--defaults-file/ { print $2; }' \
| head -n1)"
if [ "$port" ]; then
# Find the cnf file for the specific port.
cnf_file="$(grep --max-count 1 "/mysqld.*--port=$port" "$file" \
| awk 'BEGIN{RS=" "; FS="=";} $1 ~ /--defaults-file/ { print $2; }')"
else
cnf_file="$(grep '/mysqld' "${file}" \
| awk 'BEGIN{RS=" "; FS="=";} $1 ~ /--defaults-file/ { print $2; }' \
| head -n1)"
# Find the cnf file for the first mysqld instance.
cnf_file="$(grep --max-count 1 '/mysqld' "$file" \
| awk 'BEGIN{RS=" "; FS="=";} $1 ~ /--defaults-file/ { print $2; }')"
fi
if [ ! -n "${cnf_file}" ]; then
# "Cannot autodetect config file, trying common locations"
cnf_file="/etc/my.cnf";
if [ ! -e "${cnf_file}" ]; then
cnf_file="/etc/mysql/my.cnf";
fi
if [ ! -e "${cnf_file}" ]; then
if [ -z "$cnf_file" ]; then
# Cannot autodetect config file, try common locations.
if [ -e "/etc/my.cnf" ]; then
cnf_file="/etc/my.cnf"
elif [ -e "/etc/mysql/my.cnf" ]; then
cnf_file="/etc/mysql/my.cnf"
elif [ -e "/var/db/mysql/my.cnf" ]; then
cnf_file="/var/db/mysql/my.cnf";
fi
fi

View File

@@ -79,26 +79,50 @@ is \
"collect_internal_vars works"
# find_my_cnf_file
# We know the port is 12345 (2nd to last test), but the sandbox is started
# with just --defaults-file, no --port, so find_my_cnf_file isn't going to
# be able to get the specific cnf file, and the test machine shouldn't have
# any of the default files (/etc/my.cnf, etc.).
cnf_file=$(find_my_cnf_file "$p/mysqld-instances" ${port});
is \
"$cnf_file" \
"/tmp/12345/my.sandbox.cnf" \
"find_my_cnf_file gets the correct file"
is "$cnf_file" "" "find_my_cnf_file gets the correct file"
[ $? -ne 0 ] && diag "$p/mysqld-instances"
# ps-mysqld-001.txt has several instances:
# port 3306 cnf -
# port 12345 cnf /tmp/12345/my.sandbox.cnf
# port 12346 cnf /tmp/12346/my.sandbox.cnf
res=$(find_my_cnf_file "$samples/ps-mysqld-001.txt")
is "$res" "/tmp/12345/my.sandbox.cnf" "ps-mysqld-001.txt"
is "$res" "" "ps-mysqld-001.txt no port"
res=$(find_my_cnf_file "$samples/ps-mysqld-001.txt" 3306)
is "$res" "" "ps-mysqld-001.txt port but no cnf"
res=$(find_my_cnf_file "$samples/ps-mysqld-001.txt" 999)
is "$res" "" "ps-mysqld-001.txt nonexistent port"
res=$(find_my_cnf_file "$samples/ps-mysqld-001.txt" 12346)
is "$res" "/tmp/12346/my.sandbox.cnf" "ps-mysqld-001.txt with port"
is "$res" "/tmp/12346/my.sandbox.cnf" "ps-mysqld-001.txt port 12346"
res=$(find_my_cnf_file "$samples/ps-mysqld-001.txt" 12345)
is "$res" "/tmp/12345/my.sandbox.cnf" "ps-mysqld-001.txt port 12345"
# ps-mysqld-004.txt has 1 instance without --port using
# --defaults-file=/var/lib/mysql/my.cnf
res=$(find_my_cnf_file "$samples/ps-mysqld-004.txt")
is "$res" "/var/lib/mysql/my.cnf" "ps-mysqld-004.txt"
is "$res" "/var/lib/mysql/my.cnf" "ps-mysqld-004.txt no port"
res=$(find_my_cnf_file "$samples/ps-mysqld-004.txt" 12345)
is "$res" "/var/lib/mysql/my.cnf" "ps-mysqld-004.txt with port"
is "$res" "" "ps-mysqld-004.txt port 12345"
# ps-mysqld-005.txt has the 3 sandbox instances, but 12347
# is first, which was causing bug 1070916.
res=$(find_my_cnf_file "$samples/ps-mysqld-005.txt" 12345)
is "$res" "" "ps-mysqld-005.txt port 12345 (bug 1070916)"
# collect_mysql_databases
$CMD_MYSQL $EXT_ARGV -ss -e 'SHOW DATABASES' > "$PT_TMPDIR/mysql_collect_databases" 2>/dev/null

View File

@@ -0,0 +1,4 @@
PID TTY STAT TIME COMMAND
1427 ? Sl 0:13 /home/jenkins/mysql-bin/mysql-5.5.24-i386-barebones/bin/mysqld --defaults-file=/tmp/12347/my.sandbox.cnf
20928 ? Sl 0:07 /home/jenkins/mysql-bin/mysql-5.5.24-i386-barebones/bin/mysqld --defaults-file=/tmp/12345/my.sandbox.cnf
29930 ? Sl 0:00 /home/jenkins/mysql-bin/mysql-5.5.24-i386-barebones/bin/mysqld --defaults-file=/tmp/12346/my.sandbox.cnf