Backup MySQL users

后端 未结 8 2024
无人及你
无人及你 2021-01-31 09:15

How do I backup MySQL users and their privileges?

Anything like mysqldump?

I am looking for something like:

mysqldump -d -u root -p MyTable &         


        
8条回答
  •  心在旅途
    2021-01-31 09:34

    The scripts given above give the general idea, but they're inefficient. They're forking/execing mysql n+1 times. It can be done in only two calls to mysql

    mysql ${logininfo} -B -N -e "SELECT CONCAT('\'',user,'\'@\'',host,'\'') from user where user != 'root'" mysql | \
    while read uh
    do
       echo "SHOW GRANTS FOR ${uh};"
    done | mysql ${logininfo} -B -N | sed -e 's/$/;/' > ${outfile}
    

    If there are users other than root that you don't want to backup use or and specify user != 'whatever' in the where clause of the first mysql call.

提交回复
热议问题