Chinaunix首页 | 论坛 | 博客
  • 博客访问: 184898
  • 博文数量: 55
  • 博客积分: 1471
  • 博客等级: 上尉
  • 技术积分: 420
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-08 14:00
文章分类

全部博文(55)

文章存档

2012年(5)

2011年(50)

分类: LINUX

2012-01-10 13:06:59

1、说明
查看mysql数据库中所有非root用户的权限
在迁移数据库时会用到
2、运行示例
先提示输入mysql密码,输入密码后,检查密码的正确性,如果正确则输出mysql用户权限
运行过程中会产生两个文件user.lst和user.grants
user.lst 记录数据库中所有用户
user.grants 记录用户权限
  1. Input mysql password :

  2. ======================================================================================
  3. --------------------------------------------------------------------------------------
  4. Grants for test@192.168.1%
  5. GRANT USAGE ON *.* TO 'test'@'192.168.1%' IDENTIFIED BY PASSWORD 'password'
  6. GRANT ALL PRIVILEGES ON `test%`.* TO 'test'@'192.168.1%' WITH GRANT OPTION
  7. --------------------------------------------------------------------------------------
  8. ======================================================================================
3、代码
  1. #!/bin/bash
  2. #
  3. #
  4. #2012/01/10 anmile

  5. PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
  6. HOME=/user/local/games

  7. Show_Grants(){
  8. mysql -uroot -p"$DB_PWD" -e "select user,host from mysql.user;">user.lst
  9. sed -i '1d' user.lst
  10. echo "======================================================================================">user.grants
  11. echo "--------------------------------------------------------------------------------------">>user.grants
  12. for i in `awk '$2!=null && $1!="root" {printf "\047%s\047@\047%s\047\n",$1,$2}' user.lst `
  13. do
  14.     mysql -uroot -p"$DB_PWD" -e "show grants for $i">>user.grants
  15.     echo "--------------------------------------------------------------------------------------">>user.grants
  16. done
  17. echo "======================================================================================">>user.grants
  18. less user.grants
  19. }

  20. CHECK_MYSQL_PWD(){
  21.     unset PWD_CHECK DB_PWD
  22.     echo -e "\033[1;40;36m"
  23.     echo -ne "Input mysql password : "
  24.     echo -ne "\033[0m"
  25.     read -s DB_PWD
  26.     while :
  27.     do
  28.         if [ -z "$DB_PWD" ] ;then
  29.             echo
  30.             echo -ne "\033[1;40;31m"
  31.             echo -ne "the password is null,please reinput:"
  32.             echo -ne "\033[0m"
  33.             read -s DB_PWD
  34.             continue
  35.         fi
  36.         PWD_CHECK=$(mysqladmin -uroot -p"$DB_PWD" ping 2>/dev/null|grep alive)
  37.         if [ -z "$PWD_CHECK" ];then
  38.             echo
  39.             echo -ne "\033[1;40;31m"
  40.             echo -ne "the password"
  41.             echo -ne "\033[1;40;35m"
  42.             echo -ne "($DB_PWD)\033[1;40;31m is wrong,please reinput:"
  43.             echo -ne "\033[1;40;31m "
  44.             echo -ne "is wrong,please reinput:"
  45.             echo -ne "\033[0m"
  46.             read -s DB_PWD
  47.             continue
  48.         elif [ "$PWD_CHECK" = "mysqld is alive" ];then
  49.             Show_Grants
  50.            break
  51.         fi
  52.     done
  53. }

  54. CHECK_MYSQL_PWD

阅读(2646) | 评论(0) | 转发(0) |
0

上一篇:awk 笔记

下一篇:linux 颜色输出示意

给主人留下些什么吧!~~