MySQL 导出用户权限

Version <= 5.6

#!/bin/bash  
#Function export user privileges  
 
source /etc/profile
 
pwd=******
expgrants()  
{  
  mysql -B -u'root' -p${pwd} -N -S /var/lib/mysql/mysql.sock $@ -e "SELECT CONCAT(  'SHOW GRANTS FOR ''', user, '''@''', host, ''';' ) AS query FROM mysql.user" | \
  mysql -u'root' -p${pwd} -S /var/lib/mysql/mysql.sock $@ | \
  sed 's/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/-- \1 /;/--/{x;p;x;}'  
}  
 
expgrants > ./grants.sql

Version >= 5.7

#/bin/bash
#Function export user privileges

source /etc/profile

pwd=password 
expgrants()  
{  
  mysql -B -u'root' -p${pwd} -N  -P3306  $@ -e "SELECT CONCAT(  'SHOW CREATE USER   ''', user, '''@''', host, ''';' ) AS query FROM mysql.user where user not in ('mysql.session','mysql.sys')" | \
  mysql -u'root' -p${pwd} -P3306 -f  $@ | \
  sed 's#$#;#g;s/^\(CREATE USER for .*\)/-- \1 /;/--/{x;p;x;}' 
 
  mysql -B -u'root' -p${pwd} -N  -P3306  $@ -e "SELECT CONCAT(  'SHOW GRANTS FOR ''', user, '''@''', host, ''';' ) AS query FROM mysql.user where user not in ('mysql.session','mysql.sys')" | \
  mysql -u'root' -p${pwd} -P3306 -f  $@ | \
  sed 's/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/-- \1 /;/--/{x;p;x;}'   
}  

expgrants > ./grants.sql

 

转载于:https://www.cnblogs.com/ilifeilong/p/10542365.html

你可能感兴趣的:(MySQL 导出用户权限)