mysql数据库root口令忘记自动恢复脚本

来源:互联网 发布:云计算基础知识 编辑:程序博客网 时间:2024/05/18 02:06

Linux

大家好,最近有用户提出root口令忘记了怎么办的需求,我也在网上看到过好多这样的文章,写的都挺好的,但有些还是有问题,不是少几句话就是执行时出错。我写了一个简单的小脚本,请大家多指教吧。很简单但也很适用。 


注意事项: 
1。该脚本必须将属主改为root,权限为700,否则有安全问题。 
2。该脚本的作用是将root用户的口令清除,若想改口令请用mysqladmin工具来做,或者进入后用update 命令也可 
3。此脚本是针对系统自带的mysql命令的。如果是自己安装的源码包,可以修改关于停止mysql的那一部分。 



#!/bin/sh 
#This is programme is recover the password of root for mysql databases. 
#author is coffee_45 
#data is  4月  4 12:14:03 CST 2005 

#kill mysqld 
/bin/kill `cat /var/run/mysqld/mysqld.pid`  2> /dev/null 
#start mysqld with special paramaters 
/usr/bin/safe_mysqld --skip-grant-tables & 
sleep 3 
#change password for root 
chpw_mysql=/home/command_mysql 
echo "use mysql"  >$chpw_mysql 2> /dev/null 
echo "update user set password=password('') where user='root';" >> $chpw_mysql 2>/dev/null 
echo "quit" >> $chpw_mysql 
mysql < $chpw_mysql 

/etc/init.d/mysqld restart


http://blog.chinaunix.net/uid-568396-id-2734912.html

0 0