Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Sometimes there is a requirement to run multiple different MySQL servers on the same FreeBSD host, this makes it possible to create profiles to run different MySQL servers with different database paths/settings.

View mysql-server-multi
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
#!/bin/sh
#
# PROVIDE: mysql
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf to enable mysqlmulti:
#
# mysqlmulti_enable (bool): Set it to "YES" to enable uwsgi
# Default is "NO".
# mysqlmulti_limits (bool): Set to "NO" by default.
# Set it to yes to run `limits -e -U mysqlmulti`
# just before mysqlmulti starts.
# mysqlmulti_dbdir (str): Default to "/var/db/mysql"
# Base database directory.
# mysqlmulti_pidfile (str): Custum PID file path and name.
# Default to "${mysqlmulti_dbdir}/${hostname}.pid".
# mysqlmulti_args (str): Custom additional arguments to be passed
# to mysqlmultid_safe (default empty).
# mysqlmulti_user (str): User to start MySQL server as (default: mysql)
#
# If you would like to have multiple mysqlmulti instances running, you can
# define multiple profiles:
#
# mysqlmulti_profiles (str): Set the list of mysqlmulti profiles
# Default is "".
#
# For each profile you can then define different options (except for
# mysqlmulti_enable) using the syntax uwsgi_<profile>_<option>
 
. /etc/rc.subr
 
name="mysqlmulti"
rcvar=mysqlmulti_enable
 
load_rc_config $name
 
: ${mysqlmulti_enable="NO"}
: ${mysqlmulti_profiles=""}
: ${mysqlmulti_limits="NO"}
: ${mysqlmulti_dbdir="/var/db/mysql"}
: ${mysqlmulti_user="mysql"}
 
is_mysqlmulti_profile() {
local profile
 
for profile in $mysqlmulti_profiles; do
if [ "$profile" = "$1" ]; then
return 0
fi
done
 
return 1
}
 
if [ -n "${mysqlmulti_profiles}" ]; then
 
if [ -n "$2" ]; then
profile="$2"
if ! is_mysqlmulti_profile $profile; then
echo "$0: no such profile defined in mysqlmulti_profile"
exit 1
fi
 
eval mysqlmulti_limits=\${mysqlmulti_${profile}_limits:-"${mysqlmulti_limits}"}
eval mysqlmulti_dbdir=\${mysqlmulti_${profile}_dbdir:-"${mysqlmulti_dbdir}"}
eval mysqlmulti_args=\${mysqlmulti_${profile}_args:-"${mysqlmulti_args}"}
eval mysqlmulti_pidfile=\${mysqlmulti_${profile}_pidfile:-"${mysqlmulti_pidfile}"}
eval mysqlmulti_user=\${mysqlmulti_${profile}_user:-"${mysqlmulti_user}"}
 
elif [ -n "$1" ]; then
for profile in ${mysqlmulti_profiles}; do
echo "Processing ${name} profile: ${profile}"
/usr/local/etc/rc.d/mysql-server-multi $1 ${profile}
done
exit 0
fi
fi
 
mysql_limits_args="-e -U ${mysqlmulti_user}"
pidfile=${mysqlmulti_pidfile:-"${mysqlmulti_dbdir}/`/bin/hostname`.pid"}
command="/usr/sbin/daemon"
command_args="-c -f /usr/local/bin/mysqld_safe --defaults-extra-file=${mysqlmulti_dbdir}/my.cnf --user=${mysqlmulti_user} --datadir=${mysqlmulti_dbdir} --pid-file=${pidfile} ${mysqlmulti_args}"
procname="/usr/local/libexec/mysqld"
start_precmd="${name}_prestart"
start_postcmd="${name}_poststart"
mysql_install_db="/usr/local/bin/mysql_install_db"
mysql_install_db_args="--basedir=/usr/local --datadir=${mysqlmulti_dbdir} --force"
mysqlmulti_create_auth_tables()
{
eval $mysql_install_db $mysql_install_db_args >/dev/null 2>/dev/null
[ $? -eq 0 ] && chown -R ${mysqlmulti_user}:${mysqlmulti_user} ${mysqlmulti_dbdir}
}
mysqlmulti_prestart()
{
if [ ! -d "${mysqlmulti_dbdir}/mysql/." ]; then
mysqlmulti_create_auth_tables || return 1
fi
if checkyesno mysqlmulti_limits; then
eval `/usr/bin/limits ${mysql_limits_args}` 2>/dev/null
else
return 0
fi
}
mysqlmulti_poststart()
{
local timeout=15
while [ ! -f "${pidfile}" -a ${timeout} -gt 0 ]; do
timeout=$(( timeout - 1 ))
sleep 1
done
return 0
}
run_rc_command "$1"
View mysql-server-multi
1 2 3 4
mysqlmulti_enable="YES"
mysqlmulti_profiles="main second"
mysqlmulti_main_dbdir="/usr/db/main"
mysqlmulti_second_dbdir="/usr/db/second"

A similar patch can be found in an old FreeBSD bug report: http://www.freebsd.org/cgi/query-pr.cgi?pr=ports/171852

If your running multiple instance do not use an instance named "mysql" or others will get my.cnf from the default folder.
Other thing : Change listen port, path to socket for each instance ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.