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

前端 未结 30 2431
余生分开走
余生分开走 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:17

    Steps :

    1. Hit http://localhost/phpmyadmin/
    2. Select your DB
    3. Click on Operations Tab
    4. There will be a tab as "Rename database to". Add new name and check Adjust privileges.
    5. Click on Go.

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

    Seems noone mentioned this but here is another way:

    create database NewDatabaseName like OldDatabaseName;
    

    then for each table do:

    create NewDatabaseName.tablename like OldDatabaseName.tablename;
    insert into NewDataBaseName.tablename select * from OldDatabaseName.tablename;
    

    then, if you want to,

    drop database OldDatabaseName;
    

    This approach would have the advantage of doing the entire transfer on server with near zero network traffic, so it will go a lot faster than a dump/restore.

    If you do have stored procedures/views/etc you might want to transfer them as well.

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

    Use these few simple commands:

    mysqldump -u username -p -v olddatabase > olddbdump.sql
    mysqladmin -u username -p create newdatabase
    mysql -u username -p newdatabase < olddbdump.sql
    

    Or to reduce I/O use the following as suggested by @Pablo Marin-Garcia:

    mysqladmin -u username -p create newdatabase
    mysqldump -u username -v olddatabase -p | mysql -u username -p -D newdatabase
    
    0 讨论(0)
  • 2020-11-22 15:19

    For your convenience, below is a small shellscript that has to be executed with two parameters: db-name and new db-name.

    You might need to add login-parameters to the mysql-lines if you don't use the .my.cnf-file in your home-directory. Please make a backup before executing this script.


    #!/usr/bin/env bash
    
    mysql -e "CREATE DATABASE $2 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"
    for i in $(mysql -Ns $1 -e "show tables");do
        echo "$1.$i -> $2.$i"
        mysql -e "rename TABLE $1.$i to $2.$i"
    done
    mysql -e "DROP DATABASE $1"
    
    0 讨论(0)
  • 2020-11-22 15:20

    MySQL does not support the renaming of a database through its command interface at the moment, but you can rename the database if you have access to the directory in which MySQL stores its databases. For default MySQL installations this is usually in the Data directory under the directory where MySQL was installed. Locate the name of the database you want to rename under the Data directory and rename it. Renaming the directory could cause some permissions issues though. Be aware.

    Note: You must stop MySQL before you can rename the database

    I would recommend creating a new database (using the name you want) and export/import the data you need from the old to the new. Pretty simple.

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

    When you rename a database in PHPMyAdmin it creates a dump, then drops and recreates the database with the new name.

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