自动化安装mariadb脚本

 cat mysql.sh
#!/bin/bash
DIR=`pwd`  # 最好将下载的二进制数据库存放在/usr/local/src目录下
NAME="mariadb-10.2.29-linux-x86_64.tar.gz"  # 下载的二进制mariadb数据库
FULL_NAME=${DIR}/${NAME}
DATA_DIR="/data/mysql"  # 数据库目录
 
yum install vim gcc gcc-c++ wget autoconf  net-tools lrzsz iotop lsof iotop bash-completion -y
yum install curl policycoreutils openssh-server openssh-clients postfix -y
 
if [ -f ${FULL_NAME} ];then
    echo "安装文件存在"
else
    echo "安装文件不存在"
    exit 3
fi
if [ -h /usr/local/mysql ];then # 判断目录是否有链接
    echo "Mysql 已经安装"
    exit 3
else
    tar xvf ${FULL_NAME}   -C /usr/local/src
    ln -sv /usr/local/src/mariadb-10.2.29-linux-x86_64  /usr/local/mysql # 将数据库版本号修改
    if id  mysql;then
        echo "mysql 用户已经存在,跳过创建用户过程"
    fi
        useradd  mysql  -r -s /sbin/nologin  -d /data/mysql
    if  id  mysql;then
        chown  -R mysql.mysql  /usr/local/mysql/* -R
        if [ ! -d  /data/mysql ];then
            mkdir -pv /data/mysql && chown  -R mysql.mysql  /data  
            /usr/local/mysql/scripts/mysql_install_db  --user=mysql --datadir=/data/mysql  --basedir=/usr/local/mysql/
        cp  /usr/local/src/mariadb-10.2.29-linux-x86_64/support-files/mysql.server  /etc/init.d/mysqld   # 将数据库版本号进行修改
        chmod a+x /etc/init.d/mysqld
        cp ${DIR}/mariadb-10.2.29-linux-x86_64/support-files/my-huge.cnf   /etc/my.cnf   # 将数据库版本号进行修改
            ln -sv /usr/local/mysql/bin/mysql  /usr/bin/mysql
            sed -r -i  '/^\[mysqld\]/adatadir=/data/mysql' /etc/my.cnf   # 用sed命令在数据库配置文件中添加/data/mysql数据库存放目录,否则数据库无法启动。
            cd /etc/init.d/
            /usr/sbin/chkconfig --add mysqld
            /usr/sbin/chkconfig mysqld on  #  设置为开机启动
            echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
            source /etc/profile.d/mysql.sh
        /etc/init.d/mysqld start
    else
            echo "MySQL数据目录已经存在,"
            exit 3
    fi
    fi
fi
posted @ 2021-03-07 23:08  上善若水~小辉  阅读(141)  评论(0编辑  收藏  举报