导出没有数据的架构

发布于 2024-11-10 14:27:20 字数 84 浏览 5 评论 0原文

我正在使用带有 Java 程序的 MySql 数据库,现在我想将该程序提供给其他人。

如何导出MySQL数据库结构而不包含数据,只导出结构?

I'm using a MySql database with a Java program, now I want to give the program to somebody else.

How to export the MySQL database structure without the data in it, just the structure?

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

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

发布评论

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

评论(16

汐鸠 2024-11-17 14:27:20

您可以使用 mysqldump 命令使用 --no-data 选项

mysqldump -h yourhostnameorIP -u root -p --no-data dbname > schema.sql

You can do with the --no-data option with mysqldump command

mysqldump -h yourhostnameorIP -u root -p --no-data dbname > schema.sql
西瑶 2024-11-17 14:27:20

是的,您可以将 mysqldump--no-data 选项一起使用:

mysqldump -u user -h localhost --no-data -p database > database.sql

Yes, you can use mysqldump with the --no-data option:

mysqldump -u user -h localhost --no-data -p database > database.sql
终弃我 2024-11-17 14:27:20

您还可以使用 --no-data 选项提取单个表

mysqldump -u user -h localhost --no-data -p database tablename > table.sql

you can also extract an individual table with the --no-data option

mysqldump -u user -h localhost --no-data -p database tablename > table.sql
梦屿孤独相伴 2024-11-17 14:27:20

您可以将 -d 选项与 mysqldump 命令一起使用

mysqldump -u root -p -d databasename > database.sql

You can use the -d option with mysqldump command

mysqldump -u root -p -d databasename > database.sql
乙白 2024-11-17 14:27:20

但请注意 --no-data 选项不会包含视图定义。因此,如果您有一个类似

create view v1 
 select `a`.`id` AS `id`,
 `a`.`created_date` AS `created_date` 
from t1;

带有 --no-data 选项的视图,视图定义将更改为以下

create view v1
 select 1 AS `id`, 1 AS `created_date`

Beware though that --no-data option will not include the view definition. So if yo had a view like following

create view v1 
 select `a`.`id` AS `id`,
 `a`.`created_date` AS `created_date` 
from t1;

with --no-data option, view definition will get changed to following

create view v1
 select 1 AS `id`, 1 AS `created_date`
苍暮颜 2024-11-17 14:27:20

不使用输出进行转储。

mysqldump --no-data <database name> --result-file=schema.sql

Dumping without using output.

mysqldump --no-data <database name> --result-file=schema.sql
风月客 2024-11-17 14:27:20

如果您使用 IntelliJ,您可以启用数据库视图(视图 -> 工具窗口 -> 数据库),

在该视图内连接到您的数据库。然后你可以右键单击数据库并选择“复制DDL”。其他 IDE 可能提供类似的功能。

IntelliJ DDL

In case you are using IntelliJ you can enable the Database view (View -> Tools Window -> Database)

Inside that view connect to your database. Then you can rightclick the database and select "Copy DDL". Other IDEs may offer a similar function.

IntelliJ DDL

小红帽 2024-11-17 14:27:20

如果您想转储所有数据库中的所有表并且没有数据(仅数据库和表结构),您可以使用:

mysqldump -P port -h hostname_or_ip -u username -p --no-data --all-databases > db_backup.sql

这将生成一个 .sql 文件,您可以将其加载到 mysql 服务器上以创建新数据库。
在生产环境中,这种情况的用例并不多,但我每周都会这样做,以重置链接到演示网站的服务器,因此无论用户在一周内做什么,在周日晚上,一切都会回滚到“新”: )

If you want to dump all tables from all databases and with no data (only database and table structures) you may use:

mysqldump -P port -h hostname_or_ip -u username -p --no-data --all-databases > db_backup.sql

This will produce a .sql file that you can load onto a mysql server to create a fresh database.
Use cases for this are not many in a production environment, but I do this on a weekly basis to reset servers which are linked to demo websites, so whatever the users do during the week, on sunday nights everything rolls back to "new" :)

旧伤慢歌 2024-11-17 14:27:20

您可以使用免费的 MYSQL 管理员工具
http://dev.mysql.com/downloads/gui-tools/5.0.html

你会发现有很多导出 MYSQL 数据库的选项

You Can Use MYSQL Administrator Tool its free
http://dev.mysql.com/downloads/gui-tools/5.0.html

you'll find many options to export ur MYSQL DataBase

魂牵梦绕锁你心扉 2024-11-17 14:27:20

添加 --routines 和 --events 选项以还包括存储的例程和事件定义

mysqldump -u <user> -p --no-data --routines --events test > dump-defs.sql

Add the --routines and --events options to also include stored routine and event definitions

mysqldump -u <user> -p --no-data --routines --events test > dump-defs.sql
情丝乱 2024-11-17 14:27:20

例如,您可以使用 -d(--no-data) 如下所示。 *我的回答解释了如何导出架构和数据数据库表的数量:

mysqldump -u john -p -d apple > backup.sql

或者:

mysqldump -u john -p --no-data apple > backup.sql

For example, you can export only the schema of the tables of apple database to backup.sql with -d(--no-data) as shown below. *My answer explains how to export both the schema and data of the tables of a database:

mysqldump -u john -p -d apple > backup.sql

Or:

mysqldump -u john -p --no-data apple > backup.sql
骄傲 2024-11-17 14:27:20

外壳> mysqldump --no-data --routines --events 测试>转储-defs.sql

shell> mysqldump --no-data --routines --events test > dump-defs.sql

浪漫之都 2024-11-17 14:27:20

使用 mysql-backup4j,只需几行代码即可轻松备份任何数据库。这将为数据库生成 sql 转储,稍后可用于轻松恢复数据库。

所需的 maven 依赖项是:

 <dependency>
        <groupId>com.smattme</groupId>
        <artifactId>mysql-backup4j</artifactId>
        <version>1.0.1</version>
    </dependency>

这里是 java 中的实现

/**
 * Creator : Tanvir Chowdhury
 * Date    : 2021-11-15
 */
public class BackupDatabase {
    public static void main(String[] args) throws Exception {

        Properties properties = new Properties();
        properties.setProperty(MysqlExportService.DB_NAME, "hcs_test_db");
        properties.setProperty(MysqlExportService.DB_USERNAME, "root");
        properties.setProperty(MysqlExportService.DB_PASSWORD, "root");
        properties.setProperty(MysqlExportService.DELETE_EXISTING_DATA, "true");
        properties.setProperty(MysqlExportService.DROP_TABLES, "true");
        properties.setProperty(MysqlExportService.ADD_IF_NOT_EXISTS, "true");
        properties.setProperty(MysqlExportService.JDBC_DRIVER_NAME, "com.mysql.cj.jdbc.Driver");
        properties.setProperty(MysqlExportService.JDBC_CONNECTION_STRING, "jdbc:mysql://localhost:3306/hcs_test_db");

        properties.setProperty(MysqlExportService.TEMP_DIR, new File("/Users/tanvir/Desktop/backups/backup.sql").toString());
        properties.setProperty(MysqlExportService.PRESERVE_GENERATED_ZIP, "true");

        MysqlExportService mysqlExportService = new MysqlExportService(properties);
        mysqlExportService.export();

    }
}

..如果需要,您可以使用很酷的参数集,您还可以非常轻松地使用邮件发送选项将备份文件发送到任何电子邮件地址。

或者您可以使用 mysqldump 命令的 --no-data 选项来执行此操作。如果您想从 java 执行此操作,请将此命令作为参数传递给运行时 exec() 方法。

mysqldump -u root -h localhost --no-data -proot hcs_db_one?useSSL=false > db_one_dump.sql

Using mysql-backup4j its quite easy to backup any database with few lines of code.this will generate the sql dump for database that can later be use to restore database easily.

maven dependency required for this is :

 <dependency>
        <groupId>com.smattme</groupId>
        <artifactId>mysql-backup4j</artifactId>
        <version>1.0.1</version>
    </dependency>

And here goes the implementation in java..you can play with the cool parameter sets

/**
 * Creator : Tanvir Chowdhury
 * Date    : 2021-11-15
 */
public class BackupDatabase {
    public static void main(String[] args) throws Exception {

        Properties properties = new Properties();
        properties.setProperty(MysqlExportService.DB_NAME, "hcs_test_db");
        properties.setProperty(MysqlExportService.DB_USERNAME, "root");
        properties.setProperty(MysqlExportService.DB_PASSWORD, "root");
        properties.setProperty(MysqlExportService.DELETE_EXISTING_DATA, "true");
        properties.setProperty(MysqlExportService.DROP_TABLES, "true");
        properties.setProperty(MysqlExportService.ADD_IF_NOT_EXISTS, "true");
        properties.setProperty(MysqlExportService.JDBC_DRIVER_NAME, "com.mysql.cj.jdbc.Driver");
        properties.setProperty(MysqlExportService.JDBC_CONNECTION_STRING, "jdbc:mysql://localhost:3306/hcs_test_db");

        properties.setProperty(MysqlExportService.TEMP_DIR, new File("/Users/tanvir/Desktop/backups/backup.sql").toString());
        properties.setProperty(MysqlExportService.PRESERVE_GENERATED_ZIP, "true");

        MysqlExportService mysqlExportService = new MysqlExportService(properties);
        mysqlExportService.export();

    }
}

if required you can also use the mail sending options quite easily to send the backup file to any email address.

Or You can do with the --no-data option with mysqldump command.If you want to do this from java then pass this command to runtime exec() method as param.

mysqldump -u root -h localhost --no-data -proot hcs_db_one?useSSL=false > db_one_dump.sql
违心° 2024-11-17 14:27:20

您可以使用以下方法

mysqldump -d <database name> > <filename.sql> // -d : without data

You can take using the following method

mysqldump -d <database name> > <filename.sql> // -d : without data
薄荷港 2024-11-17 14:27:20

要获取单个表的创建脚本:
- 选择所有表格(使用shift键)
- 只需右键单击表名称,然后单击复制到剪贴板>创建声明。

To get an individual table's creation script:
- select all the table (with shift key)
- just right click on the table name and click Copy to Clipboard > Create Statement.

雨巷深深 2024-11-17 14:27:20

从 phpmyadmin 您可以执行以下操作:

  1. 导出
  2. 导出方法:自定义 - 显示所有可能的选项
  3. 格式特定选项:结构NOT 结构和数据

要导出整个数据库:

在此处输入图像描述

From phpmyadmin you can do the following:

  1. Export
  2. Export method: Custom - display all possible options
  3. Format-specific options: structure NOT structure and data

To export the entire database:

enter image description here

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