Warning: include(/home/blog66rr/public_html/wp-content/plugins/hyper-cache/cache.php): failed to open stream: No such file or directory in /home/blog66rr/public_html/wp-content/advanced-cache.php on line 24

Warning: include(/home/blog66rr/public_html/wp-content/plugins/hyper-cache/cache.php): failed to open stream: No such file or directory in /home/blog66rr/public_html/wp-content/advanced-cache.php on line 24

Warning: include(): Failed opening '/home/blog66rr/public_html/wp-content/plugins/hyper-cache/cache.php' for inclusion (include_path='.:/opt/cpanel/ea-php70/root/usr/share/pear') in /home/blog66rr/public_html/wp-content/advanced-cache.php on line 24
mysql – 第 2 頁 – unethost無限空間虛擬主機 技術分享部落格

如何設定mysql的自動備分

cpanel有一個功能是mysql備分,這個功能有一個不足的地方是,沒有辦法設定成自動化的。然而,只要透過ftp上傳一個shell script,再加上crontab的設定,還是可以完成類似的事。

下方是shell script的內容,將這個shell script用文字編輯器,編輯好之後,存檔成為backup.sh。並且在透過ftp上傳到使用者的家目錄下之後,加上可執行的權限。

#!/bin/sh
receiver=此處填要用來收backup的email
mysql_username=此處填mysql的使用者名稱
mysql_dbname=此處填mysql的資料庫名稱
mysql_userpwd=此處填mysql的密碼
file=./mysqldb_`date ‘+%m-%d-%Y’`.sql.gz
name=mysqldb_`date ‘+%m-%d-%Y’`.sql.gz
mysqldump -u $mysql_username -p$mysql_userpwd -h localhost –routines  –single-transaction  –skip-add-locks –skip-lock-tables –default-character-set=utf8 $mysql_dbname | gzip > $file
uuencode $file $name  | mail -s “Database backup” $receiver
rm $file

 

接著就要來設定cron job,cron job可以透過cpanel的GUI來設定。下方的例子,就是每天的0時0分要執行一次這個backup.sh。

0 0 * * * /home/使用者名稱/backup.sh>/dev/null 2>&1

透過command來搬mysql資料庫

獨立主機的客戶,從別家搬過來,mysql的資料庫總是搬不好。透過phpmyadmin也是搞不定。其實phpmyadmin會有error也是很正常的事。phpmyadmin是跑在http protocol之上的。http 的post如果有時間限制或是php的執行時間有限制,就很容易timout。

沒有辦法,只好直接下command來搬。

<<匯出>>
mysqldump -u “your_username” -p –lock-tables –databases DB1 [DB2 DB3…] > your_database_dump.sql

<<匯入>>
mysql -u “your username” -p “your_database” < database_dump.sql

如果匯出的時候,有using –all-databases 選項,可以使用:
mysql -u “your_username” -p < your_database_dump.sql   來匯入,比較簡單。