mysql學習筆記之十六(資料庫維護),mysql學習筆記

來源:互聯網
上載者:User

mysql學習筆記之十六(資料庫維護),mysql學習筆記

資料備份    資料損失原因:儲存介質鼓掌,使用者操作錯誤(誤刪除整個資料庫),伺服器徹底癱瘓    1、檔案複製        需要先停止伺服器服務,在停止之前,執行flushtables將所有資料寫入到資料檔案,停止之後,將資料檔案拷貝到其他地方        只適合MyISAM儲存引擎,對其他引擎並不合適    2、mysqldump        mysqldump將包含資料的表結構和資料內容儲存在相應的文字檔。具體執行時,首先檢查備份資料的表結構,在相應的文字檔中產生create語句。然後檢查資料內容,在相應的文字檔中產生insert into 語句。將來需要進行還原時,只需要執行文字檔中的create和insert into        三種形式:            備份一個資料庫                mysqldump -u username -p dname table1,table2,... >backupname.sql                不指定table則會備份整個資料庫                ">"兩邊要有空格。                例:                    C:\Users\208-1>mysqldump -u root -p123456 leetcode t_scores t_employee > D:\AppServ\MySQL\backupleetcode.sql                    C:\Users\208-1>mysqldump -u root -p123456 leetcode > D:\AppServ\MySQL\backupleetcode1.sql                mysqldump不能再mysql>後面輸入,直接在dos視窗執行            備份多個資料庫                mysqldump -u username -p --databases dbname1 dbname2 ... > backupname.sql                各個資料庫之間不能加逗號直接用空格隔開就行            備份所有資料庫                mysqldump -u username -p --all-databases > backupname.sql                all-databases是連在一起的。資料還原    1、複本備份檔案還原資料庫        這種方式必須保證兩個mysql資料庫的主要版本號一致,因為只有主要版本號相同時,才能保證兩個mysql資料庫的檔案類型是相同的       僅對MyISAM類型的表有效        主要版本號,MySQL 5.5.21和MySQL 5.5.01主要版本號是相同的,第一個數字是主要版本號     2、mysql        mysql -u username -p [dbname]<backup.sql        指定dbname,則還原該資料庫下的表,不指定,則還原備份檔案中所有的資料庫匯出    通過資料庫中表的匯入和匯出操作,可以在mysql資料庫伺服器與其他資料庫伺服器間(sql server 、Oracle)輕鬆移動資料    匯出:        將資料從mysql資料表裡複製到文字檔    匯入        將資料從文字檔載入到mysql資料庫表裡    1、select...into outfile        select [filename] from table_name [where condition] into outfile 'filename' [option]        兩部分:普通查詢語句(查詢要匯出的資料)和匯出位置        option的取值:            fileds terminated by "string"       用來設定欄位的分隔字元字串對象(string),預設為定位字元            fields enclosed by "char"       用來設定括上欄位值的字元符號,預設不使用任何符號            fields optionally enclosed by 'char'    用來括上char,varchar和text等欄位的字元符號,預設不使用任何符號            fieles escaped by 'char'        設定逸出字元的字元符號,預設情況下使用"\"字元            lines starting by 'char'        設定每行開頭的字元符號,預設不使用任何符號            lines terminated by 'string'        設定每行結束的字串符號,預設使用'\n'    2、mysqldump        mysqldump -u root -p -T file_directory dbname tablename[option]        option的取值:            --fileds-terminated-by=string            --fileds-enclosed-by=char            --fileds-optionally-enclosed-by=char            --lines-terminated-by=string匯入    1、load data infile        load data[local] infile filename into table table_name[option]        local:指定在本機電腦中尋找文字檔;        filename:用來指定文字檔的路徑和名稱;        tablename:用來指定表的名稱        oprion的取值            fileds terminated by "string"       用來設定欄位的分隔字元字串對象(string),預設為定位字元            fields enclosed by "char"       用來設定括上欄位值的字元符號,預設不使用任何符號            fields optionally enclosed by 'char'    用來括上char,varchar和text等欄位的字元符號,預設不使用任何符號            fieles escaped by 'char'        設定逸出字元的字元符號,預設情況下使用"\"字元            lines starting by 'char'        設定每行開頭的字元符號,預設不使用任何符號            lines terminated by 'string'        設定每行結束的字串符號,預設使用'\n'            ignore n lines              實現忽略檔案的前n行記錄            (欄位列表)              實現根據欄位列表中的欄位和順序來載入記錄            set column=expr             用來設定列的轉換條件,即所指定的列經過相應的轉換後會被載入    2、mysqlimport        mysqlimport -u root -p [--local] dbname file_name[oiption]        option的取值:            --fileds-terminated-by=string            --fileds-enclosed-by=char            --fileds-optionally-enclosed-by=char            --lines-terminated-by=string            --ignrs-lines=n        注意:命令裡沒有指定匯入到哪個表裡。按照書上的意思,按照檔案名稱尋找表,然後匯入進去。資料庫遷移    1、相同版本間的mysql資料庫之間的遷移        mysqldump -h hostname -u root -password=password1 --all-databases        |mysql -h hostname2 -u root -password=password2        備份與還原同時操作。        對於相同版本間的mysql資料庫之間的遷移,先使用mysqldump進行備份,然後使用mysql命令將備份檔案還原到新的mysql資料庫。               2、不同版本間的mysql資料庫之間的遷移        低版本->高版本最是容易實現,因為高版本相容低版本        對於MyISAM的表              使用檔案直接複製的方式或者mysqlhotcopy        對於InnoDB的表            使用mysqldump備份,使用mysql命令還原    3、不同資料庫間的遷移         mysql->sql server :通過MyODBC實現遷移        mysql->oracle:先通過mysqldump命令匯出sql檔案,手動修改                sql中的create語句

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.