參考來源:
https://www.cnblogs.com/huangmr0811/p/5570994.html
https://blog.csdn.net/u011665746/article/details/79067656
5.6導出執行腳本:
入參: export_db_ip=$1 #服務器IP export_db_port=$2 #MySQL端口 export_user=$3 #導出用戶 export_user_passwd=$4 #導出用戶密碼
1 ###傳入導出服務器及用戶信息 2 export_db_ip=$1 3 export_db_port=$2 4 export_user=$3 5 export_user_passwd=$4 6 7 ###文件保存目錄 8 path=`pwd` 9 10 ###導出用戶 11 mysql -B -N -h $export_db_ip -P $export_db_port -u${export_user} -p${export_user_passwd} -e "SELECT CONCAT('\'', user,'\'@\'', host, '\'') FROM user WHERE user != 'mysql.session' and user != 'mysql.sys' AND user != 'root' AND user != ''" mysql > $path/mysql_all_users.txt 12 13 ###導出用戶權限 14 while read line; 15 do 16 mysql -B -N -h $export_db_ip -P $export_db_port -u${export_user} -p${export_user_passwd} -e "SHOW GRANTS FOR $line"; 17 done < $path/mysql_all_users.txt > $path/mysql_all_users_sql.sql 18 19 20 ###每行行尾增加; 21 sed -i 's/$/;/' $path/mysql_all_users_sql.sql 22 23 ###立即生效 24 echo "flush privileges;" >> $path/mysql_all_users_sql.sql
5.7導出執行腳本:
入參: export_db_ip=$1 #服務器IP export_db_port=$2 #MySQL端口 export_user=$3 #導出用戶 export_user_passwd=$4 #導出用戶密碼
#!/bin/bash #Function export user privileges #5.7存在問題: show grants for 不會給出密碼信息,必須用 show create user # https://dev.mysql.com/doc/refman/5.7/en/show-grants.html # show create user 為5.7版本開始存在,5.6執行報錯。 ###傳入導出服務器及用戶信息 export_db_ip=$1 export_db_port=$2 export_user=$3 export_user_passwd=$4 ###文件保存目錄 path=`pwd` source /etc/profile pwd=password expgrants() { mysql -B -h $export_db_ip -P $export_db_port -u${export_user} -p${export_user_passwd} -N $@ -e "SELECT CONCAT( 'SHOW CREATE USER ''', user, '''@''', host, ''';' ) AS query FROM mysql.user" | \ mysql -h $export_db_ip -P $export_db_port -u${export_user} -p${export_user_passwd} -f $@ | \ sed 's#$#;#g;s/^\(CREATE USER for .*\)/-- \1 /;/--/{x;p;x;}' mysql -B -h $export_db_ip -P $export_db_port -u${export_user} -p${export_user_passwd} -N $@ -e "SELECT CONCAT( 'SHOW GRANTS FOR ''', user, '''@''', host, ''';' ) AS query FROM mysql.user" | \ mysql -h $export_db_ip -P $export_db_port -u${export_user} -p${export_user_passwd} -f $@ | \ sed 's/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/-- \1 /;/--/{x;p;x;}' } expgrants > $path/mysql_all_users_sql_5.7.sql ###立即生效 echo "flush privileges;" >> $path/mysql_all_users_sql_5.7.sql
導出執行語句:
[root@db02 tmp]# mysql -u root -p < mysql_all_users_sql.sql
