备份MySQL用户

发布于 2024-07-15 02:46:17 字数 149 浏览 6 评论 0原文

如何备份MySQL用户及其权限?

类似于 mysqldump 的东西吗?

我正在寻找类似的东西:

mysqldump -d -u root -p MyTable > Schema.sql

How do I backup MySQL users and their privileges?

Anything like mysqldump?

I am looking for something like:

mysqldump -d -u root -p MyTable > Schema.sql

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

忆离笙 2024-07-22 02:46:18
mysql -BNe "select concat('\'',user,'\'@\'',host,'\'') from mysql.user where user != 'root'" | \
while read uh; do mysql -BNe "show grants for $uh" | sed 's/$/;/; s/\\\\/\\/g'; done > grants.sql
mysql -BNe "select concat('\'',user,'\'@\'',host,'\'') from mysql.user where user != 'root'" | \
while read uh; do mysql -BNe "show grants for $uh" | sed 's/$/;/; s/\\\\/\\/g'; done > grants.sql
沉默的熊 2024-07-22 02:46:18

您可以使用备份mysql数据库

mysqldump -u root -p mysql > mysql.sql

来恢复mysql数据库

 mysql -uroot -p mysql < mysql.sql

不要忘记

FLUSH PRIVILEGES

并通过执行恢复转储后

。 希望能帮助到你...

You can backup mysql database using

mysqldump -u root -p mysql > mysql.sql

and restore mysql database by executing

 mysql -uroot -p mysql < mysql.sql

Dont forget to

FLUSH PRIVILEGES

after restoring dump.

Hope it helps...

为人所爱 2024-07-22 02:46:18

到目前为止,我在 MySQL 方面的经验还没有看到任何可以通过命令行备份用户及其权限的内容。

但我可以通过备份mysql来备份那些关键数据

mysqldump -u root -p mysql > mysql.sql

So far my experience with MySQL i didn't see anything to backup user and their privileges through a command line.

But i can backup those critical data by backing up mysql

mysqldump -u root -p mysql > mysql.sql
铜锣湾横着走 2024-07-22 02:46:18

Percona 为此提供了一个很棒的工具。 pt-show-grants 将转储用户及其权限,以便您可以轻松地重新加载它们。

https://www.percona.com/doc/percona -toolkit/LATEST/pt-show-grants.html

Percona has a great tool for this. pt-show-grants will dump users and their permissions so you can easily reload them.

https://www.percona.com/doc/percona-toolkit/LATEST/pt-show-grants.html

独﹏钓一江月 2024-07-22 02:46:18

用户和权限存储在名为“mysql”的数据库中。 您可以使用 mysqldump 备份名为“mysql”的数据库中的表。

The users and privileges are stored in the databased named 'mysql'. You can use mysqldump to backup the tables in the databased named 'mysql'.

顾挽 2024-07-22 02:46:18

好的做法是使用脚本来日常备份 MySQL 用户及其权限。
看一下:

#!/bin/sh

HOSTNAME="localhost"

mysql -h $HOSTNAME -B -N -e "SELECT CONCAT('\'', user,'\'@\'', host, '\'') FROM user WHERE user != 'debian-sys-maint' AND user != 'root' AND user != ''" mysql > mysql_all_users_$HOSTNAME.txt

while read line; do mysql -h $HOSTNAME -B -N -e "SHOW GRANTS FOR $line"; done < mysql_all_users_$HOSTNAME.txt > mysql_all_users_$HOSTNAME.sql

sed -i.bak 's/$/;/' mysql_all_users_$HOSTNAME.sql

rm mysql_all_users_$HOSTNAME.txt
rm mysql_all_users_$HOSTNAME.sql.bak

该脚本的结果将是包含用户和权限的 mysqldump 文件。

PS 如果您的 MySQL 需要密码,请将 -p-u username -p 放在 mysql -h $HOSTNAME 之后的两个位置。

Good practice is using script for daily backup MySQL users and their privileges.
Take take a look on a one:

#!/bin/sh

HOSTNAME="localhost"

mysql -h $HOSTNAME -B -N -e "SELECT CONCAT('\'', user,'\'@\'', host, '\'') FROM user WHERE user != 'debian-sys-maint' AND user != 'root' AND user != ''" mysql > mysql_all_users_$HOSTNAME.txt

while read line; do mysql -h $HOSTNAME -B -N -e "SHOW GRANTS FOR $line"; done < mysql_all_users_$HOSTNAME.txt > mysql_all_users_$HOSTNAME.sql

sed -i.bak 's/$/;/' mysql_all_users_$HOSTNAME.sql

rm mysql_all_users_$HOSTNAME.txt
rm mysql_all_users_$HOSTNAME.sql.bak

Result of this script will be mysqldump file with users and privileges.

P.S. If your MySQL requires password - put -p or -u username -p after mysql -h $HOSTNAME in two places.

腹黑女流氓 2024-07-22 02:46:18

上面给出的脚本给出了总体思路,但效率很低。 他们分叉/执行 mysql n+1 次。 只需两次调用 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}

如果您不想备份 root 以外的用户,请使用 or 并在第一个 mysql 调用的 where 子句中指定 user != 'whatever'。

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.

最偏执的依靠 2024-07-22 02:46:18

对于 mysql 命令行可能非常明显,但对于上面@spirit的答案,必须在两个 mysql 命令之后添加 -u root -ppassword

mysql -u root -ppassword -BNe“从mysql.user中选择concat(''',用户,''@'',主机,'''),其中用户!='root'”| 阅读时呃; 执行 mysql -u root -ppassword -BNe“显示 $uh 的补助金”| sed 的/$/;/; s/\\/\/g'; 完成> 补助金.sql;

probably pretty obvious for mysql command liners but for @spirit's answer above had to add -u root -ppassword after both mysql commands

mysql -u root -ppassword -BNe "select concat(''',user,''@'',host,''') from mysql.user where user != 'root'" | while read uh; do mysql -u root -ppassword -BNe "show grants for $uh" | sed 's/$/;/; s/\\/\/g'; done > grants.sql;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文