:: ***************************************************************************:: FILE: RENAME_MYSQLDB.BAT:: ***************************************************************************:: DESCRIPTION:: This is a Windows /MS-DOS batch file that automates renaming a MySQL database:: by using MySQLDump, MySQLAdmin, and MySQL to perform the required tasks.:: The MySQL\bin folder needs to be in your environment path or the working directory.:::: WARNING: The script will delete the original database, but only if it successfully:: created the new copy. However, read the disclaimer below before using.:::: DISCLAIMER:: This script is provided without any express or implied warranties whatsoever.:: The user must assume the risk of using the script.:::: You are free to use, modify, and distribute this script without exception.:: ***************************************************************************
:INITIALIZE@ECHO OFFIF [%2]==[] GOTO HELPIF [%3]==[] (SET RDB_ARGS=--user=root) ELSE (SET RDB_ARGS=%3 %4 %5 %6 %7 %8 %9)SET RDB_OLDDB=%1SET RDB_NEWDB=%2SET RDB_DUMPFILE=%RDB_OLDDB%_dump.sqlGOTO START
:STARTSET RDB_STEP=1ECHO Dumping "%RDB_OLDDB%"...mysqldump %RDB_ARGS% %RDB_OLDDB% > %RDB_DUMPFILE%IF %ERRORLEVEL% NEQ 0 GOTO ERROR_ABORTSET RDB_STEP=2ECHO Creating database "%RDB_NEWDB%"...mysqladmin %RDB_ARGS% create %RDB_NEWDB%IF %ERRORLEVEL% NEQ 0 GOTO ERROR_ABORTSET RDB_STEP=3ECHO Loading dump into "%RDB_NEWDB%"...mysql %RDB_ARGS% %RDB_NEWDB% < %RDB_DUMPFILE%IF %ERRORLEVEL% NEQ 0 GOTO ERROR_ABORTSET RDB_STEP=4ECHO Dropping database "%RDB_OLDDB%"...mysqladmin %RDB_ARGS% drop %RDB_OLDDB% --forceIF %ERRORLEVEL% NEQ 0 GOTO ERROR_ABORTSET RDB_STEP=5ECHO Deleting dump...DEL %RDB_DUMPFILE%IF %ERRORLEVEL% NEQ 0 GOTO ERROR_ABORTECHO Renamed database "%RDB_OLDDB%" to "%RDB_NEWDB%".GOTO END
:ERROR_ABORTIF %RDB_STEP% GEQ 3 mysqladmin %RDB_ARGS% drop %NEWDB% --forceIF %RDB_STEP% GEQ 1 IF EXIST %RDB_DUMPFILE% DEL %RDB_DUMPFILE%ECHO Unable to rename database "%RDB_OLDDB%" to "%RDB_NEWDB%".GOTO END
:HELPECHO Renames a MySQL database.ECHO Usage: %0 database new_database [OPTIONS]ECHO Options: Any valid options shared by MySQL, MySQLAdmin and MySQLDump.ECHO --user=root is used if no options are specified.GOTO END
:ENDSET RDB_OLDDB=SET RDB_NEWDB=SET RDB_ARGS=SET RDB_DUMP=SET RDB_STEP=
mysql -u username -ppassword old_db -sNe 'show tables' | while read table; \do mysql -u username -ppassword -sNe "rename table old_db.$table to new_db.$table"; done
或
for table in `mysql -u root -ppassword -s -N -e "use old_db;show tables from old_db;"`; do mysql -u root -ppassword -s -N -e "use old_db;rename table old_db.$table to new_db.$table;"; done;
DROP PROCEDURE IF EXISTS mysql.rename_db;DELIMITER ||CREATE PROCEDURE mysql.rename_db(IN old_db VARCHAR(100), IN new_db VARCHAR(100))BEGINSELECT CONCAT('CREATE DATABASE ', new_db, ';') `# create new database`;SELECT CONCAT('RENAME TABLE `', old_db, '`.`', table_name, '` TO `', new_db, '`.`', table_name, '`;') `# alter table` FROM information_schema.tables WHERE table_schema = old_db;SELECT CONCAT('DROP DATABASE `', old_db, '`;') `# drop old database`;END||DELIMITER ;
$ time mysql -uroot -e "call mysql.rename_db('db1', 'db2');" | mysql -uroot
然而,目标db中的任何触发器都不会开心。您需要先删除它们,然后在重命名后重新创建它们。
mysql -uroot -e "call mysql.rename_db('test', 'blah2');" | mysql -urootERROR 1435 (HY000) at line 4: Trigger in wrong schema
< p > / * !40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT /;/ < / em > !40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS /;/ < / em > !40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION /;/ < / em > !40101 SET NAMES utf8 */
--
——数据库:mydatab_online
--
——表0的表结构
创建表如果不存在user (timestamp int(15) NOT NULL default '0',ip varchar(40) NOT NULL default ",file varchar(100) NOT NULL default ",主键timestamp,键ip (ip),键file (file))引擎=MyISAM默认CHARSET=latin1;
-- stored procedure rename_db: Rename a database my means of table copying.-- Caveats:-- Will clobber any existing database with the same name as the 'new' database name.-- ONLY copies tables; stored procedures and other database objects are not copied.-- Tomer Altman (taltman@ai.sri.com)
delimiter //DROP PROCEDURE IF EXISTS rename_db;CREATE PROCEDURE rename_db(IN old_db VARCHAR(100), IN new_db VARCHAR(100))BEGINDECLARE current_table VARCHAR(100);DECLARE done INT DEFAULT 0;DECLARE old_tables CURSOR FOR select table_name from information_schema.tables where table_schema = old_db;DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
SET @output = CONCAT('DROP SCHEMA IF EXISTS ', new_db, ';');PREPARE stmt FROM @output;EXECUTE stmt;
SET @output = CONCAT('CREATE SCHEMA IF NOT EXISTS ', new_db, ';');PREPARE stmt FROM @output;EXECUTE stmt;
OPEN old_tables;REPEATFETCH old_tables INTO current_table;IF NOT done THENSET @output = CONCAT('alter table ', old_db, '.', current_table, ' rename ', new_db, '.', current_table, ';');PREPARE stmt FROM @output;EXECUTE stmt;
END IF;UNTIL done END REPEAT;
CLOSE old_tables;
END//delimiter ;
history -d $((HISTCMD-1)) && mysql -udb_user -p'db_password' -Dold_schema -ABNnqre'SHOW TABLES;' | sed -e's/.*/RENAME TABLE old_schema.`&` TO new_schema.`&`;/' | mysql -udb_user -p'db_password' -Dnew_schema
SELECT CONCAT('RENAME TABLE ',table_schema,'.`',table_name,'` TO ','new_schema.`',table_name,'`;')FROM information_schema.TABLESWHERE table_schema LIKE 'old_schema';
DELIMITER //DROP PROCEDURE IF EXISTS `rename_database`;CREATE PROCEDURE `rename_database` (IN `old_name` VARCHAR(20), IN `new_name` VARCHAR(20))BEGINDECLARE `current_table_name` VARCHAR(20);DECLARE `done` INT DEFAULT 0;DECLARE `table_name_cursor` CURSOR FOR SELECT `table_name` FROM `information_schema`.`tables` WHERE (`table_schema` = `old_name`);DECLARE CONTINUE HANDLER FOR NOT FOUND SET `done` = 1;
SET @sql_string = CONCAT('CREATE DATABASE IF NOT EXISTS `', `new_name` , '`;');PREPARE `statement` FROM @sql_string;EXECUTE `statement`;DEALLOCATE PREPARE `statement`;
OPEN `table_name_cursor`;REPEATFETCH `table_name_cursor` INTO `current_table_name`;IF NOT `done` THEN
SET @sql_string = CONCAT('RENAME TABLE `', `old_name`, '`.`', `current_table_name`, '` TO `', `new_name`, '`.`', `current_table_name`, '`;');PREPARE `statement` FROM @sql_string;EXECUTE `statement`;DEALLOCATE PREPARE `statement`;
END IF;UNTIL `done` END REPEAT;CLOSE `table_name_cursor`;
SET @sql_string = CONCAT('DROP DATABASE `', `old_name`, '`;');PREPARE `statement` FROM @sql_string;EXECUTE `statement`;DEALLOCATE PREPARE `statement`;END//DELIMITER ;
mysql> select TABLE_NAME from information_schema.tables wheretable_schema='<old_schema_name>' and TABLE_TYPE='VIEW';$ mysqldump <database> <view1> <view2> … > views.out
4)删除old_schema中当前表上的触发器。
mysql> DROP TRIGGER <trigger_name>;...
在步骤#2中找到的所有“Base”表重命名后,恢复上述转储文件。
mysql> RENAME TABLE <old_schema>.table_name TO <new_schema>.table_name;...$ mysql <new_schema> < views.out$ mysql <new_schema> < stored_routines_triggers_events.out
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || emp || mysql || performance_schema || test |+--------------------+
[root@dba ~]# time /tmp/rename_db localhost emp emp_testcreate database emp_test DEFAULT CHARACTER SET latin1drop trigger salary_triggerrename table emp.__emp_new to emp_test.__emp_newrename table emp._emp_new to emp_test._emp_newrename table emp.departments to emp_test.departmentsrename table emp.dept to emp_test.deptrename table emp.dept_emp to emp_test.dept_emprename table emp.dept_manager to emp_test.dept_managerrename table emp.emp to emp_test.emprename table emp.employees to emp_test.employeesrename table emp.salaries_temp to emp_test.salaries_temprename table emp.titles to emp_test.titlesloading viewsloading triggers, routines and eventsDropping database emp
real 0m0.643suser 0m0.053ssys 0m0.131s
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || emp_test || mysql || performance_schema || test |+--------------------+
经过几天的挠头和拔头发,我今天发布了这个如何使用MySQL修改数据库名称?。解决方案非常简单,将一个模式导出到一个.sql文件中,然后打开该文件并在顶部的sql CREAT TABLE部分中更改数据库/模式名称。如果将多个模式保存到文件中,则有三个或更多实例,并且可能不在页面顶部。以这种方式编辑整个数据库是可能的,但我预计在大型数据库中,跟踪一个表属性或索引的所有实例可能是相当痛苦的。< / p >