How do I quickly rename a MySQL database (change schema name)?

前端 未结 30 2428
余生分开走
余生分开走 2020-11-22 14:54

The MySQL manual at MySQL covers this.

Usually I just dump the database and reimport it with a new name. This is not an option for very big databases. Apparently

相关标签:
30条回答
  • 2020-11-22 15:02

    I think the solution is simpler and was suggested by some developers. phpMyAdmin has an operation for this.

    From phpMyAdmin, select the database you want to select. In the tabs there's one called Operations, go to the rename section. That's all.

    It does, as many suggested, create a new database with the new name, dump all tables of the old database into the new database and drop the old database.

    Enter image description here

    0 讨论(0)
  • 2020-11-22 15:03

    Simplest bullet-and-fool-proof way of doing a complete rename (including dropping the old database at the end so it's a rename rather than a copy):

    mysqladmin -uroot -pmypassword create newdbname
    mysqldump -uroot -pmypassword --routines olddbname | mysql -uroot -pmypassword newdbname
    mysqladmin -uroot -pmypassword drop olddbname
    

    Steps:

    1. Copy the lines into Notepad.
    2. Replace all references to "olddbname", "newdbname", "mypassword" (+ optionally "root") with your equivalents.
    3. Execute one by one on the command line (entering "y" when prompted).
    0 讨论(0)
  • 2020-11-22 15:03

    I posed a question on Server Fault trying to get around downtime when restoring very large databases by using MySQL Proxy. I didn't have any success, but I realized in the end what I wanted was RENAME DATABASE functionality because dump/import wasn't an option due to the size of our database.

    There is a RENAME TABLE functionality built in to MySQL so I ended up writing a simple Python script to do the job for me. I've posted it on GitHub in case it could be of use to others.

    0 讨论(0)
  • 2020-11-22 15:04

    I've only recently came across a very nice way to do it, works with MyISAM and InnoDB and is very fast:

    RENAME TABLE old_db.table TO new_db.table;
    

    I don't remember where I read it but credit goes to someone else not me.

    0 讨论(0)
  • 2020-11-22 15:04

    Here is a quick way to generate renaming sql script, if you have many tables to move.

    SELECT DISTINCT CONCAT('RENAME TABLE ', t.table_schema,'.', t.table_name, ' TO ',     
    t.table_schema, "_archive", '.', t.table_name, ';' ) as Rename_SQL 
    FROM information_schema.tables t
    WHERE table_schema='your_db_name' ;
    
    0 讨论(0)
  • 2020-11-22 15:05

    Emulating the missing RENAME DATABASE command in MySQL:

    1. Create a new database
    2. Create the rename queries with:

      SELECT CONCAT('RENAME TABLE ',table_schema,'.',table_name,
          ' TO ','new_schema.',table_name,';')
      FROM information_schema.TABLES
      WHERE table_schema LIKE 'old_schema';
      
    3. Run that output

    4. Delete old database

    It was taken from Emulating The Missing RENAME DATABASE Command in MySQL.

    0 讨论(0)
提交回复
热议问题