共計(jì) 4702 個(gè)字符,預(yù)計(jì)需要花費(fèi) 12 分鐘才能閱讀完成。
本篇內(nèi)容介紹了“怎么遷移 mysql 數(shù)據(jù)庫中的表”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓丸趣 TV 小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
作為寫腳本的,這次的重點(diǎn)在于實(shí)現(xiàn)了類似于其他語言的 logging 模塊的輸出功能。感覺還是蠻有用的,簡單直觀。
輸出 log 如下所示:
2017-03-31 16:26:57 --- INFO --- You choose the name of the table below:
2017-03-31 16:26:57 --- INFO --- 2016_06_24_record_base_log
2017-03-31 16:26:57 --- INFO --- 2016_06_16_record_base_log
2017-03-31 16:26:57 --- INFO --- table insert statements 2016_06_24_record_base_log backuping
2017-03-31 16:26:57 --- INFO --- table struct 2016_06_24_record_base_log backuping
2017-03-31 16:26:57 --- INFO --- table insert statements 2016_06_16_record_base_log backuping
2017-03-31 16:26:57 --- INFO --- table struct 2016_06_16_record_base_log backuping
2017-03-31 16:26:57 --- INFO --- append the ENGINE=InnoDB -- DATA DIRECTORY\=\ \/data2\/db\/mysql\ / to struct/2016_06_24_record_base_log.sql
2017-03-31 16:26:57 --- INFO --- append the ENGINE=InnoDB -- DATA DIRECTORY\=\ \/data2\/db\/mysql\ / to struct/2016_06_16_record_base_log.sql
2017-03-31 16:26:57 --- INFO --- There is no back up the table
2017-03-31 16:26:57 --- INFO --- The import table structure
2017-03-31 16:26:57 --- INFO --- Insert data to the table
2017-03-31 16:26:57 --- INFO --- There is no back up the table
2017-03-31 16:26:57 --- INFO --- The import table structure
2017-03-31 16:26:57 --- INFO --- Insert data to the table
2017-03-31 16:26:57 --- INFO --- Successfully completed the operation !
shell 腳本如下所示, 此腳本在 centos 系統(tǒng)測試通過:
#!/bin/bash
mysqluser= root
mysqlpass= dbpassword
mysqlhost= 127.0.0.1
mysqldb= dbname
mysqlpath= /usr/local/mysql/bin
mysqlport=3306
datetimes=`date +%Y-%m-%d %H:%M:%S `
datetimes2=`date +%Y%m%d%H%M `
datetimes3=`date +%Y%m%d%H%M%S `
[ ! -d $backupdir ] mkdir -p $backupdir
[ ! -d $structdir ] mkdir $structdir
[ ! -d logs ] mkdir logs
function logging { if [ ! -z $1 ] [ ! -z $2 ];then
echo -e ${green} ${1} --- ${2} ${reset}
echo -e ${datetimes} --- ${1} --- ${2} $logfile
fi
else
logging ERROR ${reset}${red} $1 ,have an error occurred!
exit 1
fi
function yesorno { echo -e ${yellow} $1 ${reset}
read var
case $var in
[yY][eE][sS] )
echo Your input is YES,Program to continue ;;
[nN][oO] )
echo Your input is no.
exit 0;;
**)
echo -e ${red} Input Error! ${reset}
exit 0
;;
esac
echo -e ${yellow} This script is used to mysql table DATA DIRECTORY and INDEX DIRECTORY set to/data2/db/mysql and the migration of data to /data2/db/mysql directory. ${reset}
yesorno Do you want to continue, yes or no?
logging INFO You choose the name of the table below:
# Confirm the table to by update
for tab in `cat $tablist`;do
logging INFO ${tab}
yesorno The above is the table you choose, you want to continue? Yes or no?
# dump table data and struct to $backupdir and $structdir
for tab in `cat $tablist`;do
$mysqlpath/mysqldump -u$mysqluser -p$mysqlpass -h$mysqlhost -P$mysqlport --no-create-info $mysqldb $tab ${backupdir}/${tab}.sql
error table insert statements $tab backuping
$mysqlpath/mysqldump -u$mysqluser -p$mysqlpass -h$mysqlhost -P$mysqlport --no-data $mysqldb $tab ${structdir}/${tab}.sql
error table struct $tab backuping
# append the DATA DIRECTOY and DATA DIRINDEX to table struct.
for tab in `cat $tablist`;do
if grep ENGINE=InnoDB ${structdir}/${tab}.sql;then
sed -i s/ENGINE=InnoDB/ DATA DIRECTORY\=\ \/data2\/db\/mysql\ INDEX DIRECTORY\=\ \/data2\/db\/mysql\ / ${structdir}/${tab}.sql
error append the ENGINE=InnoDB -- DATA DIRECTORY\=\ \/data2\/db\/mysql\ / to ${structdir}/${tab}.sql
elif grep ENGINE=MyISAM ${structdir}/${tab}.sql;then
sed -i s/ENGINE=MyISAM/ DATA DIRECTORY\=\ \/data2\/db\/mysql\ INDEX DIRECTORY\=\ \/data2\/db\/mysql\ / ${structdir}/${tab}.sql
error append the ENGINE=InnoDB -- DATA DIRECTORY\=\ \/data2\/db\/mysql\ / to ${structdir}/${tab}.sql
else
logging ERROR,Table structure is not found in the match engine .
exit 1
# drop old database
for tab in `cat $tablist`;do
if [ -f ${backupdir}/${tab}.sql ] [ -f ${structdir}/${tab}.sql ];then
$mysqlpath/mysql -u$mysqluser -p$mysqlpass -h$mysqlhost -P$mysqlport $mysqldb -e drop table ${tab};
error There is no back up the table
# import table struct to db
if [ -f ${structdir}/${tab}.sql ];then
$mysqlpath/mysql -u$mysqluser -p$mysqlpass -h$mysqlhost -P$mysqlport $mysqldb ${structdir}/${tab}.sql
error The import table structure
# import table data to db
if [ -f ${backupdir}/${tab}.sql ];then
$mysqlpath/mysql -u$mysqluser -p$mysqlpass -h$mysqlhost -P$mysqlport $mysqldb ${backupdir}/${tab}.sql
error Insert data to the table
logging INFO Successfully completed the operation !
ltab.txt 中存儲你要想遷移的表名,如下所示:
[root@SERVER_DB] cat ltab.txt
2016_06_24_record_base_log
2016_06_16_record_base_log
最后,記得在 screen 下執(zhí)行腳本,即使終端斷開,也不用擔(dān)心,腳本終止執(zhí)行。如果遇到錯(cuò)誤,腳本會立即終止執(zhí)行,此時(shí)需要手動(dòng)來處理。
“怎么遷移 mysql 數(shù)據(jù)庫中的表”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注丸趣 TV 網(wǎng)站,丸趣 TV 小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
正文完
發(fā)表至: 數(shù)據(jù)庫
2023-08-01