變更 wordpress 資料庫名稱

之前我就很想改, 但是一直懶惰沒有測試. 現在終於整理一下資料庫的名稱了. 方法如下.

進去 MySQL 更改 database 及 tables 名稱. 語法如下 :

RENAME TABLE OLD_DB.OLD_PREFIX_commentmeta TO NEW_DB.NEW_PREFIX_commentmeta,
OLD_DB.OLD_PREFIX_comments TO NEW_DB.NEW_PREFIX_comments,
OLD_DB.OLD_PREFIX_links TO NEW_DB.NEW_PREFIX_links,
OLD_DB.OLD_PREFIX_options TO NEW_DB.NEW_PREFIX_options,
OLD_DB.OLD_PREFIX_postmeta TO NEW_DB.NEW_PREFIX_postmeta,
OLD_DB.OLD_PREFIX_posts TO NEW_DB.NEW_PREFIX_posts,
OLD_DB.OLD_PREFIX_terms TO NEW_DB.NEW_PREFIX_terms,
OLD_DB.OLD_PREFIX_term_relationships TO NEW_DB.NEW_PREFIX_term_relationships,
OLD_DB.OLD_PREFIX_term_taxonomy TO NEW_DB.NEW_PREFIX_term_taxonomy,
OLD_DB.OLD_PREFIX_usermeta TO NEW_DB.NEW_PREFIX_usermeta,
OLD_DB.OLD_PREFIX_users TO NEW_DB.NEW_PREFIX_users

如果沒有要改 database. 那就直接省略 OLD_DB. 與 NEW_DB.

改完之後還要進去資料庫裡面更新 prefix 語法如下 :

UPDATE `NEW_PREFIX_usermeta` SET `meta_key` = replace(`meta_key`, ‘OLD_PREFIX_’, ‘NEW_PREFIX_’);
UPDATE `NEW_PREFIX_options` SET `option_name` = replace(`option_name`, ‘OLD_PREFIX_’, ‘NEW_PREFIX_’);

最後去 wordpress 裡面改 wp-config.php 檔案 下列兩行就可以了. 如果你的資料庫的帳號密碼有改變也記得改一下

define(‘DB_NAME’, ‘NEW_DB’);
$table_prefix = ‘NEW_PREFIX_’;