function set_mysql_passwd () {

if [ -z "${MYSQL_PASSWORD}" ]; then
    yaimlog ERROR "The MYSQL_PASSWORD variable is not set in site-info.def. Exiting."
    return 1
fi

pwds=`mysql -s -s --exec "select distinct password from \
user where user=SUBSTRING_INDEX(USER(),'@',1) \
and host=SUBSTRING_INDEX(USER(),'@',-1);" -D mysql 2> /dev/null`

if ( mysql -u root -h `hostname -f` --exec exit 2> /dev/null && [ -z "$pwds" ] ); then
    mysqladmin -h `hostname -f` password "${MYSQL_PASSWORD}"
    mysql -u root -h `hostname -f` --pass="$MYSQL_PASSWORD" --exec "grant all on *.* to 'root'@'`hostname -s`' identified by '$MYSQL_PASSWORD' with grant option"
    mysql -u root -h `hostname -f` --pass="$MYSQL_PASSWORD" --exec "grant all on *.* to 'root'@'localhost' identified by '$MYSQL_PASSWORD' with grant option"
elif ( mysql -u root -h `hostname -s` --exec exit 2> /dev/null && [ -z "$pwds" ] ); then
    mysqladmin -h `hostname -s` password "${MYSQL_PASSWORD}"
    mysql -u root -h `hostname -s` --pass="$MYSQL_PASSWORD" --exec "grant all on *.* to 'root'@'`hostname -f`' identified by '$MYSQL_PASSWORD' with grant option"
    mysql -u root -h `hostname -s` --pass="$MYSQL_PASSWORD" --exec "grant all on *.* to 'root'@'localhost' identified by '$MYSQL_PASSWORD' with grant option"
elif ( mysql -u root -h localhost --exec exit 2> /dev/null && [ -z "$pwds" ] ); then
    mysqladmin -h localhost password "${MYSQL_PASSWORD}"
    mysql -u root -h localhost --pass="$MYSQL_PASSWORD" --exec "grant all on *.* to 'root'@'`hostname -f`' identified by '$MYSQL_PASSWORD' with grant option"
    mysql -u root -h localhost --pass="$MYSQL_PASSWORD" --exec "grant all on *.* to 'root'@'`hostname -s`' identified by '$MYSQL_PASSWORD' with grant option"
fi

if ( mysql -u root --pass="$MYSQL_PASSWORD" mysql --exec exit 2> /dev/null ) ; then
   mysql -u root -h localhost --pass="$MYSQL_PASSWORD" --exec "grant all on *.* to 'root'@'`hostname -f`' identified by '$MYSQL_PASSWORD' with grant option"
   mysql -u root -h localhost --pass="$MYSQL_PASSWORD" --exec "grant all on *.* to 'root'@'`hostname -s`' identified by '$MYSQL_PASSWORD' with grant option"
   mysql -u root -h localhost --pass="$MYSQL_PASSWORD" --exec "grant all on *.* to 'root'@'localhost' identified by '$MYSQL_PASSWORD' with grant option"
   return 0
fi 
if [ -z "$pwds" ]; then
    # passwordless account, set one
    mysqladmin password "${MYSQL_PASSWORD}" 
    mysql -u root -h localhost --pass="$MYSQL_PASSWORD" --exec "grant all on *.* to 'root'@'`hostname -s`' identified by '$MYSQL_PASSWORD' with grant option"
    mysql -u root -h localhost --pass="$MYSQL_PASSWORD" --exec "grant all on *.* to 'root'@'`hostname -f`' identified by '$MYSQL_PASSWORD' with grant option"
fi

mysql -u root --pass="$MYSQL_PASSWORD" mysql --exec exit

if [ ! $? = 0 ]; then
    echo "Can not access MySQL DB, check password."
    return 1
fi

return 0

}
