#!/bin/bash set -e user="root" passwd_file="/root/.my.cnf" passwd="" timeout=0 mysql_running=0 mysql_status="" mysqlsafe_running=0 sqltest="$(mktemp /tmp/lliurex-sgbdXXXX)" echo "quit" > $sqltest do_check_user(){ local uid=$(id -u) [ $uid -ne 0 ] && return 1 return 0 } do_create_passwd(){ passwd="`pwgen --capitalize --numerals 7 1`" } do_get_passwd(){ passwd=$(sed -ne "/^\[mysql\]/,/^\[/s%^password[^=]\+=[[:space:]]\+%%p" "$passwd_file") } is_mycnf_present(){ if [ ! -e "$passwd_file" ] ; then # root my.cnf file does not exists # generate password for root return 1 fi return 0 } do_write_mycnf(){ cat << EOF [mysql] user = $user password = $passwd [mysqladmin] user = $user password = $passwd [mysqldump] user = $user password = $passwd EOF return 0 } do_fix_permission(){ chmod 600 $passwd_file } do_write_passsql() { cat << EOF use mysql update user set password=password('$passwd') where user='$user'; flush privileges; exit EOF return 0 } is_mysql_alive(){ local ret local pid ret=0 pid=$(pidof mysqld) || ret=1 mysql_status="$(LC_ALL=C service mysql status 2>&1)" [ $ret -eq 0 ] && return 0 return 1 } #------ # main #----- if ! do_check_user ; then echo "You must be root to execute this utility" exit 1 fi # If mysql is running we activate this flag if [ "$1" = "is-mysql-live" ]; then if is_mysql_alive && [ "$mysql_status" != "status: Unknown job: mysql" ]; then exit 0 else exit 1 fi fi if is_mysql_alive ; then mysql_running=1 fi if is_mycnf_present && [ "$1" != "force-upgrade" ]; then # If my.cnf file exists then sync in mysql do_get_passwd else # If not present my.cnf then we create a passwd # and a my.cnf file do_create_passwd do_write_mycnf > "$passwd_file" fi do_fix_permission if [ $mysql_running -eq 1 ] ; then if ! mysql -u $user -p$passwd < $sqltest 2>/dev/null; then rm -f $sqltest echo " * Mysql database server running, setting Safe Mode" [ "$mysql_status" != "mysql stop/waiting" ] && service mysql stop 2>/dev/null >/dev/null else rm -f $sqltest exit 0 fi else echo " * Mysql database server is not running" exit 0 fi # Mysql is started in safe mode /usr/bin/mysqld_safe --skip-grant-tables 2>/dev/null >/dev/null & while [ $timeout -lt 3 -a $mysqlsafe_running -eq 0 ] ; do sleep 1 if is_mysql_alive ; then mysqlsafe_running=1 fi timeout=$(($timeout+1)) done if [ $mysqlsafe_running -eq 1 ] ; then # Sql statement to set password created sqltemp="$(mktemp /tmp/lliurex-sgbdXXXX)" do_write_passsql > $sqltemp # Password is assigned in mysql /usr/bin/mysql < $sqltemp echo " * Mysql root password assigned" # tempfile deleted rm -f $sqltemp else echo " * Mysql root password cannot be assigned" echo " * Root password assigment deferred to next execution of this utility" echo " or next reboot of the machine" exit 1 fi # If mysql was running when the utility is invoked then restart mysql # else stop mysql because for changing the password it was started # in safe mode if [ $mysql_running -eq 1 ] || [ $mysqlsafe_running -eq 1 ]; then echo " * Mysql database server was running, so restarting" killall mysqld service mysql start 2>/dev/null >/dev/null else echo " * Mysql database server was stopped, so stopping" service mysql stop 2>/dev/null >/dev/null fi exit 0