shell脚本------一键部署MySQL
Posted 下雨天的放羊娃
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell脚本------一键部署MySQL相关的知识,希望对你有一定的参考价值。
一.数据库的基本概念
将安装包传到opt目录中
安装包地址:MySQL安装包
编写脚本文件,将代码复制到脚本文件中,source执行脚本,等待安装即可
#!/bin/bash
CKGZ=`df -Th | grep -o "iso9660"`
ZDGZ=`cat /etc/fstab | grep -o "iso9660"`
#检查光盘是否挂载
if [ "$CKGZ" == 'iso9660' ];then
echo "您的挂盘已经挂载,无需重复操作!"
else
echo "正在为您挂载光盘……"
mount /dev/sr0 /mnt &> /dev/null
echo "已为您挂载挂载光盘!"
fi
if [ "$ZDGZ" == 'iso9660' ];then
echo "您的光盘已设置自动挂载,无需重复操作!"
else
echo "正在为您设置自动挂载……"
echo "/dev/sr0 /mnt iso9660 defaults 0 0 ">>/etc/fstab
echo "已为您设置自动挂载。"
fi
echo "正在配置yum源中"
ping -c5 www.baidu.com &> /dev/null
if [ $? -eq 0 ];then
mkdir -p /etc/yum.repos.d/repo.bak
mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null&> /dev/null
"yum clean all && yum makecache" &> /dev/null
yum list &> /dev/null
echo "在线yum源已配置完成!"
else
mkdir -p /etc/yum.repos.d/repo.bak
mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
echo -e '[local]\\nname=local\\nbaseurl=file:///mnt\\nenabled=1\\ngpgcheck=0' > /etc/yum.repos.d/local.repo
"yum clean all && yum makecache" &> /dev/null
yum list &> /dev/null
echo "本地yum源已配置完成!"
fi
#环境准备
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
echo -e "正在检查安装包是否正确"
ls /opt | egrep "^mysql-boost-5.7.20.tar.gz$"
if [ $? -eq 0 ];then
echo -e "准备安装,请等待"
else
echo -e "请准备正确安装包:mysql-boost-5.7.20.tar.gz"
exit
fi
#安装依赖包
yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake &>/dev/null
if [ $? -eq 0 ];then
echo -e "依赖包安装完毕"
else
exit
echo -e "依赖包安装失败,请检查"
fi
#创建用户
useradd -M -s /sbin/nologin mysql
#解压安装包
cd /opt
tar xzvf mysql-boost-5.7.20.tar.gz -C /opt &>/dev/null
if [ $? -eq 0 ];then
echo -e "安装包解压完毕"
else
exit
echo -e "安装包解压失败,请检查"
fi
#安装模块
cd /opt/mysql-5.7.20/
cmake \\
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \\
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \\
-DSYSCONFDIR=/etc \\
-DSYSTEMD_PID_DIR=/usr/local/mysql \\
-DDEFAULT_CHARSET=utf8 \\
-DDEFAULT_COLLATION=utf8_general_ci \\
-DWITH_EXTRA_CHARSETS=all \\
-DWITH_INNOBASE_STORAGE_ENGINE=1 \\
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \\
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \\
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \\
-DMYSQL_DATADIR=/usr/local/mysql/data \\
-DWITH_BOOST=boost \\
-DWITH_SYSTEMD=1 &>/dev/null
if [ $? -eq 0 ];then
echo -e "模块安装完毕"
else
exit
echo -e "模块安装失败,请检查"
fi
#编译及安装
make && make install
#修改mysql 配置文件
echo '[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES' > /etc/my.cnf
#更改mysql安装目录和配置文件的属主属组
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf
#设置路径环境变量
echo "export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib">> /etc/profile
source /etc/profile
#初始化数据库
cd /usr/local/mysql/bin/
./mysqld \\
--initialize-insecure \\
--user=mysql \\
--basedir=/usr/local/mysql \\
--datadir=/usr/local/mysql/data
#添加mysqld系统服务
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl start mysqld.service
以上是关于shell脚本------一键部署MySQL的主要内容,如果未能解决你的问题,请参考以下文章