#!/bin/bash set -e user="root" passwd_file="/root/.my.cnf" utf8_cnf="/etc/mysql/conf.d/utf8_charset.cnf" mysqlSock="/var/run/mysqld/mysqld.sock" passwd="null" sqltest="$(mktemp /tmp/lliurex-sgbdXXXX)" debug=0 limit=10 alive=2 is_mysql_alive(){ local ret=1 alive=1 if [ -e $mysqlSock ] ; then ret=0 alive=0 fi if [ $debug -eq 1 ] ; then echo "esta vivo:"$ret fi return $ret } do_create_passwd(){ passwd="`pwgen --capitalize --numerals 7 1`" do_write_mycnf > "$passwd_file" chmod 600 $passwd_file return 0 } do_get_passwd(){ passwd=$(sed -ne "/^\[mysql\]/,/^\[/s%^password[^=]\+=[[:space:]]\+%%p" "$passwd_file") if [ $debug -eq 1 ] ; then echo "pass root Mysql:"$passwd fi return 0 } do_write_passSql() { local sqlQuery sqlQuery="UPDATE mysql.user SET Password=PASSWORD('$passwd') WHERE User='root'; " sqlQuery=$sqlQuery"FLUSH PRIVILEGES;" echo $sqlQuery return 0 } do_write_mycnf(){ echo -e "[mysql]" echo -e "user = "$user echo -e "password = "$passwd echo -e "" echo -e "[mysqladmin]" echo -e "user = "$user echo -e "password = "$passwd echo -e "" echo -e "[mysqldump]" echo -e "user = "$user echo -e "password = "$passwd return 0 } do_write_utf8_cnf(){ echo -e "[mysqld]" echo -e "character-set-server=utf8" echo -e "collation-server=utf8_general_ci" echo -e "init-connect='SET NAMES utf8'" echo -e "" echo -e "[client]" echo -e "default-character-set=utf8" echo -e "" return 0 } do_create_utf8_cnf(){ do_write_utf8_cnf > "$utf8_cnf" return 0 } is_mycnf_present(){ if [ ! -e "$passwd_file" ] ; then # root my.cnf file does not exists # generate password for root return 1 fi return 0 } is_utf8_present(){ if [ ! -e "$utf8_cnf" ] ; then return 1 fi return 0 } mysql_process(){ local timeout if [ $debug -eq 1 ] ; then echo "************************entering mysql mode "$1 fi case "$1" in "start") if [ $debug -eq 1 ] ; then echo "arrancando" fi service mysql $1 || true timeout=0 while true ; do is_mysql_alive || true if [ $alive -eq 0 ] ; then break ; fi if [ $timeout -eq $limit ] ; then echo "Error al arrancar" exit 1 fi timeout=$(($timeout+1)) sleep 1 if [ $debug -eq 1 ] ; then echo "time out: "$timeout fi done if [ $debug -eq 1 ] ; then echo "arrancado" fi ;; "stop") if [ $debug -eq 1 ] ; then echo "parando" fi service mysql $1 || true killall -9 mysqld || true timeout=0 while true ; do is_mysql_alive || true if [ $alive -eq 1 ] ; then break ; fi if [ $timeout -eq $limit ] ; then echo "Error al parar" exit 1 fi timeout=$(($timeout+1)) sleep 1 if [ $debug -eq 1 ] ; then echo "time out: "$timeout fi done if [ $debug -eq 1 ] ; then echo "parado" fi ;; *) echo "Error en param" ;; esac return 0 } mysql_safe_process(){ local timeout if [ $debug -eq 1 ] ; then echo "+++++++++++++++++++++++entering mysql safe mode "$1 fi case "$1" in "start") if [ $debug -eq 1 ] ; then echo "arrancando" fi /usr/bin/mysqld_safe --skip-grant-tables & timeout=0 while true ; do is_mysql_alive || true if [ $alive -eq 0 ] ; then break ; fi if [ $timeout -eq $limit ] ; then echo "Error al arrancar safe" exit 1 fi timeout=$(($timeout+1)) sleep 1 if [ $debug -eq 1 ] ; then echo "time out: "$timeout fi done if [ $debug -eq 1 ] ; then echo "arrancado" fi ;; "stop") if [ $debug -eq 1 ] ; then echo "parando" fi killall -9 mysqld 2>/dev/null >/dev/null || true killall -9 mysqld_safe 2>/dev/null >/dev/null || true timeout=0 while true ; do is_mysql_alive || true if [ $alive -eq 1 ] ; then break ; fi if [ $timeout -eq $limit ] ; then echo "Error al parar safe" exit 1 fi if [ $timeout -gt 1 ] ; then service mysql start || true service mysql stop || true fi timeout=$(($timeout+1)) sleep 1 if [ $debug -eq 1 ] ; then echo "time out: "$timeout fi done if [ $debug -eq 1 ] ; then echo "parado" fi ;; *) echo "Error en param" ;; esac return 0 } test_password(){ if mysql -u $user -p$passwd < $sqltest 2>/dev/null; then if [ $debug -eq 1 ] ; then echo "test passwd ok" fi rm -f $sqltest return 0 fi return 1 } configure (){ need_restart=0 if ! is_utf8_present; then do_create_utf8_cnf need_restart=1 fi if is_mycnf_present && is_mysql_alive ; then do_get_passwd if test_password ; then if [ $debug -eq 1 ] ; then echo "initial test passed. exit" fi if [ $need_restart -eq 1 ]; then service mysql restart fi exit 0 fi fi mysql_safe_process stop mysql_process start if is_mycnf_present ; then # If my.cnf file exists then sync in mysql do_get_passwd if [ $debug -eq 1 ] ; then echo "mycnf_present" fi else # If not present my.cnf then we create a passwd # and a my.cnf file do_create_passwd if [ $debug -eq 1 ] ; then echo "mycnf_not_present" fi fi # Test mysql password if is_mysql_alive ; then if [ $debug -eq 1 ] ; then echo "mysql alive" fi if test_password ; then exit 0 fi if [ $debug -eq 1 ] ; then echo "Mysql password not synced with mycnf file" fi else echo " * Mysql database server is not running" exit 0 fi echo " * Mysql database server running, setting Safe Mode" mysql_process stop if [ $debug -eq 1 ] ; then if is_mysql_alive ; then echo "mysqld not killed" fi fi # Mysql is started in safe mode mysql_safe_process start if [ $debug -eq 1 ] ; then echo "mysql_safe mode" fi if is_mysql_alive ; then # Sql statement to set password created sqlTemp=$(do_write_passSql) # Password is assigned in mysql if [ $debug -eq 1 ] ; then echo $sqlTemp fi echo "$sqlTemp" | mysql echo " * Mysql root password assigned" 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 mysql_safe_process stop mysql_process start return 0 } # main if [[ $EUID -ne 0 ]]; then echo "This script must be run as root" 1>&2 exit 1 fi if [ $# -eq 2 ] ; then if [ $2 == "-d" ] ; then debug=1 fi fi case "$1" in "-a"|"--alive_mysql") if is_mysql_alive ; then echo "YES" else echo "NO" fi ;; "-i"|"--initialize") configure ;; "-g"|"--get_password") do_get_passwd echo $passwd ;; "-p"|"--present_mycnf") if is_mycnf_present ; then echo "YES" else echo "NO" fi ;; *) echo "usage: mysql_root_passwd -a (--alive_mysql) | -i (--initialize) | -g (--get_password) | -p (--present_mycnf)" ;; esac exit 0