MySQL/MariaDB/Percona database upgrade script

Source: Internet
Author: User
Tags mkdir reset versions percona

Generally, it is not recommended to upgrade the database version. This script is dedicated to version control. To prevent compatibility issues between major versions, scripts by default only support upgrades between the same major version, such as MySQL-5.6.25 upgrade to MySQL-5.6.26, MySQL-5.5.44 upgrade to MySQL-5.5.45, MariaDB-10.0.20 upgrade to MariaDB-10.0.21; not across Branch versions and not across major versions, such as upgrading from MySQL-5.5.44 to PerconaL-5.5.44-37.3, MySQL-5.5.44 to MySQL-5.6.25.

#! /Bin/bash

Upgrade_DB ()
{
Cd $ oneinstack_dir/src
[! -E "$ db_install_dir/bin/mysql"] & echo-e "\ 033 [31 mThe MySQL/MariaDB/Percona is not installed on your system! \ 033 [0 m "& exit 1
DB_version_tmp = '$ db_install_dir/bin/mysql-V | awk' {print $5} '| awk-F,' {print $1 }''
DB_tmp = 'echo $ DB_version_tmp | awk-F'-''{print $2 }''
If ["$ DB_tmp" = 'mariadb']; then
Public_IP = '../functions/get_public_ip.py'
If ["'../functions/get_ip_area.py $ public_IP'" = '\ u4e2d \ u56fd']; then
FLAG_IP = CN
Fi
["$ FLAG_IP" x = "CN" x] & DOWN_ADDR = http://mirrors.aliyun.com/mariadb | DOWN_ADDR = https://downloads.mariadb.org/f
[-D "/lib64"] & {SYS_BIT_a = x86_64; SYS_BIT_ B = x86_64 ;}||{ SYS_BIT_a = x86; SYS_BIT_ B = i686 ;}
LIBC_VERSION = 'getconf-a | grep GNU_LIBC_VERSION | awk '{print $ NF }''
LIBC_YN = 'echo "$ LIBC_VERSION <2.14" | BC'
[$ LIBC_YN = '1'] & GLIBC_FLAG = linux | GLIBC_FLAG = linux-glibc_214

DB = MariaDB
Old_DB_version = 'echo $ DB_version_tmp | awk-F'-''{print $1 }''
Elif [-n "$ DB_tmp"-a "$ DB_tmp "! = 'Mariadb']; then
DB = Percona
Old_DB_version = $ DB_version_tmp
Else
DB = MySQL
Old_DB_version = $ DB_version_tmp
Fi

# Backup
While:
Do
$ Db_install_dir/bin/mysql-uroot-p $ {dbrootpwd}-e "quit">/dev/null 2> & 1
If [$? -Eq 0]; then
Break
Else
Echo
Read-p "Please input the root password of database:" NEW_dbrootpwd
$ Db_install_dir/bin/mysql-uroot-p $ {NEW_dbrootpwd}-e "quit">/dev/null 2> & 1
If [$? -Eq 0]; then
Dbrootpwd = $ NEW_dbrootpwd
Sed-I "s + ^ dbrootpwd. * + dbrootpwd = '$ dbrootpwd' +".../options. conf
Break
Else
Echo-e "\ 033 [31 m $ DB root password incorrect, Please enter again! \ 033 [0 m"
Fi
Fi

Done
Echo
Echo-e "\ 033 [32 mStarting $ DB backup... \ 033 [0 m"
$ Db_install_dir/bin/mysqldump-uroot-p $ {dbrootpwd} -- opt -- all-databases> DB_all_backup _ $ (date + "% Y % m % d"). SQL
[-F "DB_all_backup _ $ (date +" % Y % m % d "). SQL "] & echo-e" $ DB backup success, Backup file: \ 033 [32m 'pwd'/DB_all_backup _ $ (date + "% Y % m % d "). SQL \ 033 [0 m"

# Upgrade
Echo
Echo-e "Current $ DB Version: \ 033 [32 m $ Old_DB_version \ 033 [0 m"
[-E/usr/local/lib/libtcmalloc. so] & {je_tc_malloc = 2; EXE_LINKER = "-DCMAKE_EXE_LINKER_FLAGS = '-ltcmalloc '";}
[-E/usr/local/lib/libjemalloc. so] & {je_tc_malloc = 1; EXE_LINKER = "-DCMAKE_EXE_LINKER_FLAGS = '-ljemalloc '";}

While:
Do
Echo
Read-p "Please input upgrade $ DB Version (example: 5.6.25):" DB_version
If ['echo $ DB_version | awk-F. '{print $1 ". "$2}'' = 'echo $ Old_DB_version | awk-F. '{print $1 ". "$2}'']; then
If ["$ DB" = 'mariadb']; then
DB_name = mariadb-$ {DB_version}-$ {GLIBC_FLAG}-$ {SYS_BIT_ B}
DB_URL = $ DOWN_ADDR/mariadb-$ {DB_version}/bintar-$ {GLIBC_FLAG}-$ SYS_BIT_a/mongodb_name.tar.gz
Elif ["$ DB" = 'percona ']; then
DB_name = percona-server-$ DB_version
DB_URL = http://www.percona.com/redir/downloads/Percona-Server-'echo $ DB_version | awk-F. '{print $1 "." $2}'/LATEST/source/tarball/mongodb_name.tar.gz
Elif ["$ DB" = 'mysql']; then
DB_name = mysql-$ DB_version
DB_URL = http://cdn.mysql.com/Downloads/MySQL-'echo $ DB_version | awk-F. '{print $1 "." $2}'/mongodb_name.tar.gz
Fi
[! -E "mongodb_name.tar.gz"] & wget-c $ DB_URL>/dev/null 2> & 1
       
If [-e "mongodb_name.tar.gz"]; then
Echo-e "Download \ 033[32m1_db_name.tar.gz \ 033 [0 m successfully! "
Else
Echo-e "\ 033 [31mIt does not exist! \ 033 [0 m"
Fi
Break
Else
Echo-e "\ 033 [31 minput error! \ 033 [0 m Please only input '\ 033 [32 m $ {Old_DB_version %. *}. Xx' \ 033 [0 m"
Fi
Done

If [-e "mongodb_name.tar.gz"]; then
Echo-e "\ 0331_32m1_db_name.tar.gz \ 033 [0 m [found]"
Echo "Press Ctrl + c to cancel or Press any key to continue ..."
Char = 'get _ char'
If ["$ DB" = 'mariadb']; then
Service mysqld stop
Mv $ {db_install_dir} {, _ old _ 'date + "% Y % m % d "'}
Mv $ {db_data_dir} {, _ old _ 'date + "% Y % m % d "'}
Mkdir-p $ db_data_dir; chown mysql. mysql-R $ db_data_dir
Tar xzf mongodb_name.tar.gz
[! -D "$ db_install_dir"] & mkdir-p $ db_install_dir
Mv mariadb-$ {DB_version}-linux-$ {SYS_BIT_ B}/* $ db_install_dir
If ["$ je_tc_malloc" = '1']; then
Sed-I's @ executing mysqld_safe \ nexport LD_PRELOAD =/usr/local/lib/libjemalloc. so @ '$ db_install_dir/bin/mysqld_safe
Elif ["$ je_tc_malloc" = '2']; then
Sed-I's @ executing mysqld_safe \ nexport LD_PRELOAD =/usr/local/lib/libtcmalloc. so @ '$ db_install_dir/bin/mysqld_safe
Fi
$ Db_install_dir/scripts/mysql_install_db -- user = mysql -- basedir = $ db_install_dir -- datadir = $ db_data_dir
Chown mysql. mysql-R $ db_data_dir
Service mysqld start
$ Db_install_dir/bin/mysql <DB_all_backup _ $ (date + "% Y % m % d"). SQL
Service mysqld restart
$ Db_install_dir/bin/mysql-uroot-p $ {dbrootpwd}-e "drop database test;">/dev/null 2> & 1
$ Db_install_dir/bin/mysql-uroot-p $ {dbrootpwd}-e "reset master;">/dev/null 2> & 1
[$? -Eq 0] & echo-e "You have \ 033 [32 m $ DB successfully \ 033 [0 m upgrade from \ 033 [32 m $ Old_DB_version \ 033 [0 m \ 033 [32 m $ DB_version \ 033 [0 m"
Elif ["$ DB" = 'percona ']; then
Tar zxf mongodb_name.tar.gz
Cd $ DB_name
Make clean
If ["'echo $ DB_version | awk-F. '{print $1". "$2}'' "= '5. 5']; then
Cmake.-DCMAKE_INSTALL_PREFIX = $ db_install_dir \
-DMYSQL_DATADIR = $ db_data_dir \
-DSYSCONFDIR =/etc \
-DWITH_INNOBASE_STORAGE_ENGINE = 1 \
-DWITH_PARTITION_STORAGE_ENGINE = 1 \
-DWITH_FEDERATED_STORAGE_ENGINE = 1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE = 1 \
-DWITH_MYISAM_STORAGE_ENGINE = 1 \
-DWITH_ARCHIVE_STORAGE_ENGINE = 1 \
-DWITH_READLINE = 1 \
-DENABLE_DTRACE = 0 \
-DENABLED_LOCAL_INFILE = 1 \
-DDEFAULT_CHARSET = utf8mb4 \
-DDEFAULT_COLLATION = utf8mb4_general_ci \
$ EXE_LINKER
Else
Cmake.-DCMAKE_INSTALL_PREFIX = $ db_install_dir \
-DMYSQL_DATADIR = $ db_data_dir \
-DSYSCONFDIR =/etc \
-DWITH_INNOBASE_STORAGE_ENGINE = 1 \
-DWITH_PARTITION_STORAGE_ENGINE = 1 \
-DWITH_FEDERATED_STORAGE_ENGINE = 1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE = 1 \
-DWITH_MYISAM_STORAGE_ENGINE = 1 \
-DWITH_ARCHIVE_STORAGE_ENGINE = 1 \
-DENABLED_LOCAL_INFILE = 1 \
-DENABLE_DTRACE = 0 \
-DDEFAULT_CHARSET = utf8mb4 \
-DDEFAULT_COLLATION = utf8mb4_general_ci \
$ EXE_LINKER
Fi
Make-j 'grep processor/proc/cpuinfo | wc-L'
Service mysqld stop
Mv $ {db_install_dir} {, _ old _ 'date + "% Y % m % d "'}
Mv $ {db_data_dir} {, _ old _ 'date + "% Y % m % d "'}
[! -D "$ db_install_dir"] & mkdir-p $ db_install_dir
Mkdir-p $ db_data_dir; chown mysql. mysql-R $ db_data_dir
Make install
Cd ..
$ Db_install_dir/scripts/mysql_install_db -- user = mysql -- basedir = $ db_install_dir -- datadir = $ db_data_dir
Chown mysql. mysql-R $ db_data_dir
Service mysqld start
$ Db_install_dir/bin/mysql <DB_all_backup _ $ (date + "% Y % m % d"). SQL
Service mysqld restart
$ Db_install_dir/bin/mysql-uroot-p $ {dbrootpwd}-e "drop database test;">/dev/null 2> & 1
$ Db_install_dir/bin/mysql-uroot-p $ {dbrootpwd}-e "reset master;">/dev/null 2> & 1
[$? -Eq 0] & echo-e "You have \ 033 [32 m $ DB successfully \ 033 [0 m upgrade from \ 033 [32 m $ Old_DB_version \ 033 [0 m \ 033 [32 m $ DB_version \ 033 [0 m"
Elif ["$ DB" = 'mysql']; then
Tar zxf mongodb_name.tar.gz
Cd $ DB_name
Make clean
If ["'echo $ DB_version | awk-F. '{print $1". "$2}'' "= '5. 5']; then
Cmake.-DCMAKE_INSTALL_PREFIX = $ db_install_dir \
-DMYSQL_DATADIR = $ db_data_dir \
-DSYSCONFDIR =/etc \
-DWITH_INNOBASE_STORAGE_ENGINE = 1 \
-DWITH_PARTITION_STORAGE_ENGINE = 1 \
-DWITH_FEDERATED_STORAGE_ENGINE = 1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE = 1 \
-DWITH_MYISAM_STORAGE_ENGINE = 1 \
-DWITH_ARCHIVE_STORAGE_ENGINE = 1 \
-DWITH_READLINE = 1 \
-DENABLED_LOCAL_INFILE = 1 \
-DENABLE_DTRACE = 0 \
-DDEFAULT_CHARSET = utf8mb4 \
-DDEFAULT_COLLATION = utf8mb4_general_ci \
-DWITH_EMBEDDED_SERVER = 1 \
$ EXE_LINKER
Else
Cmake.-DCMAKE_INSTALL_PREFIX = $ db_install_dir \
-DMYSQL_DATADIR = $ db_data_dir \
-DSYSCONFDIR =/etc \
-DWITH_INNOBASE_STORAGE_ENGINE = 1 \
-DWITH_PARTITION_STORAGE_ENGINE = 1 \
-DWITH_FEDERATED_STORAGE_ENGINE = 1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE = 1 \
-DWITH_MYISAM_STORAGE_ENGINE = 1 \
-DENABLED_LOCAL_INFILE = 1 \
-DENABLE_DTRACE = 0 \
-DDEFAULT_CHARSET = utf8mb4 \
-DDEFAULT_COLLATION = utf8mb4_general_ci \
-DWITH_EMBEDDED_SERVER = 1 \
$ EXE_LINKER
Fi
Make-j 'grep processor/proc/cpuinfo | wc-L'
Service mysqld stop
Mv $ {db_install_dir} {, _ old _ 'date + "% Y % m % d "'}
Mv $ {db_data_dir} {, _ old _ 'date + "% Y % m % d "'}
[! -D "$ db_install_dir"] & mkdir-p $ db_install_dir
Mkdir-p $ db_data_dir; chown mysql. mysql-R $ db_data_dir
Make install
Cd ..
$ Db_install_dir/scripts/mysql_install_db -- user = mysql -- basedir = $ db_install_dir -- datadir = $ db_data_dir
Chown mysql. mysql-R $ db_data_dir
Service mysqld start
$ Db_install_dir/bin/mysql <DB_all_backup _ $ (date + "% Y % m % d"). SQL
Service mysqld restart
$ Db_install_dir/bin/mysql-uroot-p $ {dbrootpwd}-e "drop database test;">/dev/null 2> & 1
$ Db_install_dir/bin/mysql-uroot-p $ {dbrootpwd}-e "reset master;">/dev/null 2> & 1
[$? -Eq 0] & echo-e "You have \ 033 [32 m $ DB successfully \ 033 [0 m upgrade from \ 033 [32 m $ Old_DB_version \ 033 [0 m \ 033 [32 m $ DB_version \ 033 [0 m"
Fi
Fi
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.