summaryrefslogtreecommitdiff
path: root/files/scripts/CentOS/setmysqlpass.sh
blob: b643edb1e17e6b78e4e30af702a62ac100db575b (plain)
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
#!/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 chown

rootpw=$(grep password /root/.my.cnf | sed -e 's/^[^=]*= *\(.*\) */\1/')

/sbin/service mysqld stop

/usr/libexec/mysqld --skip-grant-tables --user=root --datadir=/var/lib/mysql/data --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';
FLUSH PRIVILEGES;
EOF
killall mysqld
# chown to be on the safe side
chown mysql.mysql /var/lib/mysql/mysql-bin.*

/sbin/service mysqld start