mysql☞离线迁移脚本

阅读量: zyh 2020-08-29 15:53:04
Categories: > Tags:
databases=""


mysqltonew(){
    olddatabasehost=
    olddatabaseport=
    olddatabase=$1
    oldUserName=
    oldpassword=

    newdatabasehost=
    newolddatabaseport=
    newdatabase=${olddatabase}
    newUserName=
    newpassword=


    # utf8 / utf8mb4
    read -p "character [utf8/utf8mb4/latin1]:" Character


    # 导出老库
    mysqldump -u${oldUserName} -p${oldpassword} -h${olddatabasehost} -P${olddatabaseport} --default-character-set=${Character} --single-transaction ${olddatabase} > ${olddatabase}.sql

    # 创建新库
    mysql -u${newUserName} -p${newpassword} -h${newdatabasehost} -P${newolddatabaseport} -e "CREATE DATABASE ${newdatabase} DEFAULT CHARSET ${Character} COLLATE ${Character}_general_ci;"

    # 导入新库
    mysql -u${newUserName} -p${newpassword} -h${newdatabasehost} -P${newolddatabaseport} --default-character-set=${Character} ${newdatabase} < ${olddatabase}.sql
}

for i in ${databases};do
        echo "start $i"
        mysqltonew $i
done