一键安装mysql-5.X二进制包的脚本
Posted 凭栏听枫
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一键安装mysql-5.X二进制包的脚本相关的知识,希望对你有一定的参考价值。
1.1离线安装mysql-5.X二进制包的脚本
[root@centos8 ~]#vim install_mysql5.6.sh
#!/bin/bash
#
#************************************************
#Author: zhang
#QQ: 980554638
#Data: 2021-05-17
#FileName: mysql-install.sh
#URL: www.zhanglongjie.cn
#Description: The test script
#Copyright (C): 2021 All rights reserved
#***********************************************
DIR=`pwd`
NAME="mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz"
MDIR="/usr/local/src/mysql-5.6.51-linux-glibc2.12-x86_64"
IN_FILE=${DIR}/${NAME}
yum install -y libaio perl-Data-Dumper libncurses*
cat > /etc/my.cnf <<EOF
[mysqld]
socket=/data/mysql.sock
user=mysql
symbolic-links=0
datadir=/data/mysql
innodb_file_per_table=1
[client]
port=3306
socket=/data/mysql.sock
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/tmp/mysql.sock
EOF
if [ -f ${IN_FILE} ];then
echo "安装文件存在"
else
echo "安装文件不存在"
exit 0
fi
if [-h /usr/local/mysql ];then
echo "Mysql已安装"
exit 0
else
tar xvf ${IN_FILE} -C /usr/local/src
ln -sv $MDIR /usr/local/mysql
if id mysql;then
echo "mysql 用户已经存在,跳过创建"
else
groupadd -r -g 306 mysql
useradd -r -g 306 -u 306 -s /sbin/nologin mysql
fi
if id mysql;then
chown -R mysql.mysql /usr/local/mysql/*
if [ ! -d /data/mysql ];then
mkdir -pv /data/mysql && chown -R mysql.mysql /data -R
/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql
cp -p /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod a+x /etc/init.d/mysqld
echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
ln -sv $MDIR/bin/mysql /usr/bin/mysql
chkconfig --add mysqld
/etc/init.d/mysqld start
else
echo "Mysql数据目录已存在"
exit 0
fi
fi
fi
[root@centos8 ~]#ls
mysql-install.sh mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
1.2 在线安装mysql-5.X二进制包的脚本
#!/bin/bash
#
#************************************************
#Author: zhang
#QQ: 980554638
#Data: 2021-05-18
#FileName: install_online_mysql.sh
#URL: www.zhanglongjie.cn
#Description: The test script
#Copyright (C): 2021 All rights reserved
#***********************************************
. /etc/init.d/functions
DIR=`pwd`
MYSQL_VERSION=5.6.51
NAME="mysql-${MYSQL_VERSION}-linux-glibc2.12-x86_64.tar.gz"
FULL_NAME=${DIR}/${NAME}
URL=http://mirrors.163.com/mysql/Downloads/MySQL-5.6
DATA_DIR="/data/mysql"
yum install -y libaio perl-Data-Dumper autoconf libncurses* wget
wget $URL/$NAME || { action "下载失败,异常退出" false;exit 10; }
cat > /etc/my.cnf <<EOF
[mysqld]
socket=/data/mysql.sock
user=mysql
symbolic-links=0
datadir=/data/mysql
innodb_file_per_table=1
[client]
port=3306
socket=/data/mysql.sock
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/tmp/mysql.sock
EOF
if [ -f ${FULL_NAME} ];then
action "安装文件存在"
else
action "安装文件不存在"false
exit 3
fi
if [ -e /usr/local/mysql ];then
action "MySQL 已经安装"false
exit 3
else
tar xf ${FULL_NAME} -C /usr/local/src
ln -sv /usr/local/src/mysql-${MYSQL_VERSION}-linux-glibc2.12-x86_64 /usr/local/mysql
if id mysql;then
action "mysql 用户已经存在,跳过创建过程"
else
groupadd -r -g 306 mysql
useradd -r -g 306 -u 306 -s /sbin/nologin mysql
fi
if id mysql;then
chown -R mysql.mysql /usr/local/mysql/*
if [ ! -d /data/mysql ];then
mkdir -pv /data/mysql && chown -R mysql.mysql /data -R
/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql
cp -p /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod a+x /etc/init.d/mysqld
echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
ln -sv /usr/local/src/mysql-${MYSQL_VERSION}-linux-glibc2.12-x86_64/bin/mysql /usr/bin/mysql
chkconfig --add mysqld
/etc/init.d/mysqld start
else
action "MySQL数据目录已经存在" false
exit 3
fi
fi
fi
以上是关于一键安装mysql-5.X二进制包的脚本的主要内容,如果未能解决你的问题,请参考以下文章