hello world

MySQL5.6.x二进制包升级到5.7.x

#!/bin/bash

#mysql数据库用户名
myuser="root"
#mysql数据库密码
mypwd="123456"
#数据库备份路径
dbbakdir="/data/dbbak"
#数据库目录
datadir="/data/mysql"
#系统mysql用户
sysmysqluser="mysql"
#系统mysql组
sysmysqlgroup="mysql"
#mysql5.7安装文件路径
mysqltarpath="/usr/local/src/mysql-5.7.40-linux-glibc2.12-x86_64.tar.gz"

function version_gt() { test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" != "$1"; }
echo ""
echo "MySQL即将升级,请确认参数"
echo ""
echo "#################################################################################################"
echo "MySQL用户名:$myuser"
echo "MySQL密码:$mypwd"
echo "MySQL备份路径:$dbbakdir"
echo "MySQL数据目录:$datadir"
echo "MySQL系统用户:$sysmysqluser"
echo "MySQL系统组:$sysmysqlgroup"
echo "升级文件路径:$mysqltarpath"
echo "#################################################################################################"
echo ""

if [ ! -e $mysqltarpath ]; then
    echo "${mysqltarpath}升级文件不存在,升级终止"
    exit  6
fi

mysqlver=$(mysql -V | awk '{print $5}')


#echo $mysqlver

if version_gt "$mysqlver" "5.7.40"; then
   echo "当前运行版本为$mysqlver,高于要升级的版本."
   exit 1
fi

echo "开始备份数据库"

if [ ! -d $dbbakdir ]; then
    mkdir -p $dbbakdir
fi

resault=$(mysqldump -R -E -q --lock-all-tables --all-databases -u$myuser -p$mypwd >/$dbbakdir/all_databases.sql)

if [ ! -z $resault ]; then
    echo "备份数据库失败,升级终止"
    echo $resault
    exit 2
fi

echo "备份数据库成功,备份文件路径为: ${dbbakdir}/all_databases.sql"

echo "开始备份配置文件"

cp /etc/my.cnf ${dbbakdir}/my.cnf.bk

#判断配置文件中server-id是否为空如果是空的话需要注释掉,否则在初始化数据库的时候会失败

svrid=$(grep  server-id /etc/my.cnf)

svridval=${svrid#*=}

if [ -z $svridval ]; then 
    svrid=$(grep -n server-id /etc/my.cnf)
    svrid=${svrid%:*}
    sed -i "${svrid}s/^/#/g" /etc/my.cnf
fi


echo "即将停止MySQL服务"

/etc/init.d/mysqld stop

resault=$!

if [ ! -z $resault ]; then
    echo "停止Mysql服务失败,升级终止"
    echo $resault
    exit 3
fi


if [ ! -d /usr/local/mysql ]; then
    echo "/usr/local/mysql不存在,升级终止"
    exit 4
fi

if [  -d /usr/local/mysql5.6 ]; then
   rm -rf /usr/local/mysql5.6
fi

echo "备份旧程序"

mv /usr/local/mysql /usr/local/mysql5.6



if [  ! -d /data/mysql ]; then
    echo "/data/mysql不存在,升级终止"
    exit 5
fi

if [  -d /data/mysql5.6 ]; then
   rm -rf /data/mysql5.6
fi

mkdir -p /data/mysql5.6
cp -r /data/mysql/* /data/mysql5.6 echo "解压新程序" tar xf /usr/local/src/mysql-5.7.40-linux-glibc2.12-x86_64.tar.gz -C /usr/local/src/ mv /usr/local/src/mysql-5.7.40-linux-glibc2.12-x86_64 /usr/local/mysql chown $sysmysqluser.$sysmysqlgroup /usr/local/mysql -R mkdir /data/mysql -p chown $sysmysqluser.$sysmysqlgroup /data/mysql -R /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql --user=mysql --initialize \cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld cp /usr/local/mysql5.6/lib/libmysqlclient.so.18.1.0 /usr/local/mysql/lib/ ln -s /usr/local/mysql/lib/libmysqlclient.so.18.1.0 /usr/local/mysql/lib/libmysqlclient.so.18 cp /usr/local/mysql5.6/lib/plugin/encrypt.so /usr/local/mysql/lib/plugin/ chown $sysmysqluser.$sysmysqlgroup /usr/local/mysql -R /etc/init.d/mysqld start mysql_upgrade -u$myuser -p$mypwd --force echo "重启MySQL" /etc/init.d/mysqld restart echo "升级完成"

 

posted @ 2023-07-10 18:55  我是刹那、  阅读(28)  评论(0编辑  收藏  举报