在MySQL 5.5/5.6版本中,使用SHOW GRANTS命令可以导出用户的创建脚本和授权脚本。

hostname='127.0.0.1'
port=3358
username='root'
password='abc@123.com'
mysql_exe="/export/servers/mysql/bin/mysql"
echo "select concat('show grants for ''',user,'''@''',host, ''';') from mysql.user where user <>'root'" | \
${mysql_exe} --host=$hostname --user=$username --password=$password --port=$port -N | \
${mysql_exe} --host=$hostname --user=$username --password=$password --port=$port -N | \
sed "s/$/;/" > /tmp/create-users.sql

 

在MySQL 5.7版本中,需要使用SHOW CRETAE USER命令导出用户创建脚本,然后使用SHOW GRANT命令导出用户授权脚本。

hostname='127.0.0.1'
port=3358
username='root'
password='abc@123.com'
mysql_exe="/export/servers/mysql/bin/mysql"
echo "select concat('show create user ''',user,'''@''',host, ''';','show grants for ''',user,'''@''',host, ''';') from mysql.user where user <>'root'" | \
${mysql_exe} --host=$hostname --user=$username --password=$password --port=$port -N | \
${mysql_exe} --host=$hostname --user=$username --password=$password --port=$port -N | \
sed "s/$/;/" > /tmp/create-users.sql

 

相关文章:

  • 2022-02-10
  • 2022-01-07
  • 2021-12-05
  • 2021-12-05
  • 2021-06-06
  • 2021-05-28
  • 2021-11-10
猜你喜欢
  • 2022-12-23
  • 2021-12-04
  • 2021-11-26
  • 2021-11-26
  • 2021-06-30
  • 2021-05-26
相关资源
相似解决方案