MySQL小版本升级

前言:由于安全扫描,漏洞问题需进行升级,实际这是个无底洞

环境

  • CentOS 7
  • MySQL 5.7.27 --> MySQL 5.7.34

采用的是 in-place 方式升级

MySQL升级

1.检查XA事务

# XA检查是否有值
mysql> XA RECOVER;
Empty set (0.00 sec)

# 若有值,则需要 COMMIT 或 ROLLBACK xid
mysql> XA COMMIT xid;
或
mysql> XA ROLLBACK xid;

2.完全关闭MySQL

mysql> SET GLOBAL innodb_fast_shutdown=0;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
[root@BDGatewayT ~]# service mysqld stop
Shutting down MySQL.... SUCCESS! 

3.升级前备份数据文件

cd /app
cp -rp mysql_data/ mysql_data_20210623

4.升级 MySQL 二进制安装或软件包

cd /usr/local
lrwxrwxrwx. 1 root root         46 Nov 12  2019 mysql -> /usr/local/mysql-5.7.27-linux-glibc2.12-x86_64  -- 原来的软链
drwxr-xr-x. 9 root mysql       129 Nov 12  2019 mysql-5.7.27-linux-glibc2.12-x86_64
drwxr-xr-x  9 root root        129 Jun 24 14:58 mysql-5.7.34-linux-glibc2.12-x86_64
-rw-r--r--  1 root root  665389778 Mar 26 14:43 mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz

-- 删除软链
rm -rf mysql
-- 重建软链
ln -s mysql-5.7.34-linux-glibc2.12-x86_64 mysql
lrwxrwxrwx  1 root root         35 Jun 24 15:16 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64   -- 新的软链,链接到了 5.7.34 版本
drwxr-xr-x. 9 root mysql       129 Nov 12  2019 mysql-5.7.27-linux-glibc2.12-x86_64
drwxr-xr-x  9 root root        129 Jun 24 14:58 mysql-5.7.34-linux-glibc2.12-x86_64
-rw-r--r--  1 root root  665389778 Mar 26 14:43 mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz

5.启动MySQL

service mysqld start

6.执行inplace升级

cd /usr/local/mysql/bin/
# mysql_upgrade -uroot -p 
Enter password: 
Checking if update is needed.
Checking server version.
Running queries to upgrade MySQL server.
Checking system database.
mysql.columns_priv                                 OK
mysql.db                                           OK
mysql.engine_cost                                  OK
mysql.event                                        OK
mysql.func                                         OK
mysql.general_log                                  OK
mysql.gtid_executed                                OK
mysql.help_category                                OK
mysql.help_keyword                                 OK
mysql.help_relation                                OK
mysql.help_topic                                   OK
mysql.innodb_index_stats                           OK
mysql.innodb_table_stats                           OK
mysql.ndb_binlog_index                             OK
mysql.plugin                                       OK
mysql.proc                                         OK
mysql.procs_priv                                   OK
mysql.proxies_priv                                 OK
mysql.server_cost                                  OK
mysql.servers                                      OK
mysql.slave_master_info                            OK
mysql.slave_relay_log_info                         OK
mysql.slave_worker_info                            OK
mysql.slow_log                                     OK
mysql.tables_priv                                  OK
mysql.time_zone                                    OK
mysql.time_zone_leap_second                        OK
mysql.time_zone_name                               OK
mysql.time_zone_transition                         OK
mysql.time_zone_transition_type                    OK
mysql.user                                         OK
Found outdated sys schema version 1.5.1.
Upgrading the sys schema.
Checking databases.
gateway.g_api_dtl                                  OK
gateway.g_api_group                                OK
gateway.g_app_authorization                        OK
gateway.g_app_dtl                                  OK
gateway.g_gateway_ins                              OK
gateway.g_gray_release                             OK
gateway.g_secret_key                               OK
gateway.g_stream_controler                         OK
gateway.ms_account                                 OK
gateway.ms_account_role_relation                   OK
gateway.ms_resource                                OK
gateway.ms_role                                    OK
gateway.ms_role_resource_relation                  OK
gateway.ms_route                                   OK
gateway.ms_variable                                OK
sys.sys_config                                     OK
Upgrade process completed successfully.
Checking if update is needed.

7.关闭并重新启动

关闭并重新启动 MySQL 服务器以确保对系统表所做的任何更改生效

# service mysqld restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL.. SUCCESS! 

一键升级脚本

需要修改配置文件中对应的信息,且脚本放在同一目录下

[root@initnode101 ~]# clear
[root@initnode101 ~]# cat upgrading_mysql_config.txt 
# MySQL 所在的bin路径
mysql_bin_path=/usr/local/mysql/bin

# MySQL的root的密码
mysql_user_root=root
mysql_root_password=123456

# 新版本的二进制文件路径
mysql_new_version=/tmp/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz

# MySQL安装路径
mysql_base_local=/usr/local/
[root@initnode101 ~]# cat upgrading_mysql.sh 
:< /tmp/up.password << EOF
[client]
user=$mysql_user_root
password=$mysql_root_password
EOF
}

del_file(){
 if [ -f /tmp/up.password ];then
   rm -rf /tmp/up.password
 fi
}

# 配置免密
mysql_login

# ---------- 查看XA事务 --------------------
## 查看XA事务
echo "查看XA事务..."
xa_count=`"$mysql_bin_path"/mysql --defaults-extra-file=/tmp/up.password -e"XA RECOVER"|grep -v "[Warning] Using a password"|wc -l`
if [[ "$xa_count" -gt 0 ]];then
  echo "[Error] 存在未关闭XA事务,请确认!"
  del_file
  exit 1
fi

# ---------- 设置 innodb_fast_shutdown 参数 --------------------
echo "设置 innodb_fast_shutdown = 1 参数"
## 使用慢速关机,InnoDB在关机前执行完全清除和更改缓冲区合并,确保在版本之间的文件格式差异的情况下充分准备数据文件。
"$mysql_bin_path"/mysql --defaults-extra-file=/tmp/up.password -e"set global innodb_fast_shutdown = 0"

# 获取数据文件路径
base_dir=`"$mysql_bin_path"/mysql --defaults-extra-file=/tmp/up.password -e"show variables like 'datadir'"|grep datadir|awk -F ' ' '{print $2}'`
if [ $base_dir = '' ];then
  echo "[Error] 数据文件路径有误!mysql 参数 basedir 没有获得对应的值。"
  del_file
  exit 1
fi

if [ ! -d "$base_dir" ];then
  echo "[Error] MySQL 数据文件路径[$base_dir]不存在,请确认!"
  del_file
  exit 1
fi

# --------------- 关闭MySQL ------------------------------------
echo "关闭MySQL..."
"$mysql_bin_path"/mysqladmin --defaults-extra-file=/tmp/up.password shutdown

# ------------  解压新版的MySQL到指定路径  -----------------------------
## 获取解压后的文件名称
tar_name=`echo ${mysql_new_version/\/tmp\//''}`
untar_name=`echo ${tar_name/".tar.gz"/''}`
echo $untar_name
echo $mysql_base_local$untar_name

# 解压新版本MySQL
if [ ! -d "$mysql_base_local$untar_name" ];then
  echo "解压MySQL新版本文件..."
  tar zxvf "$mysql_new_version" -C "$mysql_base_local"
else
  echo "[Warning] 已存在被解压文件..."
fi

# ----------- 升级前备份下原数据文件 ----------------------
## 是否进行备份原数据文件[拷贝原data文件为bak] 
while true
do
echo "是否需要将原数据文件 $base_dir 进行备份,备份前请先确认磁盘容量是否足够。请输入 [Y/N]"
read chooise
case $chooise in
Y | y)
echo "开始备份,请等待..."
cp -rp $base_dir ${base_dir%/*}.bak.`date '+%Y%m%d%H%M%S'`
echo "备份完成,备份文件名为 ${base_dir%/*}.bak.日期"
break
;;
N | n)
echo "跳过备份操作..."
break
;;
*)
echo "请输入正确的值!"
;;
esac
done

# ----------- 替换软链为新版的MySQL -----------------------
## 删除旧软链
cd $mysql_base_local
if [ -L mysql ];then
  echo "删除旧软链..."
  rm -rf mysql
else
  echo "[Error] 不存在软链 mysql"
  del_file
  exit 1
fi

## 重建软链
echo "重建软链到新版本..."
ln -s $untar_name mysql

# 启动数据库
echo "启动数据库..."
service mysqld start

echo "进行inplace升级..."
"$mysql_bin_path"/mysql_upgrade --defaults-extra-file=/tmp/up.password

echo "升级完成需要再次重启下..."
service mysqld restart

# 清楚免密配置文件
del_file

你可能感兴趣的:(MySQL小版本升级)