我們知道 WordPress 的資料庫表,可以設定字首,預設是 wp_,很多站長也就預設用了 wp_,如果某種原因 (比如提高安全性) 要修改的 WordPress 資料的字首,我們應該怎麼做?

開始之前修改資料是一件風險很高的工作,開始之前必然就是做好資料庫備份:WordPress Database Backup 備份資料庫,phpMyAdmin 匯出資料庫。

wp-config.php 修改資料庫字首

開啟你的 wp-config.php 檔案,把資料庫字首 wp_ 改成你想要的字首,比如 xhsay_。

1

$table_prefix  ='xhsay_';

修改資料表名字

開啟 PHPMySQL,找到你的資料庫,然後執行以下 SQL,把所有資料庫表字首從 wp_ 改成 xhsay_ 。

RENAME table'wp_commentmeta'TO'xhsay_commentmeta';

RENAME table'wp_comments'TO'xhsay_comments';

RENAME table'wp_links'TO'xhsay_links';

RENAME table'wp_options'TO'xhsay_options';

RENAME table'wp_postmeta'TO'xhsay_postmeta';

RENAME table'wp_posts'TO'xhsay_posts';

RENAME table'wp_terms'TO'xhsay_terms';

RENAME table'wp_term_relationships'TO'xhsay_term_relationships';

RENAME table'wp_term_taxonomy'TO'xhsay_term_taxonomy';

RENAME table'wp_usermeta'TO'xhsay_usermeta';

RENAME table'wp_users'TO'xhsay_users';

修改 Options 表中的資料

使用下面語句把 options 表中的 option_name 以 wp_ 開頭的值改成 xhsay_ 開頭。

1

SELECT REPLACE(option_name,'wp_','xhsay_')FROM wpdx_options;

修改 UserMeta 表中的資料

使用下面語句把 UserMeta 表中的 meta_key 以 wp_ 開頭的值改成 xhsay_ 開頭。

1

SELECT REPLACE(meta_key,'wp_','xhsay_')FROM xhsay_usermeta;

即可。