MySQL資料備份與恢複的相關操作命令

來源:互聯網
上載者:User

將mysql安裝目錄設定到系統內容變數中, 方便在命令列終端直接執行.

linux下mysql安裝後, root預設密碼為空白, 可直接執行mysql 登入.

正常登入命令mysql -h主機地址 -u使用者名稱 -p密碼

注意, 不需加空格, 如果是直接登入原生mysql, 可以不用加-h參數.

資料庫的備份與恢複:

1. 僅備份資料庫結構:

mysqldump -h127.0.0.1 -uroot -p654321 --default-character-set=utf8 --no-data --databases --add-drop-table -R --result-file="c:/bak.sql" test

說明: -R是代表備份預存程序或函數, 但視圖不受此控制.

-result-file 指定備份檔案路徑: windows下需加盤符, linux下輸入完整目錄名.

test 代表的是要備份的資料庫名稱

2. 備份結構和資料(全備份):

mysqldump -h127.0.0.1 -uroot -p654321 --default-character-set=utf8 --databases --add-drop-table -R --result-file="c:/bak.sql" test

說明: 如果要防止在備份期間操作的資料不流失, 增加參數--lock-all-tables=true 鎖定整個表

3. 僅備份部分資料(差異備份)

mysqldump -h127.0.0.1 -uroot -p654321 --default-character-set=utf8 test t_table1 t_table2 t_table3 --no-create-info --result-file="c:/bak.sql"

說明: --no-create-info 不要增加建立表語句, 這樣可以防止原有資料的丟失, 可以把備份的資料追加至資料庫中.

如果要覆蓋原有資料, 加上--replace參數

4. 資料庫恢複

mysql -127.0.0.1 -uroot -p654321 --comment < c:/bak.sql

一般恢複, 都沒加上--comment 參數, 這樣會丟失預存程序中的注釋, 建議加上.

mysql -127.0.0.1 -uroot -p654321 --comment test < c:/bak.sql

對於差異備份的恢複, 後面需指定資料庫名稱.

相關文章

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.