MySQL手册中有介绍。
通常我只是转储数据库并用一个新名称重新导入它。这不是非常大的数据库的一个选项。重命名数据库| SCHEMA} db_name TO new_db_name做坏事,只存在于少数版本中,总的来说是个坏主意。
这需要与InnoDB一起工作,InnoDB存储的东西与MyISAM非常不同。
MySQL手册中有介绍。
通常我只是转储数据库并用一个新名称重新导入它。这不是非常大的数据库的一个选项。重命名数据库| SCHEMA} db_name TO new_db_name做坏事,只存在于少数版本中,总的来说是个坏主意。
这需要与InnoDB一起工作,InnoDB存储的东西与MyISAM非常不同。
当前回答
实际上,最简单的答案是导出旧数据库,然后将其导入到您创建的新数据库中以替换旧数据库。当然,您应该使用phpMyAdmin或命令行来做到这一点。
重命名和操纵数据库是一个坏主意!不要这样做。(除非你是那种“黑客类型”的人,在黑暗中坐在妈妈的地下室里,吃着披萨,白天睡觉。)
你最终会遇到比你想要的更多的问题和工作。
So,
Create a new_database and name it the correct way. Go to your phpMyAdmin and open the database you want to export. Export it (check the options, but you should be OK with the defaults. You will get a file like or similar to this. The extension on this file is .sql -- phpMyAdmin SQL Dump -- version 3.2.4 -- http://www.phpmyadmin.net -- Host: localhost -- Generation Time: Jun 30, 2010 at 12:17 PM -- Server version: 5.0.90 -- PHP Version: 5.2.6 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT /; /!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS /; /!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION /; /!40101 SET NAMES utf8 */; -- -- Database: mydatab_online -- -- Table structure for table user CREATE TABLE IF NOT EXISTS user ( timestamp int(15) NOT NULL default '0', ip varchar(40) NOT NULL default '', file varchar(100) NOT NULL default '', PRIMARY KEY (timestamp), KEY ip (ip), KEY file (file) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; -- -- Dumping data for table user INSERT INTO user (timestamp, ip, file) VALUES (1277911052, '999.236.177.116', ''), (1277911194, '999.236.177.116', '');
这将是您的.sql文件。你刚刚导出的那个。
在你的硬盘上找到它;通常在/temp中。选择具有正确名称(阅读本文的原因)的空数据库。 说:导入-开始
通过将程序输入到通常是configuration.php文件中,将程序连接到正确的数据库。刷新服务器(两个。为什么?因为我是UNIX老手,我说过。 现在,你的状态应该很好。如果你还有任何问题,请到网上来找我。
其他回答
有两种方法:
方法1:一个著名的重命名数据库模式的方法是使用Mysqldump转储模式并将其恢复到另一个模式中,然后删除旧的模式(如果需要)。
从壳
mysqldump emp > emp.out
mysql -e "CREATE DATABASE employees;"
mysql employees < emp.out
mysql -e "DROP DATABASE emp;"
虽然上面的方法很简单,但它是时间和空间消耗。如果模式大于100GB怎么办?有一些方法可以将上述命令连接在一起以节省空间,但这不会节省时间。
为了纠正这种情况,有另一种重命名模式的快速方法,但是在执行时必须小心。
方法2:MySQL在重命名表方面有一个非常好的特性,甚至可以跨不同的模式工作。这个重命名操作是原子的,在重命名表时没有其他人可以访问它。这只需要很短的时间就可以完成,因为更改表名或表的模式只是元数据的更改。下面是重命名的过程方法:
使用所需的名称创建新的数据库模式。 使用MySQL的" Rename TABLE "命令将旧模式的表重命名为新模式。 删除旧的数据库模式。 如果模式中有视图、触发器、函数、存储过程,也需要重新创建它们。如果表上存在触发器,MySQL的“RENAME TABLE”将失败。为了解决这个问题,我们可以做以下事情:
1)将触发器、事件和存储的例程转储到单独的文件中。在mysqldump命令中使用-E, -R标志(以及转储触发器的-t -d)。一旦触发器被转储,我们将需要从模式中删除它们,以便RENAME TABLE命令能够工作。
$ mysqldump <old_schema_name> -d -t -R -E > stored_routines_triggers_events.out
2)生成一个只有“BASE”表的列表。可以通过对information_schema的查询找到这些参数。表的表。
mysql> select TABLE_NAME from information_schema.tables where
table_schema='<old_schema_name>' and TABLE_TYPE='BASE TABLE';
3)在输出文件中转储视图。视图可以使用同一个information_schema上的查询来找到。表的表。
mysql> select TABLE_NAME from information_schema.tables where
table_schema='<old_schema_name>' and TABLE_TYPE='VIEW';
$ mysqldump <database> <view1> <view2> … > views.out
4)删除old_schema中当前表上的触发器。
mysql> DROP TRIGGER <trigger_name>;
...
5)在步骤#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
Intricacies with above methods : We may need to update the GRANTS for users such that they match the correct schema_name. These could fixed with a simple UPDATE on mysql.columns_priv, mysql.procs_priv, mysql.tables_priv, mysql.db tables updating the old_schema name to new_schema and calling “Flush privileges;”. Although “method 2″ seems a bit more complicated than the “method 1″, this is totally scriptable. A simple bash script to carry out the above steps in proper sequence, can help you save space and time while renaming database schemas next time.
Percona Remote DBA团队写了一个名为“rename_db”的脚本,其工作方式如下:
[root@dba~]# /tmp/rename_db
rename_db <server> <database> <new_database>
为了演示这个脚本的使用,使用了一个示例模式“emp”,创建测试触发器,在该模式上存储例程。将尝试使用脚本重命名数据库模式,与耗时的转储/还原方法相比,重命名数据库模式需要几秒钟的时间。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| emp |
| mysql |
| performance_schema |
| test |
+--------------------+
[root@dba ~]# time /tmp/rename_db localhost emp emp_test
create database emp_test DEFAULT CHARACTER SET latin1
drop trigger salary_trigger
rename table emp.__emp_new to emp_test.__emp_new
rename table emp._emp_new to emp_test._emp_new
rename table emp.departments to emp_test.departments
rename table emp.dept to emp_test.dept
rename table emp.dept_emp to emp_test.dept_emp
rename table emp.dept_manager to emp_test.dept_manager
rename table emp.emp to emp_test.emp
rename table emp.employees to emp_test.employees
rename table emp.salaries_temp to emp_test.salaries_temp
rename table emp.titles to emp_test.titles
loading views
loading triggers, routines and events
Dropping database emp
real 0m0.643s
user 0m0.053s
sys 0m0.131s
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| emp_test |
| mysql |
| performance_schema |
| test |
+--------------------+
正如您在上面的输出中看到的,数据库模式“emp”在不到一秒钟的时间内就被重命名为“emp_test”。最后,这是来自Percona的脚本,上面用于“方法2″”。
#!/bin/bash
# Copyright 2013 Percona LLC and/or its affiliates
set -e
if [ -z "$3" ]; then
echo "rename_db <server> <database> <new_database>"
exit 1
fi
db_exists=`mysql -h $1 -e "show databases like '$3'" -sss`
if [ -n "$db_exists" ]; then
echo "ERROR: New database already exists $3"
exit 1
fi
TIMESTAMP=`date +%s`
character_set=`mysql -h $1 -e "show create database $2\G" -sss | grep ^Create | awk -F'CHARACTER SET ' '{print $2}' | awk '{print $1}'`
TABLES=`mysql -h $1 -e "select TABLE_NAME from information_schema.tables where table_schema='$2' and TABLE_TYPE='BASE TABLE'" -sss`
STATUS=$?
if [ "$STATUS" != 0 ] || [ -z "$TABLES" ]; then
echo "Error retrieving tables from $2"
exit 1
fi
echo "create database $3 DEFAULT CHARACTER SET $character_set"
mysql -h $1 -e "create database $3 DEFAULT CHARACTER SET $character_set"
TRIGGERS=`mysql -h $1 $2 -e "show triggers\G" | grep Trigger: | awk '{print $2}'`
VIEWS=`mysql -h $1 -e "select TABLE_NAME from information_schema.tables where table_schema='$2' and TABLE_TYPE='VIEW'" -sss`
if [ -n "$VIEWS" ]; then
mysqldump -h $1 $2 $VIEWS > /tmp/${2}_views${TIMESTAMP}.dump
fi
mysqldump -h $1 $2 -d -t -R -E > /tmp/${2}_triggers${TIMESTAMP}.dump
for TRIGGER in $TRIGGERS; do
echo "drop trigger $TRIGGER"
mysql -h $1 $2 -e "drop trigger $TRIGGER"
done
for TABLE in $TABLES; do
echo "rename table $2.$TABLE to $3.$TABLE"
mysql -h $1 $2 -e "SET FOREIGN_KEY_CHECKS=0; rename table $2.$TABLE to $3.$TABLE"
done
if [ -n "$VIEWS" ]; then
echo "loading views"
mysql -h $1 $3 < /tmp/${2}_views${TIMESTAMP}.dump
fi
echo "loading triggers, routines and events"
mysql -h $1 $3 < /tmp/${2}_triggers${TIMESTAMP}.dump
TABLES=`mysql -h $1 -e "select TABLE_NAME from information_schema.tables where table_schema='$2' and TABLE_TYPE='BASE TABLE'" -sss`
if [ -z "$TABLES" ]; then
echo "Dropping database $2"
mysql -h $1 $2 -e "drop database $2"
fi
if [ `mysql -h $1 -e "select count(*) from mysql.columns_priv where db='$2'" -sss` -gt 0 ]; then
COLUMNS_PRIV=" UPDATE mysql.columns_priv set db='$3' WHERE db='$2';"
fi
if [ `mysql -h $1 -e "select count(*) from mysql.procs_priv where db='$2'" -sss` -gt 0 ]; then
PROCS_PRIV=" UPDATE mysql.procs_priv set db='$3' WHERE db='$2';"
fi
if [ `mysql -h $1 -e "select count(*) from mysql.tables_priv where db='$2'" -sss` -gt 0 ]; then
TABLES_PRIV=" UPDATE mysql.tables_priv set db='$3' WHERE db='$2';"
fi
if [ `mysql -h $1 -e "select count(*) from mysql.db where db='$2'" -sss` -gt 0 ]; then
DB_PRIV=" UPDATE mysql.db set db='$3' WHERE db='$2';"
fi
if [ -n "$COLUMNS_PRIV" ] || [ -n "$PROCS_PRIV" ] || [ -n "$TABLES_PRIV" ] || [ -n "$DB_PRIV" ]; then
echo "IF YOU WANT TO RENAME the GRANTS YOU NEED TO RUN ALL OUTPUT BELOW:"
if [ -n "$COLUMNS_PRIV" ]; then echo "$COLUMNS_PRIV"; fi
if [ -n "$PROCS_PRIV" ]; then echo "$PROCS_PRIV"; fi
if [ -n "$TABLES_PRIV" ]; then echo "$TABLES_PRIV"; fi
if [ -n "$DB_PRIV" ]; then echo "$DB_PRIV"; fi
echo " flush privileges;"
fi
如果你正在使用phpMyAdmin,一旦你选择了你想要重命名的数据库,你可以去“操作”选项卡。然后转到最后一节“将数据库复制到”(或类似的内容),给出一个名称,并选择下面的选项。在这种情况下,我猜你必须选择“结构和数据”和“在复制之前创建数据库”复选框,最后,按下该部分中的“go”按钮。
顺便说一下,我在西班牙语中使用phpMyAdmin,所以我不确定英语部分的名称是什么。
TodoInTx的解决方案和user757945的改编解决方案都不适合我的MySQL 5.5.16,所以这里是我的改编版本:
DELIMITER //
DROP PROCEDURE IF EXISTS `rename_database`;
CREATE PROCEDURE `rename_database` (IN `old_name` VARCHAR(20), IN `new_name` VARCHAR(20))
BEGIN
DECLARE `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`;
REPEAT
FETCH `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 ;
希望它能帮助到像我这样处境的人!注意:@sql_string之后将在会话中逗留。不使用它,我就无法写出这个函数。
如果你使用的是phpMyAdmin,那么你只需要到xamp中的mysql文件夹,关闭phpMyAdmin,然后重命名你看到的文件夹作为你的数据库名,然后重新启动你的phpMyAdmin。可以看到数据库重命名了。
最简单的方法是使用HeidiSQL软件。它是免费的,开源的。它可以在Windows和任何带有Wine的Linux上运行(在Linux、BSD、Solaris和Mac OS X上运行Windows应用程序)。
这是HeidiSQL的下载,转到 http://www.heidisql.com/download.php。
下载Wine,请登录http://www.winehq.org/。
要在HeidiSQL中重命名数据库,只需右键单击数据库名称并选择“Edit”。然后输入一个新名称并按“OK”。
就是这么简单。