MySQL8.0数据库⾃动部署脚本
⽣产环境MySQL数据库⽬前主推MySQL8.0版本,这⾥提供MySQL8.0数据库⾃动部署脚本。
测试结果就不写了…这⾥主要做记录⽤。
实现功能如下:
1、⾃动分区/dev/sdb磁盘(因为磁盘分区在应⽤服务器是不做的,所以这⼀步不放在环境初始化上)
2、⾃动部署MySQL8.0数据库
脚本内容如下:
#!/bin/bash
>>>>>>###
# copyright by hwb
# DATE:2020-05-06
#
# ⾃动化部署(rhel 7)
# jdk    1.8.0_141
# redis  3.2.1
# mysql  5.7.27
# tomcat 8.5.51
# postgresql 12.2
>>>>>>###
# ⽬录规划:
# mysql安装在/data
# redis安装在/usr/local/redis
# tomcat安装在/home下
# EFK统⼀rpm安装
# 脚本及安装包统⼀放在/opt⽬录下
>>>>>>###
#mysql
MYSQL_HOME=/data
MYSQL_PWD=xxx1234
MYSQL_PORT=53306
#磁盘
dev="/dev/sdb"
#调⽤函数库
[ -f /etc/init.d/functions ]&&source /etc/init.d/functions
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
source /etc/profile
#Require root to run this script.
[$(id -u) -gt 0 ]&&echo"请⽤root⽤户执⾏此脚本!"&&exit 1
#报错⽇志记录
[ -f /tmp/install_error.log ]||touch /tmp/install_error.log echo">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>$DATE系统初始化报错记录<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<">> /tmp/install_error.log
auto_disk(){
echo""
echo -e "\033[33m**************************************************⾃动分区${dev}磁盘*********************
**********************************\033[0m"
#判断磁盘是否已经进⾏了分区
judge_disk=`fdisk -l $dev |grep"^$dev"|wc -l`
#ge:⼤于等于
if[$judge_disk -ge 1 ];then
action "****$dev磁盘已经分区,请管理员慎重检查*****" /bin/false
echo"$dev磁盘已经分区,请管理员慎重检查">> /tmp/install_error.log
return$?#退出脚本,后⾯的命令不再执⾏
else
echo"*******开始进⾏磁盘分区*******"
fdisk$dev<<EOF
fdisk$dev<<EOF
n
p
1
w
EOF
action "****$dev磁盘已经分区完成*****" /bin/true
fi
echo"*******创建物理卷*******"
pvcreate ${dev}1
pvs
#待分配空间
pvfree=`pvs |grep $dev  |awk  -F '<''{print $NF-1}'`
echo"*******创建卷组*******"
vgcreate datavg ${dev}1
vgs
echo"*******创建逻辑卷*******"
lvcreate -n lv_data -L +${pvfree}G  datavg
lvs
echo"*******格式化磁盘*******"
<4 /dev/datavg/lv_data
echo"*******挂载及开机⾃动挂载*******"
[ -d /data ]||mkdir -p /data
\cp /etc/fstab /etc/fstab.$(date +%F)
echo"/dev/datavg/lv_data    /data                          ext4 defaults          0 0">>/etc/fstab
mount -a
echo -e "\033[33m*************************************************已完成磁盘⾃动分区**************************************************\033[0m"
echo""
sleep 1
}
function install_mysql8_el7()
{
auto_disk
echo""
echo -e "\033[33m***************************************************⾃动部署mysql8.0**************************************************\033[0m"
#建⽤户及⽬录
groupadd -r mysql &&useradd -r -g mysql mysql -d /home/mysql -m
mkdir -p $MYSQL_HOME/datafile &&mkdir -p $MYSQL_HOME/log &&mkdir -p $MYSQL_HOME/backup
chown -R mysql:mysql $MYSQL_HOME&&chmod -R 755 $MYSQL_HOME
#关闭selinux
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
#下载包
if[ -f /opt/mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar ];then
echo"*****存在mysql8安装包,⽆需下载*****"
else
ping -c 4 app.fslgz >/dev/null 2>&1
mysql下载libs包的网址
if[$? -eq 0 ];then
#wget sql//Downloads/MySQL-8.0/mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar  -P /opt
wget -q app.fslgz/portal/api/public/fs/association/file/downLoad?uploadId=773504498357305344 -O /opt/mysql-8.0.19-1.el7.x86_64.rpm-bund le.tar
else
echo"please download mysql8 package manual !"
echo"please download mysql8 package manual !"
exit$?
fi
fi
#配yum安装mysql依赖包
rpm -qa|grep libaio-devel
if[$? -eq 1 ];then
yum install -y gcc gcc-c++ openssl openssl-devel libaio libaio-devel  ncurses  ncurses-devel &>/dev/null
action "***************安装mysql依赖包完成***************" /bin/true
else
action "****************已安装mysql依赖包****************" /bin/false
fi
#安装mysql8.0
ps -ef|grep mysqld |grep -v grep
if[$? -eq 0 ];then
echo"*****************已存在mysql进程*****************"
exit$?
else
# 卸载 mysql
rpm -qa|grep mysql|xargs -i rpm -e --nodeps {}
# uninstall mariadb-libs
rpm -qa|grep mariadb|xargs -i rpm -e --nodeps {}
# 安装mysql
action "***************开始安装mysql数据库***************" /bin/true
tar -xvf /opt/mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar -C  /opt/  &>/dev/null
rpm -ivh /opt/mysql-community-common-8.0.19-1.el7.x86_64.rpm
rpm -ivh /opt/mysql-community-embedded-compat-8.0.19-1.el7.x86_64.rpm
rpm -ivh /opt/mysql-community-libs-8.0.19-1.el7.x86_64.rpm
rpm -ivh /opt/mysql-community-libs-compat-8.0.19-1.el7.x86_64.rpm
rpm -ivh /opt/mysql-community-devel-8.0.19-1.el7.x86_64.rpm
rpm -ivh /opt/mysql-community-client-8.0.19-1.el7.x86_64.rpm
rpm -ivh /opt/mysql-community-server-8.0.19-1.el7.x86_64.rpm
#mysql-community-test-8.0.19-1.el7.x86_64.rpm
fi
#配置myf
cp /etc/myf /etc/myf_${DATE}bak &>/dev/null
cat<< EOF > /etc/myf
[mysqld]
#解决时区问题
default-time-zone ='+8:00'
#解决mysql⽇志时间与系统时间不⼀致问题
log_timestamps=SYSTEM
port=${MYSQL_PORT}
datadir=${MYSQL_HOME}/datafile
log-error=${MYSQL_HOME}/log/mysqld.log
#mysql8默认禁⽤Symbolic links,⽆需再去标记禁⽤
#symbolic-links=0
bind-address=0.0.0.0
lower_case_table_names=1
character_set_server=utf8mb4
max_allowed_packet=500M
#SQL Mode的NO_AUTO_CREATE_USER取消
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION #InnoDB⽤于缓存数据、索引、锁、插⼊缓冲、数据字典等
innodb_buffer_pool_size=4G
#InnoDB的log buffer
innodb_log_buffer_size = 64M
#InnoDB redo log⼤⼩
innodb_log_file_size = 256M
#InnoDB redo log⽂件组
innodb_log_files_in_group = 2
innodb_flush_log_at_trx_commit = 1
sync_binlog = 1
sync_binlog = 1
#连接数
max_connections=600
max_connect_errors=1000
max_user_connections=400
#设置临时表最⼤值
max_heap_table_size = 100M
tmp_table_size = 100M
#每个连接都会分配的⼀些排序、连接等缓冲
sort_buffer_size = 2M
join_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 2M
#mysql8⾃动关闭query cache
#query_cache_size = 0
#如果是以InnoDB引擎为主的DB,专⽤于MyISAM引擎的 key_buffer_size 可以设置较⼩,8MB 已⾜够,如果是以MyISAM引擎为主,可设置较⼤,但不能超过4 G
key_buffer_size = 8M
#设置慢查询阀值,单位为秒
long_query_time = 60
slow_query_log=1
log_output=table,File    #⽇志输出会写表,也会写⽇志⽂件,为了便于程序去统计,所以最好写表
slow_query_log_file=${MYSQL_HOME}/log/slow.log
#快速预热缓冲池
innodb_buffer_pool_dump_at_shutdown=1
innodb_buffer_pool_load_at_startup=1
#打印deadlock⽇志
innodb_print_all_deadlocks=1
#⼆进制配置
server-id = 1
log-bin =${MYSQL_HOME}/log/mysql-bin.log
log-bin-index =${MYSQL_HOME}/log/binlog.index
log_bin_trust_function_creators=1
binlog_format = row
gtid_mode = ON
enforce_gtid_consistency = ON
#expire-logs-days参数取消,修改成binlog_expire_logs_seconds,单位为秒,以下代表15天
binlog_expire_logs_seconds=1296000
#schedule
event_scheduler = on
#unknown variable 'show_compatibility_56=on'
#show_compatibility_56=on
#处理TIMESTAMP with implicit DEFAULT value is deprecated
explicit_defaults_for_timestamp=true
#MySQL 8.0改了默认加密⽅式为“caching_sha2_password”,这⾥改回来
default_authentication_plugin=mysql_native_password
#禁⽤SSL提⾼性能
skip_ssl
#timeout
wait_timeout = 3600
interactive_timeout = 3600
net_read_timeout = 3600
net_write_timeout = 3600
#密码有效期
default_password_lifetime=360
EOF
#启动数据库
systemctl start mysqld.service
sleep 3
MYSQL_TEMP_PWD=$(grep"temporary password" ${MYSQL_HOME}/log/mysqld.log|cut -d "@" -f 2|awk'{print $2}')
#mysql 8密码策略validate_password_policy 变为validate_password.policy
#MYSQL 8.0内新增加mysql_native_password函数,通过更改这个函数密码来进⾏远程连接
mysql -hlocalhost  -P${MYSQL_PORT}  -uroot -p"${MYSQL_TEMP_PWD}" -e "set global validate_password.policy=0" --connect-expired-password
mysql -hlocalhost  -P${MYSQL_PORT}  -uroot -p"${MYSQL_TEMP_PWD}" -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_passwor d BY '${MYSQL_PWD}'"  --connect-expired-password
d BY '${MYSQL_PWD}'"  --connect-expired-password
mysql -hlocalhost  -P${MYSQL_PORT}  -uroot -p"${MYSQL_PWD}" -e "CREATE USER 'root'@'%' IDENTIFIED BY '${MYSQL_PWD}'"  --connect-expired-password
mysql -hlocalhost  -P${MYSQL_PORT}  -uroot -p"${MYSQL_PWD}" -e "grant all privileges on *.* to root@'%'"  --connect-expired-password
#授权root远程登录需输⼊以下命令:
#create user root@'%' identified by 'fswl@1234';
#grant all privileges on *.* to root@'%';
echo -e "\033[33m************************************************完成mysql8.0数据库部署***********************************************\033[0m"
cat> /tmp/mysql8.log  <<EOF
mysql安装⽬录:${MYSQL_HOME}
mysql版本:Mysql-8.0
mysql端⼝:${MYSQL_PORT}
mysql密码:${MYSQL_PWD}
EOF
cat /tmp/mysql8.log
echo -e "\e[1;31m 以上信息10秒后消失,保存在/tmp/mysql8.log⽂件下 \e[0m"
echo -e "\033[33m*********************************************************************************************************************\033[0m"
echo""
sleep 10
}
install_mysql8_el7

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

centos7安装ar压缩包
« 上一篇
ant之l详解
下一篇 »

发表评论