MySQL手册中有介绍。
通常我只是转储数据库并用一个新名称重新导入它。这不是非常大的数据库的一个选项。重命名数据库| SCHEMA} db_name TO new_db_name做坏事,只存在于少数版本中,总的来说是个坏主意。
这需要与InnoDB一起工作,InnoDB存储的东西与MyISAM非常不同。
MySQL手册中有介绍。
通常我只是转储数据库并用一个新名称重新导入它。这不是非常大的数据库的一个选项。重命名数据库| SCHEMA} db_name TO new_db_name做坏事,只存在于少数版本中,总的来说是个坏主意。
这需要与InnoDB一起工作,InnoDB存储的东西与MyISAM非常不同。
当前回答
我是这样做的: 对现有数据库进行备份。它将为您提供一个db.zip.tmp,然后在命令提示符中写入以下内容
"C:\Program Files (x86)\MySQL\MySQL Server 5.6\bin\ MySQL .exe localhost -u root -p[password][新数据库名]< "C:\Backups\db.zip.tmp"
其他回答
我是这样做的: 对现有数据库进行备份。它将为您提供一个db.zip.tmp,然后在命令提示符中写入以下内容
"C:\Program Files (x86)\MySQL\MySQL Server 5.6\bin\ MySQL .exe localhost -u root -p[password][新数据库名]< "C:\Backups\db.zip.tmp"
这里已经有很多非常好的答案,但我没有看到PHP版本。这将在大约一秒钟内复制一个800M DB。
$oldDbName = "oldDBName";
$newDbName = "newDBName";
$oldDB = new mysqli("localhost", "user", "pass", $oldDbName);
if($oldDB->connect_errno){
echo "Failed to connect to MySQL: (" . $oldDB->connect_errno . ") " . $oldDB->connect_error;
exit;
}
$newDBQuery = "CREATE DATABASE IF NOT EXISTS {$newDbName}";
$oldDB->query($newDBQuery);
$newDB = new mysqli("localhost", "user", "pass");
if($newDB->connect_errno){
echo "Failed to connect to MySQL: (" . $newDB->connect_errno . ") " . $newDB->connect_error;
exit;
}
$tableQuery = "SHOW TABLES";
$tableResult = $oldDB->query($tableQuery);
$renameQuery = "RENAME TABLE\n";
while($table = $tableResult->fetch_array()){
$tableName = $table["Tables_in_{$oldDbName}"];
$renameQuery .= "{$oldDbName}.{$tableName} TO {$newDbName}.{$tableName},";
}
$renameQuery = substr($renameQuery, 0, strlen($renameQuery) - 1);
$newDB->query($renameQuery);
TodoInTX的存储过程不太适合我。以下是我的尝试:
-- 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)) BEGIN DECLARE 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; REPEAT FETCH old_tables INTO current_table; IF NOT done THEN SET @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 ;
这是我写的一个批处理文件,从命令行自动化,但它适用于Windows/MS-DOS。
语法:rename_mysqldb database newdatabase -u [user] -p[password]
:: ***************************************************************************
:: 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 OFF
IF [%2]==[] GOTO HELP
IF [%3]==[] (SET RDB_ARGS=--user=root) ELSE (SET RDB_ARGS=%3 %4 %5 %6 %7 %8 %9)
SET RDB_OLDDB=%1
SET RDB_NEWDB=%2
SET RDB_DUMPFILE=%RDB_OLDDB%_dump.sql
GOTO START
:START
SET RDB_STEP=1
ECHO Dumping "%RDB_OLDDB%"...
mysqldump %RDB_ARGS% %RDB_OLDDB% > %RDB_DUMPFILE%
IF %ERRORLEVEL% NEQ 0 GOTO ERROR_ABORT
SET RDB_STEP=2
ECHO Creating database "%RDB_NEWDB%"...
mysqladmin %RDB_ARGS% create %RDB_NEWDB%
IF %ERRORLEVEL% NEQ 0 GOTO ERROR_ABORT
SET RDB_STEP=3
ECHO Loading dump into "%RDB_NEWDB%"...
mysql %RDB_ARGS% %RDB_NEWDB% < %RDB_DUMPFILE%
IF %ERRORLEVEL% NEQ 0 GOTO ERROR_ABORT
SET RDB_STEP=4
ECHO Dropping database "%RDB_OLDDB%"...
mysqladmin %RDB_ARGS% drop %RDB_OLDDB% --force
IF %ERRORLEVEL% NEQ 0 GOTO ERROR_ABORT
SET RDB_STEP=5
ECHO Deleting dump...
DEL %RDB_DUMPFILE%
IF %ERRORLEVEL% NEQ 0 GOTO ERROR_ABORT
ECHO Renamed database "%RDB_OLDDB%" to "%RDB_NEWDB%".
GOTO END
:ERROR_ABORT
IF %RDB_STEP% GEQ 3 mysqladmin %RDB_ARGS% drop %NEWDB% --force
IF %RDB_STEP% GEQ 1 IF EXIST %RDB_DUMPFILE% DEL %RDB_DUMPFILE%
ECHO Unable to rename database "%RDB_OLDDB%" to "%RDB_NEWDB%".
GOTO END
:HELP
ECHO 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
:END
SET RDB_OLDDB=
SET RDB_NEWDB=
SET RDB_ARGS=
SET RDB_DUMP=
SET RDB_STEP=
在MySQL管理员中执行以下操作:
在Catalogs下,创建一个新的数据库模式。 转到备份并创建的备份 旧的模式。 执行备份。 转到恢复并打开文件 在步骤3中创建。 在目标下选择“另一个模式” 模式并选择新的数据库 模式。 开始恢复。 验证新模式,如果看起来 很好,删除旧的。