Freelance Web Engineer

データベース名を変更するには

2011-02-28  mysql

一度作成したデータベースの名前を変える方法です。バージョンは5.1.41です。

MySQL 5.1.7以前では、RENAME DATABASEを使えば簡単にできたのですが、危険を伴うため5.1.23からこのクエリは使えなくなっています。

This statement was added in MySQL 5.1.7 but was found to be dangerous and was removed in MySQL 5.1.23. It was intended to enable upgrading pre-5.1 databases to use the encoding implemented in 5.1 for mapping database names to database directory names (see Section 8.2.3, “Mapping of Identifiers to File Names”). However, use of this statement could result in loss of database contents, which is why it was removed. Do not use RENAME DATABASE in earlier versions in which it is present.

To perform the task of upgrading database names with the new encoding, use ALTER DATABASE db_name UPGRADE DATA DIRECTORY NAME instead (see Section 12.1.1, “ALTER DATABASE Syntax”).

MySQL :: MySQL 5.1 Reference Manual :: 12.1.32 RENAME DATABASE Syntax

ではどうするかというと、泥臭くやるしか無いのです。create databaseして、旧DBからmysqldumpして、新DBに流し込みます。

1. DB作成

$ mysql -u[ユーザー名] -p[パスワード]
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 45
Server version: 5.1.41-3ubuntu12.8 (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database [new_db] character set utf8;

2. 旧DBからデータをダンプ

mysqldump -u[ユーザー名] -p[パスワード] [old_db] > [old_db].sql

3. 新DBに流しこみ

mysql -u[ユーザー名] -p[パスワード] [new_db] < [old_db].sql

以上、参考になれば幸いです。

参考

comments powered by Disqus