【问题标题】:How do I truncate all data in my MySQL database?如何截断 MySQL 数据库中的所有数据?
【发布时间】:2013-03-18 13:14:18
【问题描述】:

我在 Mac 10.7.5 上使用 MySql 5.5。从 shell(我正在使用 bash),我希望能够运行一个命令来截断所有表中的数据。另外,我想输入一个不会提示我输入密码的命令。我试过这个,我在另一个 SO 帖子上找到的,但没有骰子。我可以用来截断所有数据库表数据的 shell 命令是什么?

mysql -umyuser -p -e 'SET FOREIGN_KEY_CHECKS = 0; show tables' my_db | while read table; do mysql -e -umyuser -p "truncate table $table" my_db; done
Enter password: 

mysql  Ver 14.14 Distrib 5.5.25, for osx10.6 (i386) using readline 5.1
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Usage: mysql [OPTIONS] [database]
  -?, --help          Display this help and exit.
  -I, --help          Synonym for -?
  --auto-rehash       Enable automatic rehashing. One doesn't need to use
                  'rehash' to get table and field completion, but startup
                  and reconnecting may take a longer time. Disable with
                  --disable-auto-rehash.
                  (Defaults to on; use --skip-auto-rehash to disable.)
  -A, --no-auto-rehash
                  No automatic rehashing. One has to use 'rehash' to get
                  table and field completion. This gives a quicker start of
                  mysql and disables rehashing on reconnect.
  --auto-vertical-output
                  Automatically switch to vertical output mode if the
                  result is wider than the terminal width.
  -B, --batch         Don't use history file. Disable interactive behavior.
                  (Enables --silent.)
  --character-sets-dir=name
                  Directory for character set files.
  --column-type-info  Display column type information.
  -c, --comments      Preserve comments. Send comments to the server. The
                  default is --skip-comments (discard comments), enable
                  with --comments.
  -C, --compress      Use compression in server/client protocol.
  -#, --debug[=#]     This is a non-debug version. Catch this and exit.
  --debug-check       Check memory and open file usage at exit.
  -T, --debug-info    Print some debug info at exit.
  -D, --database=name Database to use.
  --default-character-set=name
...

【问题讨论】:

  • 没有骰子,您可能必须逐一遍历所有表格和TRUNCATE
  • “没有骰子”是什么意思?我发布这个问题是因为我无法让命令首先工作,并希望有人知道解决它的方法。
  • 如果您不想输入密码,可以将其放入 my.cnf 配置文件中。您可能希望将权限设置为 600,以便其他人看不到它。

标签: mysql shell truncate


【解决方案1】:

您可以在没有数据的情况下转储数据库:

mysqldump -u myuser -p --databases --add-drop-database --no-data my_db > my_db.sql

然后恢复它

mysql -u myuser -p < my_db.sql

【讨论】:

  • 非常聪明! (+1) 你甚至可以一口气做到这一点:mysqldump -u[user] -p[password] --databases --add-drop-table --no-data my_db | mysql -u[user] -p[password]。请注意,在-u-p 的规范形式中,选项开关与其值之间没有空格。我还假设您的意思是 --add-drop-table 而不是 --add-drop-databases。将--databases 替换为一个数据库的名称以仅清除一个给定的数据库。
猜你喜欢
  • 2011-06-12
  • 2011-08-27
  • 1970-01-01
  • 1970-01-01
  • 2011-02-19
  • 2011-08-12
  • 2010-09-14
  • 1970-01-01
  • 2010-12-27
相关资源
最近更新 更多