代码语言:javascript复制
#!/bin/bash
# mysql 破解重置root密码
read -p "
Hello, welcome to continuous integration eisc.cn mysql 同时破解远程和localhost数据库密码
请输入用户名:" setuser
read -p "请自定义密码:" setpasswd
mycnf="/etc/my.cnf"
NR=`cat -n $mycnf | grep "[mysqld]" | awk -F" " '{print $1}'` ; echo $NR
# 找到 [mysqld] 标签所在行
sed -i "/skip-grant-tables/d" $mycnf
# 删除这个root免密登录配置,重新添加
sed -i "$NR a skip-grant-tables" $mycnf ; systemctl restart mysqld mariadb
# 在mysqld 标签下面增加免密登录配置信息
# 重启数据库
echo "您输入的信息为:$setuser $setpasswd"
mysqlversion=$(mysql -uroot -peisc.cn -e "select version();" | awk -F"." 'NR==2{print $1}') ; echo "mysql版本:$mysqlversion";
if [ $mysqlversion -gt 7 ];then
echo "当前数据库版本大于7"
mysql -e "use mysql;select user,authentication_string from user; set global read_only=0;set global read_only=1; set global validate_password.policy=0 ; set global validate_password.length=6;flush privileges;alter user '$setuser'@'localhost' identified by '$setpasswd';alter user '$setuser'@'%' identified by '$setpasswd';"
else
mysql -e "use mysql;select user,authentication_string from user; set global read_only=0;set global read_only=1; use mysql;select user,authentication_string from user; set global read_only=0;set global read_only=1; UPDATE user SET password=password('$setpasswd') WHERE user='$setuser';"
echo "低于7的版破解"
fi
# 登录数据库修改root密码为:eisc.cn
# 重点:set global read_only=0;set global read_only=1; flush privileges;set global validate_password.policy=0 ; set global validate_password.length=6;
# 关掉新主库的只读属性,读写属性,密码长度为6
sed -i "/skip-grant-tables/d" $mycnf; systemctl restart mysqld mariadb
# 删除之前的配置,重启数据
# wget eisc.cn/file/shell/mysql-passwd.sh ; sh mysql-passwd.sh