upgrade_db.sh 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+
  6. #
  7. # Project home page:
  8. # http://oneinstack.com
  9. # https://github.com/lj2007331/oneinstack
  10. Upgrade_DB() {
  11. cd $oneinstack_dir/src
  12. [ ! -e "$db_install_dir/bin/mysql" ] && echo "${CWARNING}MySQL/MariaDB/Percona is not installed on your system! ${CEND}" && exit 1
  13. OLD_DB_version_tmp=`$db_install_dir/bin/mysql -V | awk '{print $5}' | awk -F, '{print $1}'`
  14. DB_tmp=`echo $OLD_DB_version_tmp | awk -F'-' '{print $2}'`
  15. if [ "$DB_tmp" == 'MariaDB' ];then
  16. [ "$IPADDR_STATE"x == "CN"x ] && DOWN_ADDR=http://mirrors.aliyun.com/mariadb || DOWN_ADDR=https://downloads.mariadb.org/f
  17. LIBC_VERSION=`getconf -a | grep GNU_LIBC_VERSION | awk '{print $NF}'`
  18. LIBC_YN=`echo "$LIBC_VERSION < 2.14" | bc`
  19. [ $LIBC_YN == '1' ] && GLIBC_FLAG=linux || GLIBC_FLAG=linux-glibc_214
  20. DB=MariaDB
  21. OLD_DB_version=`echo $OLD_DB_version_tmp | awk -F'-' '{print $1}'`
  22. elif [ -n "$DB_tmp" -a "$DB_tmp" != 'MariaDB' ];then
  23. DB=Percona
  24. OLD_DB_version=$OLD_DB_version_tmp
  25. else
  26. [ "$IPADDR_STATE"x == "CN"x ] && DOWN_ADDR=http://mirrors.sohu.com/mysql || DOWN_ADDR=http://cdn.mysql.com/Downloads
  27. DB=MySQL
  28. OLD_DB_version=$OLD_DB_version_tmp
  29. fi
  30. #backup
  31. while :; do
  32. $db_install_dir/bin/mysql -uroot -p${dbrootpwd} -e "quit" >/dev/null 2>&1
  33. if [ $? -eq 0 ];then
  34. break
  35. else
  36. echo
  37. read -p "Please input the root password of database: " NEW_dbrootpwd
  38. $db_install_dir/bin/mysql -uroot -p${NEW_dbrootpwd} -e "quit" >/dev/null 2>&1
  39. if [ $? -eq 0 ];then
  40. dbrootpwd=$NEW_dbrootpwd
  41. sed -i "s+^dbrootpwd.*+dbrootpwd='$dbrootpwd'+" ../options.conf
  42. break
  43. else
  44. echo "${CFAILURE}$DB root password incorrect,Please enter again! ${CEND}"
  45. fi
  46. fi
  47. done
  48. echo
  49. echo "${CSUCCESS}Starting $DB backup${CEND}......"
  50. $db_install_dir/bin/mysqldump -uroot -p${dbrootpwd} --opt --all-databases > DB_all_backup_$(date +"%Y%m%d").sql
  51. [ -f "DB_all_backup_$(date +"%Y%m%d").sql" ] && echo "$DB backup success, Backup file: ${MSG}`pwd`/DB_all_backup_$(date +"%Y%m%d").sql${CEND}"
  52. #upgrade
  53. echo
  54. echo "Current $DB Version: ${CMSG}$OLD_DB_version${CEND}"
  55. [ -e /usr/local/lib/libjemalloc.so ] && { je_tc_malloc=1; EXE_LINKER="-DCMAKE_EXE_LINKER_FLAGS='-ljemalloc'"; }
  56. [ -e /usr/local/lib/libtcmalloc.so ] && { je_tc_malloc=2; EXE_LINKER="-DCMAKE_EXE_LINKER_FLAGS='-ltcmalloc'"; }
  57. [ -e /usr/local/lib/libjemalloc.so -a -e /usr/local/lib/libtcmalloc.so ] && { je_tc_malloc=1; EXE_LINKER="-DCMAKE_EXE_LINKER_FLAGS='-ljemalloc'"; }
  58. while :; do echo
  59. read -p "Please input upgrade $DB Version(example: $OLD_DB_version): " NEW_DB_version
  60. if [ `echo $NEW_DB_version | awk -F. '{print $1"."$2}'` == `echo $OLD_DB_version | awk -F. '{print $1"."$2}'` ]; then
  61. if [ "$DB" == 'MariaDB' ];then
  62. DB_name=mariadb-${NEW_DB_version}-${GLIBC_FLAG}-${SYS_BIT_b}
  63. DB_URL=$DOWN_ADDR/mariadb-${NEW_DB_version}/bintar-${GLIBC_FLAG}-$SYS_BIT_a/$DB_name.tar.gz
  64. elif [ "$DB" == 'Percona' ];then
  65. DB_name=percona-server-$NEW_DB_version
  66. DB_URL=http://www.percona.com/redir/downloads/Percona-Server-`echo $NEW_DB_version | awk -F. '{print $1"."$2}'`/LATEST/source/tarball/$DB_name.tar.gz
  67. elif [ "$DB" == 'MySQL' ];then
  68. [ `echo $NEW_DB_version | awk -F. '{print $1"."$2}'` != '5.5' ] && DB_name=mysql-${NEW_DB_version}-linux-glibc2.5-${SYS_BIT_b} || DB_name=mysql-${NEW_DB_version}-linux2.6-${SYS_BIT_b}
  69. DB_URL=$DOWN_ADDR/MySQL-`echo $NEW_DB_version | awk -F. '{print $1"."$2}'`/$DB_name.tar.gz
  70. fi
  71. [ ! -e "$DB_name.tar.gz" ] && wget --no-check-certificate -c $DB_URL > /dev/null 2>&1
  72. if [ -e "$DB_name.tar.gz" ];then
  73. echo "Download [${CMSG}$DB_name.tar.gz${CEND}] successfully! "
  74. else
  75. echo "${CWARNING}$DB version does not exist! ${CEND}"
  76. fi
  77. break
  78. else
  79. echo "${CWARNING}input error! ${CEND}Please only input '${CMSG}${OLD_DB_version%.*}.xx${CEND}'"
  80. fi
  81. done
  82. if [ -e "$DB_name.tar.gz" ];then
  83. echo "[${CMSG}$DB_name.tar.gz${CEND}] found"
  84. echo "Press Ctrl+c to cancel or Press any key to continue..."
  85. char=`get_char`
  86. if [ "$DB" == 'MariaDB' ];then
  87. service mysqld stop
  88. mv ${mariadb_install_dir}{,_old_`date +"%Y%m%d_%H%M%S"`}
  89. mv ${mariadb_data_dir}{,_old_`date +"%Y%m%d_%H%M%S"`}
  90. mkdir -p $mariadb_data_dir;chown mysql.mysql -R $mariadb_data_dir
  91. tar xzf $DB_name.tar.gz
  92. [ ! -d "$mariadb_install_dir" ] && mkdir -p $mariadb_install_dir
  93. mv mariadb-${NEW_DB_version}-*-${SYS_BIT_b}/* $mariadb_install_dir
  94. if [ "$je_tc_malloc" == '1' -a "`echo $OLD_DB_version_tmp | awk -F'.' '{print $1"."$2}'`" != '10.1' ];then
  95. sed -i 's@executing mysqld_safe@executing mysqld_safe\nexport LD_PRELOAD=/usr/local/lib/libjemalloc.so@' $mariadb_install_dir/bin/mysqld_safe
  96. elif [ "$je_tc_malloc" == '2' -a "`echo $OLD_DB_version_tmp | awk -F'.' '{print $1"."$2}'`" != '10.1' ];then
  97. sed -i 's@executing mysqld_safe@executing mysqld_safe\nexport LD_PRELOAD=/usr/local/lib/libtcmalloc.so@' $mariadb_install_dir/bin/mysqld_safe
  98. fi
  99. $mariadb_install_dir/scripts/mysql_install_db --user=mysql --basedir=$mariadb_install_dir --datadir=$mariadb_data_dir
  100. chown mysql.mysql -R $mariadb_data_dir
  101. service mysqld start
  102. $mariadb_install_dir/bin/mysql < DB_all_backup_$(date +"%Y%m%d").sql
  103. service mysqld restart
  104. $mariadb_install_dir/bin/mysql -uroot -p${dbrootpwd} -e "drop database test;" >/dev/null 2>&1
  105. $mariadb_install_dir/bin/mysql -uroot -p${dbrootpwd} -e "reset master;" >/dev/null 2>&1
  106. [ $? -eq 0 ] && echo "You have ${CMSG}successfully${CEND} upgrade from ${CMSG}$OLD_DB_version${CEND} to ${CMSG}$NEW_DB_version${CEND}"
  107. elif [ "$DB" == 'Percona' ];then
  108. tar zxf $DB_name.tar.gz
  109. cd $DB_name
  110. make clean
  111. if [ "`echo $NEW_DB_version | awk -F. '{print $1"."$2}'`" == '5.5' ];then
  112. cmake . -DCMAKE_INSTALL_PREFIX=$percona_install_dir \
  113. -DMYSQL_DATADIR=$percona_data_dir \
  114. -DSYSCONFDIR=/etc \
  115. -DWITH_INNOBASE_STORAGE_ENGINE=1 \
  116. -DWITH_PARTITION_STORAGE_ENGINE=1 \
  117. -DWITH_FEDERATED_STORAGE_ENGINE=1 \
  118. -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
  119. -DWITH_MYISAM_STORAGE_ENGINE=1 \
  120. -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
  121. -DWITH_READLINE=1 \
  122. -DENABLE_DTRACE=0 \
  123. -DENABLED_LOCAL_INFILE=1 \
  124. -DDEFAULT_CHARSET=utf8mb4 \
  125. -DDEFAULT_COLLATION=utf8mb4_general_ci \
  126. $EXE_LINKER
  127. else
  128. cmake . -DCMAKE_INSTALL_PREFIX=$percona_install_dir \
  129. -DMYSQL_DATADIR=$percona_data_dir \
  130. -DSYSCONFDIR=/etc \
  131. -DWITH_INNOBASE_STORAGE_ENGINE=1 \
  132. -DWITH_PARTITION_STORAGE_ENGINE=1 \
  133. -DWITH_FEDERATED_STORAGE_ENGINE=1 \
  134. -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
  135. -DWITH_MYISAM_STORAGE_ENGINE=1 \
  136. -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
  137. -DENABLED_LOCAL_INFILE=1 \
  138. -DENABLE_DTRACE=0 \
  139. -DDEFAULT_CHARSET=utf8mb4 \
  140. -DDEFAULT_COLLATION=utf8mb4_general_ci \
  141. $EXE_LINKER
  142. fi
  143. make -j ${THREAD}
  144. service mysqld stop
  145. mv ${percona_install_dir}{,_old_`date +"%Y%m%d_%H%M%S"`}
  146. mv ${percona_data_dir}{,_old_`date +"%Y%m%d_%H%M%S"`}
  147. [ ! -d "$percona_install_dir" ] && mkdir -p $percona_install_dir
  148. mkdir -p $percona_data_dir;chown mysql.mysql -R $percona_data_dir
  149. make install
  150. cd ..
  151. if [ "`echo $NEW_DB_version | awk -F. '{print $1"."$2}'`" == '5.7' ];then
  152. $percona_install_dir/bin/mysqld --initialize-insecure --user=mysql --basedir=$percona_install_dir --datadir=$percona_data_dir
  153. else
  154. $percona_install_dir/scripts/mysql_install_db --user=mysql --basedir=$percona_install_dir --datadir=$percona_data_dir
  155. fi
  156. chown mysql.mysql -R $percona_data_dir
  157. service mysqld start
  158. $percona_install_dir/bin/mysql < DB_all_backup_$(date +"%Y%m%d").sql
  159. service mysqld restart
  160. $percona_install_dir/bin/mysql -uroot -p${dbrootpwd} -e "drop database test;" >/dev/null 2>&1
  161. $percona_install_dir/bin/mysql -uroot -p${dbrootpwd} -e "reset master;" >/dev/null 2>&1
  162. [ $? -eq 0 ] && echo "You have ${CMSG}successfully${CEND} upgrade from ${CMSG}$OLD_DB_version${CEND} to ${CMSG}$NEW_DB_version${CEND}"
  163. elif [ "$DB" == 'MySQL' ];then
  164. tar zxf $DB_name.tar.gz
  165. service mysqld stop
  166. mv ${mysql_install_dir}{,_old_`date +"%Y%m%d_%H%M%S"`}
  167. mv ${mysql_data_dir}{,_old_`date +"%Y%m%d_%H%M%S"`}
  168. [ ! -d "$mysql_install_dir" ] && mkdir -p $mysql_install_dir
  169. mkdir -p $mysql_data_dir;chown mysql.mysql -R $mysql_data_dir
  170. mv $DB_name/* $mysql_install_dir/
  171. [ "$je_tc_malloc" == '1' ] && sed -i 's@executing mysqld_safe@executing mysqld_safe\nexport LD_PRELOAD=/usr/local/lib/libjemalloc.so@' $mysql_install_dir/bin/mysqld_safe
  172. [ "$je_tc_malloc" == '2' ] && sed -i 's@executing mysqld_safe@executing mysqld_safe\nexport LD_PRELOAD=/usr/local/lib/libtcmalloc.so@' $mysql_install_dir/bin/mysqld_safe
  173. [ "`echo $NEW_DB_version | awk -F. '{print $1"."$2}'`" != '5.7' ] && $mysql_install_dir/scripts/mysql_install_db --user=mysql --basedir=$mysql_install_dir --datadir=$mysql_data_dir
  174. [ "`echo $NEW_DB_version | awk -F. '{print $1"."$2}'`" == '5.7' ] && $mysql_install_dir/bin/mysqld --initialize-insecure --user=mysql --basedir=$mysql_install_dir --datadir=$mysql_data_dir
  175. chown mysql.mysql -R $mysql_data_dir
  176. [ -e "$mysql_install_dir/my.cnf" ] && rm -rf $mysql_install_dir/my.cnf
  177. service mysqld start
  178. $mysql_install_dir/bin/mysql < DB_all_backup_$(date +"%Y%m%d").sql
  179. service mysqld restart
  180. $mysql_install_dir/bin/mysql -uroot -p${dbrootpwd} -e "drop database test;" >/dev/null 2>&1
  181. $mysql_install_dir/bin/mysql -uroot -p${dbrootpwd} -e "reset master;" >/dev/null 2>&1
  182. [ $? -eq 0 ] && echo "You have ${CMSG}successfully${CEND} upgrade from ${CMSG}$OLD_DB_version${CEND} to ${CMSG}$NEW_DB_version${CEND}"
  183. fi
  184. fi
  185. }