Duplicate Entire MySQL Database

后端 未结 9 1128
再見小時候
再見小時候 2020-12-04 08:04

Is it posible to duplicate an entire MySQL database on a linux server?

I know I can use export and import but the original database is >25MB so that\'s not ideal.

9条回答
  •  盖世英雄少女心
    2020-12-04 08:24

    Here's a windows bat file I wrote which combines Vincent and Pauls suggestions. It prompts the user for source and destination names.

    Just modify the variables at the top to set the proper paths to your executables / database ports.

    :: Creates a copy of a database with a different name.
    :: User is prompted for Src and destination name.
    :: Fair Warning: passwords are passed in on the cmd line, modify the script with -p instead if security is an issue.
    :: Uncomment the rem'd out lines if you want script to prompt for database username, password, etc.
    
    :: See also: http://stackoverflow.com/questions/1887964/duplicate-entire-mysql-database
    
    @set MYSQL_HOME="C:\sugarcrm\mysql\bin"
    @set mysqldump_exec=%MYSQL_HOME%\mysqldump
    @set mysql_exec=%MYSQL_HOME%\mysql
    @set SRC_PORT=3306
    @set DEST_PORT=3306
    @set USERNAME=TODO_USERNAME
    @set PASSWORD=TODO_PASSWORD
    
    :: COMMENT any of the 4 lines below if you don't want to be prompted for these each time and use defaults above.
    @SET /p USERNAME=Enter database username: 
    @SET /p PASSWORD=Enter database password: 
    @SET /p SRC_PORT=Enter SRC database port (usually 3306): 
    @SET /p DEST_PORT=Enter DEST database port: 
    
    %MYSQL_HOME%\mysql --user=%USERNAME% --password=%PASSWORD% --port=%DEST_PORT% --execute="show databases;"
    @IF NOT "%ERRORLEVEL%" == "0" GOTO ExitScript
    
    @SET /p SRC_DB=What is the name of the SRC Database:  
    @SET /p DEST_DB=What is the name for the destination database (that will be created):  
    
    %mysql_exec% --user=%USERNAME% --password=%PASSWORD% --port=%DEST_PORT% --execute="create database %DEST_DB%;"
    %mysqldump_exec% --add-drop-table --user=%USERNAME% --password=%PASSWORD% --port=%SRC_PORT% %SRC_DB% | %mysql_exec% --user=%USERNAME% --password=%PASSWORD% --port=%DEST_PORT% %DEST_DB%
    @echo SUCCESSFUL!!!
    @GOTO ExitSuccess
    
    :ExitScript
    @echo "Failed to copy database"
    :ExitSuccess
    

    Sample output:

    C:\sugarcrm_backups\SCRIPTS>copy_db.bat
    Enter database username: root
    Enter database password: MyPassword
    Enter SRC database port (usually 3306): 3308
    Enter DEST database port: 3308
    
    C:\sugarcrm_backups\SCRIPTS>"C:\sugarcrm\mysql\bin"\mysql --user=root --password=MyPassword --port=3308 --execute="show databases;"
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sugarcrm_550_pro   |
    | sugarcrm_550_ce    |
    | sugarcrm_640_pro   |
    | sugarcrm_640_ce    |
    +--------------------+
    What is the name of the SRC Database:  sugarcrm
    What is the name for the destination database (that will be created):  sugarcrm_640_ce
    
    C:\sugarcrm_backups\SCRIPTS>"C:\sugarcrm\mysql\bin"\mysql --user=root --password=MyPassword --port=3308 --execute="create database sugarcrm_640_ce;"
    
    C:\sugarcrm_backups\SCRIPTS>"C:\sugarcrm\mysql\bin"\mysqldump --add-drop-table --user=root --password=MyPassword --port=3308 sugarcrm   | "C:\sugarcrm\mysql\bin"\mysql --user=root --password=MyPassword --port=3308 sugarcrm_640_ce
    SUCCESSFUL!!!
    

提交回复
热议问题