module-mysql/files/scripts/Debian/setmysqlpass.sh
Jerome Charaoui a713d36aeb Merge branch 'master' of git://git.puppet.immerda.ch/module-mysql
Conflicts:
	files/scripts/optimize_tables.rb
	manifests/server/base.pp
2015-02-24 16:46:45 -05:00

47 lines
1.5 KiB
Bash

#!/bin/sh
test -f /root/.my.cnf || exit 1
must_have ()
{
# Here, using "which" would not be appropriate since it also depends on
# PATH being set correctly. The type builtin command is unaffected by the
# environment.
type $1 >/dev/null
if [ $? -ne 0 ]; then
echo "Command '$1' not found, did you correctly set PATH ? Its current value is: $PATH" >&2
exit 1
fi
}
# Since this script is doing something rather unsafe with the database, we want
# to be really careful to have all the necessary tools before doing anything so
# that we don't end up in an inconsistent state.
must_have sleep
must_have mysql
must_have killall
must_have ls
must_have chown
rootpw=$(grep password /root/.my.cnf | sed -e 's/^[^=]*= *\(.*\) */\1/')
/usr/bin/mysqladmin -uroot --password="${rootpw}" status > /dev/null && echo "Nothing to do as the password already works" && exit 0
/etc/init.d/mysql stop
/usr/sbin/mysqld --skip-grant-tables --user=root --datadir=/var/lib/mysql --log-bin=/var/lib/mysql/mysql-bin &
sleep 5
mysql -u root mysql <<EOF
UPDATE mysql.user SET Password=PASSWORD('$rootpw') WHERE User='root' AND Host='localhost';
DELETE FROM mysql.user WHERE (User='root' AND Host!='localhost') OR USER='';
FLUSH PRIVILEGES;
EOF
killall mysqld
sleep 15
# chown to be on the safe side
ls -al /var/lib/mysql/mysql-bin.* &> /dev/null
[ $? == 0 ] && chown mysql.mysql /var/lib/mysql/mysql-bin.*
chown -R mysql.mysql /var/lib/mysql/data/
/etc/init.d/mysql start