#! /bin/bash # Author: Nerijus # Company: Kolmisoft # Year: 2015, 2018 # About: Script adds skip-name-resolve to my.cnf . /usr/src/m2/framework/bash_functions.sh . /usr/src/m2/framework/settings.sh insert_line_after_pattern() { #special characters need to be escaped like: \$ #arg1 - pattern #arg2 - what to add #arg3 - path to file #example: insert_line_after_pattern "\[mysqld\]" "max_allowed_packet=100M" "/etc/my.cnf" if [ ! -f "$3" ]; then return 1; fi awk -F"#" '{print $1}' "$3" | sed 's/ //g' | grep "$2" &> /dev/null if [ "$?" == "0" ]; then echo -e "[\E[33m ALREADY EXIST \E[33m\033[0m]\t$2"; else cp $3 $3.mor_backup; sed '/'$1'.*$/a\'$2'' "$3" > /tmp/.mor_tmp && cat /tmp/.mor_tmp > "$3" && rm -rf /tmp/.mor_tmp #--------- cat "$3" | grep "$2" &> /dev/null if [ $? != 0 ]; then echo -e "[\E[31m FAILED \E[31m\033[0m]\t$2"; fi fi } #check if not already added if grep -Fq skip-name-resolve /etc/my.cnf; then report "skip-name-resolve already present in the my.cnf" 0 exit 0 fi #find out if password is needed to connect as root if /usr/bin/mysql -u root -pkolmisoft -e ";" ;then passplace="-pkolmisoft" elif /usr/bin/mysql -u root -e ";" ;then passplace="" else report "Failed to add skip-name-resolve to my.cnf: cannot connect as root" 1 exit 1 fi #check if m2@127.0.0.1 exist and add it if not NR_OF_USERS=`/usr/bin/mysql -u root $passplace --batch --silent -e "select count(*) from mysql.user where User='m2' and Host='127.0.0.1'"` if [ "$NR_OF_USERS" == "0" ]; then /usr/bin/mysql -u root $passplace -e "GRANT ALL PRIVILEGES ON *.* TO m2@127.0.0.1 IDENTIFIED BY 'm2' WITH GRANT OPTION;" /usr/bin/mysql -u root $passplace -e "GRANT PROCESS, SUPER, FILE, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO m2@127.0.0.1;" elif [ "$NR_OF_USERS" -gt "0" ] && [ "$NR_OF_USERS" -lt "99" ]; then #user exist, may continue : else report "Failed to add skip-name-resolve to my.cnf: cannot check if m2@127.0.0.1 exist" 1 exit 1 fi #doublecheck if user exist and proceed with adding to my.cnf NR_OF_USERS=`/usr/bin/mysql -u root $passplace --batch --silent -e "select count(*) from mysql.user where User='m2' and Host='127.0.0.1'"` if [ "$NR_OF_USERS" -gt "0" ]; then insert_line_after_pattern "\[mysqld\]" "skip-name-resolve" "/etc/my.cnf" if [ "$1" == "INSTALL" ]; then report "skip-name-resolve added to my.cnf" 3 report "Restarting MySQL" 3 service mysqld restart else report "skip-name-resolve added to my.cnf, restart MySQL service when possible" 2 fi else report "Failed to add skip-name-resolve to my.cnf: cannot add m2@127.0.0.1" 1 exit 1 fi exit 0