SHELL练习001:重置mysql密码(用最无脑的方式记住它)

重置mysql密码

  环境说明:

环境 版本号
操作系统 RHEL8
mysql 8.0.17

1 mysql 手动修改密码步骤

  1. 修改my.cnf 文件在【mysqld】字段下面加入 skip-grant-tables,并关闭mysql服务
  2. 启动mysql服务
  3. mysql进入数据库
  4. use mysql
  5. select user,host,authentication_string from user 查看密码,一般是密文看不了
  6. 输入flush privileges否则修改密码会出现错误
  7. ALTER USER ‘root’@‘localhost’ IDENTIFIED BY ‘新密码’;
  8. flush privileges
  9. 重启数据库

2 使用 Shell 脚本重置

shell实现内容:

  1. 输入要设置的密码(判断密码是否合规及两次输入匹配)
  2. 密码修改
#!/bin/bash

password() {
while true;do

    while true;do
        echo "Your password must be a string containing case and special characters and no less than 8 bits."
        printf -- '\n';
        echo -e "enter password: "
        printf -- '\n';
        stty -echo
        read passwd1
        stty echo

        echo -e "please input password,again: "
        printf -- '\n';
        stty -echo
        read passwd2
        stty echo

       [[ $passwd1 = $passwd2 ]] && passwd=`echo $passwd1` && break || (echo "passwords not match,retry." && printf -- '\n');
    
    done

    strlen=`echo $passwd | grep -E --color '^(.{8,}).*$'`      #密码长度是否8位以上(包含8位)
    strlow=`echo $passwd | grep -E --color '^(.*[a-z]+).*$'`   #密码是否有小写字母
    strupp=`echo $passwd | grep -E --color '^(.*[A-Z]).*$'`    #密码是否有大写字母
    strts=`echo $passwd | grep -E --color '^(.*\W).*$'`        #密码是否有特殊字符
    strnum=`echo $passwd | grep -E --color '^(.*[0-9]).*$'`    #密码是否有数字
    
    if [ -n "${strlen}" ] && [ -n "${strlow}" ] && [ -n "${strupp}" ] && [ -n "${strts}" ]  && [ -n "${strnum}" ] 
    then
        (echo "Your password can be used;") && printf -- '\n' && break
    else
        echo "Your password can not be used; input another password."
        printf -- '\n';
    fi

done
}

wait(){

for ((i=0;i<9;i++))
do
    tput sc; tput civis
    echo -ne $(date +'%Y-%m-%d %H:%M:%S')
    sleep 1
    tput rc
done
tput el; tput cnorm
printf -- '\n';

}

chage(){


fn=$(date '+%Y-%m-%d'-%H:%M:%S).sql && echo $fn && touch /tmp/$fn

echo "use mysql;" >> /tmp/$fn
echo "flush privileges;" >> /tmp/$fn
echo "ALTER USER 'root'@'%' IDENTIFIED BY '$passwd';" >> /tmp/$fn
echo "flush privileges;" >> /tmp/$fn

grep "skip-grant-tables" /etc/my.cnf && a=1 || a=0
grep "skip-grant-tables" /etc/my.cnf | grep -v ^\# && b=1 || b=2

if [[ $a -eq $b ]];
then
    echo "already add skip-grant-tables to my.cnf"
    printf -- '\n';
elif [[ $a -eq 1 ]] && [[ $b -eq 2 ]];
then
    sed -i 's/\#skip-grant-tables/skip-grant-tables/g' /etc/my.cnf
else    
    sed -i 's/\[mysqld\]/\[mysqld\]\nskip-grant-tables/g' /etc/my.cnf
fi

ps -ef| grep /usr/sbin/mysqld | grep -v pts/0 

if [[ $? -eq 0 ]];
then
    systemctl restart mysqld
else
    systemctl start mysqld
fi

wait

[[ $? -eq 0 ]] && mysql < /tmp/$fn | tail -f 

[[ $? -eq 0 ]] && rm -rf /tmp/$fn && sed -i 's/skip-grant-tables/\#skip-grant-tables/g' /etc/my.cnf && systemctl restart mysqld

[[ $? -eq 0 ]] && echo "Well, i guess your password is ****************"

printf -- '\n';
}

password && chage 

3 更简单的办法

  把密码写在纸上:

把密码写下来

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章