1 Star 0 Fork 0

yll1024335892 / shell_script

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mysql8_install_rpm_find_psw.sh 1.71 KB
一键复制 编辑 原始数据 按行查看 历史
#!/bin/bash
MYSQL_INSTALL_DIR="/usr"
MYSQL_CNF="/etc/my.cnf"
MYSQL_SERVER_NAME="mysqld"
# 检查是否是root用户
if [ $(id -u) != "0" ]; then
echo "must root"
exit 1
fi
echo "+-------------------------------------------------------------------+"
echo "| Reset MySQL Root Password"
echo "+-------------------------------------------------------------------+"
if [ -s "${MYSQL_INSTALL_DIR}/bin/mysql" ]; then
DB_Name="mysql"
else
echo "MySQL not found!"
exit 1
fi
while :; do
DB_Root_Password=""
read -p "Enter New ${DB_Name} root password: " DB_Root_Password
if [ "${DB_Root_Password}" = "" ]; then
echo "Error: Password can't be NULL!!"
else
break
fi
done
echo "Stoping ${DB_Name}..."
if command -v killall >/dev/null 2>&1; then
killall mysqld
else
kill $(pidof mysqld)
fi
echo "modify ${MYSQL_CNF} with skip-grant-tables"
sed -i '/skip-grant-tables/d' $MYSQL_CNF
sed -i '/\[mysqld\]/askip-grant-tables' $MYSQL_CNF
sleep 2
echo "update ${DB_Name} root password..."
systemctl start mysqld
${MYSQL_INSTALL_DIR}/bin/mysql -uroot mysql <<EOF
use mysql;
flush privileges;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY "${DB_Root_Password}";
commit;
flush privileges;
EOF
if [ $? -eq 0 ]; then
echo "Password reset succesfully. Now killing mysqld softly"
sed -i '/skip-grant-tables/d' $MYSQL_CNF
if command -v killall >/dev/null 2>&1; then
killall mysqld
else
kill $(pidof mysqld)
fi
sleep 5
echo "Restarting the actual ${DB_Name} service"
systemctl start ${MYSQL_SERVER_NAME}
echo "Password successfully reset to '${DB_Root_Password}'"
else
echo "Reset ${DB_Name} root password failed!"
fi
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/1024335892/shell_script.git
git@gitee.com:1024335892/shell_script.git
1024335892
shell_script
shell_script
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891