安全快速更改MySQL數據庫名稱

導讀 MySQL似乎沒有更改數據庫名稱的語句(也許是我不知道),如果你有數據庫服務器的管理權限,可以直接更改一下目錄名即可,但如果沒有權限,可以通過更改表名達到修改數據庫名的目的。下面是把centos數據庫更改爲centos_old。
1、新建數據庫centos_old.
mysql > create database centos_old;
2、使用select concat拼成所有rename table的語句。
    mysql -uroot -p -e "select concat('rename table centos.',table_name,' to centos_old.',table_name,';') from information_schema.TABLES where TABLE_SCHEMA='centos';" > rename_mysql_name.sql

打開rename_mysql_name.sql,把第一行刪除。

rename_mysql_name.sql內容大概爲:

    rename table centos.wp_commentmeta to centos_old.wp_commentmeta;
    rename table centos.wp_comments to centos_old.wp_comments;
    rename table centos.wp_forum_forums to centos_old.wp_forum_forums;
    rename table centos.wp_forum_groups to centos_old.wp_forum_groups;
    rename table centos.wp_forum_posts to centos_old.wp_forum_posts;
    rename table centos.wp_forum_threads to centos_old.wp_forum_threads;
    rename table centos.wp_forum_usergroup2user to centos_old.wp_forum_usergroup2user;
    rename table centos.wp_forum_usergroups to centos_old.wp_forum_usergroups;
    rename table centos.wp_links to centos_old.wp_links;
    rename table centos.wp_options to centos_old.wp_options;
    rename table centos.wp_postmeta to centos_old.wp_postmeta;
    rename table centos.wp_posts to centos_old.wp_posts;
    rename table centos.wp_term_relationships to centos_old.wp_term_relationships;
    rename table centos.wp_term_taxonomy to centos_old.wp_term_taxonomy;
    rename table centos.wp_terms to centos_old.wp_terms;
    rename table centos.wp_usermeta to centos_old.wp_usermeta;
    rename table centos.wp_users to centos_old.wp_users;
3、執行rename語句
mysql -uroot -p < rename_mysql_name.sql

這樣就完成了centos數據庫名更改爲centos_old的操作。


本文轉載自:http://www.linuxprobe.com/linux-mysql.html

免費提供最新Linux技術教程書籍,爲開源技術愛好者努力做得更多更好:http://www.linuxprobe.com/

發佈了268 篇原創文章 · 獲贊 14 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章