从 mysql 内部复制 mysql 数据库?

发布于 2024-11-16 11:32:58 字数 420 浏览 5 评论 0原文

这可能是一个遥不可及的事情,但是.. 是否可以使用存储过程中的一个查询或多个查询来复制整个数据库布局(表、视图、过程等)?

基本上寻找与我使用 mysqldump 相同的功能,

# mysqldump -u root -ppassword --no-data --routines dbname > file
  //create database copyofdbname
# mysql -u root -ppassword copyofdbname < file

是否可以在没有任何外部工具的情况下在 mysql 过程中执行此操作?

我想我可以通过使用“显示表​​”来创建表,然后迭代结果以获得每个表的“创建表”语句并将它们转发到新数据库中。
但这只是猜测,不知道如何以这种方式从数据库复制存储过程。

This is probably a longshot but..
is it possible to duplicate a whole database layout (tables, view, procedures, everything) with a query or multiple queries in a stored procedure?

Basically looking for the same functionality as if I would use mysqldump like this

# mysqldump -u root -ppassword --no-data --routines dbname > file
  //create database copyofdbname
# mysql -u root -ppassword copyofdbname < file

Is it possible to do this in a mysql procedure without any external tools?

I guess I could do the tables by using 'show tables' and then a iterate over the results to get the 'create table' statatements for each table and forward them into the new database.
This is just speculation though, no idea how I would copy the stored procedures from the database this way.

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

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

发布评论

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

评论(2

固执像三岁 2024-11-23 11:32:58

基本上没有,MYSql没有重复数据库功能。一些外部工具可能会,但我不知道。我使用 php 完成了这个过程,但我确信它可以使用存储过程或任何其他中间层应用程序来完成。这是我在高级别上遵循的步骤。我假设您知道如何执行每个步骤的详细信息。

  1. 创建新的数据库
  2. 查询该数据库的所有表,我可以访问 information_schema,所以我只是从那里进行了选择。
  3. 在桌子上循环。
    1. 运行类似这样的CREATE TABLE dbnew.tableA LIKE dbold.tableA它将完美地复制表的结构
    2. 将选择插入到新数据库/表中,从旧数据库/表中选择*
  4. 吃午餐或者,根据您的数据库大小,观看电影或重播“IT 人群”
  5. 欣赏您复制的数据库。

修正:在我之前的研究中,我发现有一个版本的 mySql 有重复的数据库命令,但它有错误并在以后的版本中被删除。即使您运行的版本具有可用的命令,遵循这些步骤仍然比使用该命令更好。

Basically no, MYSql does not have a duplicate database feature. Some external tools might, but none that I'm aware of. I did this process using php, but I'm sure it can be done using stored procedures or anything other middle tier application. Here's the steps I followed in high level. I assume you know the details of how to do each step.

  1. Create the new DB
  2. Query all the tables of that db, I had access to information_schema so I just did a select from there.
  3. Loop over the tables.
    1. run something like this CREATE TABLE dbnew.tableA LIKE dbold.tableA It will duplicate the structure of the table perfectly
    2. Insert Select into your new db/table selecting * from old db/table
  4. have lunch or, depending on your database size, watch a movie or re-runs of "the IT crowd"
  5. enjoy your copied database.

Amendment: In my research doing this before, I found that there was a version of mySql that had a duplicate database command, but it was buggy and taken out in future versions. Even if you are running the version with that command available, you are still better off following these steps than using that command.

往事风中埋 2024-11-23 11:32:58

复制表适用于我这个过程(新的数据库名称作为参数)

BEGIN
    DECLARE done1 INT DEFAULT FALSE; 
    DECLARE tablename TEXT; 
    DECLARE cursortable CURSOR FOR (
        SELECT table_name
        FROM information_schema.tables
        WHERE
            table_schema='sansentinel'
        ORDER BY table_name ASC
    ); 
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done1 = TRUE;
    START TRANSACTION;
    # create db
    set @createinstance := concat("CREATE DATABASE `",INSTANCENAME,"`"); 
    prepare createinstance from @createinstance; 
    execute createinstance;

    OPEN cursortable; 
    read_loop: LOOP 
            FETCH cursortable INTO tablename; 
            IF done1 THEN 
                    LEAVE read_loop; 
            END IF; 

            set @createtable := concat("CREATE TABLE `",INSTANCENAME,"`.`",tablename,"` LIKE `sansentinel`.`",tablename,"`"); 
            prepare createtable from @createtable; 
            execute createtable;
    END LOOP; 
    CLOSE cursortable;
    COMMIT;
END

Copy table works for me with this procedure (new db name as argument)

BEGIN
    DECLARE done1 INT DEFAULT FALSE; 
    DECLARE tablename TEXT; 
    DECLARE cursortable CURSOR FOR (
        SELECT table_name
        FROM information_schema.tables
        WHERE
            table_schema='sansentinel'
        ORDER BY table_name ASC
    ); 
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done1 = TRUE;
    START TRANSACTION;
    # create db
    set @createinstance := concat("CREATE DATABASE `",INSTANCENAME,"`"); 
    prepare createinstance from @createinstance; 
    execute createinstance;

    OPEN cursortable; 
    read_loop: LOOP 
            FETCH cursortable INTO tablename; 
            IF done1 THEN 
                    LEAVE read_loop; 
            END IF; 

            set @createtable := concat("CREATE TABLE `",INSTANCENAME,"`.`",tablename,"` LIKE `sansentinel`.`",tablename,"`"); 
            prepare createtable from @createtable; 
            execute createtable;
    END LOOP; 
    CLOSE cursortable;
    COMMIT;
END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文