How to Export & Import Existing User (with its Privileges!)

后端 未结 9 1978
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-03 17:01

I have an existing MySQL instance (test), containing 2 databases and a few users each having different access privileges to each database.

I now need to duplicate one of

9条回答
  •  北恋
    北恋 (楼主)
    2021-02-03 17:35

    mysql -u -p -h -e"select concat('show grants for ','\'',user,'\'@\'',host,'\'') from mysql.user" > user_list_with_header.txt
    sed '1d' user_list_with_header.txt > ./user.txt
    while read user; do  mysql -u -p -h -e"$user" > user_grant.txt; sed '1d' user_grant.txt >> user_privileges.txt; echo "flush privileges" >> user_privileges.txt; done < user.txt
    awk '{print $0";"}'  user_privileges.txt >user_privileges_final.sql
    rm user.txt user_list_with_header.txt user_grant.txt user_privileges.txt
    

    Above script will run in linux environment and output will be user_privileges_final.sql that you can import in new mysql server where you want to copy user privileges.

    UPDATE: There was a missing - for the user of the 2nd mysql statement.

提交回复
热议问题