2009-12-10 18:22:25 +01:00
|
|
|
#!/bin/sh
|
|
|
|
|
2010-12-14 18:10:54 +01:00
|
|
|
test -f /root/.my.cnf || exit 1
|
|
|
|
|
2013-11-15 09:19:40 +01:00
|
|
|
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
|
|
|
|
|
2010-12-14 18:10:54 +01:00
|
|
|
rootpw=$(grep password /root/.my.cnf | sed -e 's/^[^=]*= *\(.*\) */\1/')
|
2009-12-10 18:22:25 +01:00
|
|
|
|
2014-02-05 22:34:17 +01:00
|
|
|
/usr/bin/mysqladmin -uroot --password="${rootpw}" status > /dev/null && echo "Nothing to do as the password already works" && exit 0
|
|
|
|
|
2009-12-10 18:22:25 +01:00
|
|
|
/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
|
2010-12-14 18:10:54 +01:00
|
|
|
mysql -u root mysql <<EOF
|
|
|
|
UPDATE mysql.user SET Password=PASSWORD('$rootpw') WHERE User='root' AND Host='localhost';
|
2015-01-24 18:05:08 +01:00
|
|
|
DELETE FROM mysql.user WHERE (User='root' AND Host!='localhost') OR USER='';
|
2010-12-14 18:10:54 +01:00
|
|
|
FLUSH PRIVILEGES;
|
|
|
|
EOF
|
2009-12-10 18:22:25 +01:00
|
|
|
killall mysqld
|
2010-09-22 18:55:10 +02:00
|
|
|
sleep 15
|
2009-12-10 18:22:25 +01:00
|
|
|
# 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.*
|
2013-12-04 23:19:09 +01:00
|
|
|
chown -R mysql.mysql /var/lib/mysql/data/
|
2009-12-10 18:22:25 +01:00
|
|
|
|
|
|
|
/etc/init.d/mysql start
|
|
|
|
|