shell脚本实例-数据库
更多脚本下载:https://opszzfwordpress.club/script/
mysql
mysql源码自动安装脚本:将以下脚本复制到mysql源码文件的目录中,执行mysql_install,使用mysql-5.7.33
#!/bin/bash #设定mysql目录,端口 echo "---------------------欢迎使用mysql-5.7.33一键部署脚本-------------------------" sleep 2 dir(){ echo "请输入安装路径,格式:/\$dir/\$dir" while read my_dir;do if [ "$my_dir" = "" ];then echo "!!错误,输入请重新输入!!" else echo "安装路径设置完成" sleep 2 break fi done } port(){ echo "请输入安装端口" while read my_port;do n1=`echo "$my_port" | sed s/[0-9]//g` port_no=`netstat -antp | awk -F" " '{print $4}' | grep -w $my_port | wc -l` if [ "$my_port" = "" ];then echo "!!错误,请重新输入!!" elif [ ! -z "$n1" ];then echo "!!输入的端口号值不符合要求,请重新输入!!" elif [ "$my_port" -lt 1 ] || [ "$my_port" -gt 65535 ];then echo "!!输入的端口号超出范围,请重新输入!!" elif [ "$port_no" -gt 0 ];then echo "!!端口已被占用,请重新输入!!" else echo "端口设置完毕" sleep 2 break fi done } #建立用户、安装目录 user_dir(){ echo "##########################开始建立用户、安装目录##############################" sleep 2 my_name=`awk -F":" '{ print $1 }' /etc/passwd | grep -wi mysql` if [ -z "$my_name" ];then useradd -s /sbin/nologin mysql sleep 2 echo "mysql管理用户已设置完成:mysql" else echo "mysql管理用户已存在,继续安装" fi if [ -d "$my_dir" ];then if [ "`ls $my_dir`" != "" ];then echo "!!目录下存在文件,请检查!!" sleep 2 exit 10 else mkdir -p $my_dir/{mysql_install,data,conf,logs,tmp} echo "目录建立完成" sleep 2 fi else echo "开始建立目录" mkdir -p $my_dir/{mysql_install,data,conf,logs,tmp} echo "目录建立完成" sleep 2 fi } #下载依赖环境 envir(){ echo "############################开始下载编译安装必要程序##########################" sleep 2 ping -c3 baidu.com &>/dev/null if [ $? -eq 0 ];then echo "网络连通正常" sleep 2 else echo "!!网络连通异常,请检查!!" sleep 2 exit 10 fi if [ -f -a "/etc/yum.repos.d/*.repo" ];then echo "yum源存在" yum -y install epel-release cmake bison ncurses-devel openssl-devel yum -y groupinstall "Development Tools" sleep 2 echo "依赖插件安装完毕" else echo "!!yum源不存在,请检查!!" sleep 2 exit fi } #开始编译、安装 inst(){ echo "###############################开始进行编译###################################" sleep 2 make clean cmake -DDEFAULT_CHARSET=utf8 -DDEFAULT_CHARSET=utf8 -DCMAKE_INSTALL_PREFIX=$my_dir/mysql_install -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=boost if [ $? -eq 0 ];then echo "编译成功,继续安装" else echo "!编译失败,请检查!" sleep 2 exit fi echo "###############################开始进行安装###################################" make && make install if [ $? -eq 0 ];then echo "安装成功" else echo "!!安装失败,请检查!!" exit 10 fi } #mysql配置文件 conf(){ echo "###############################开始配置文件###################################" sleep 2 cat >$my_dir/conf/mysql.cnf <<EOF [mysqld] port = $my_port user = mysql basedir = $my_dir/mysql_install datadir = $my_dir/data pid-file = $my_dir/mysql_install/mysql.pid log_error = $my_dir/logs/mysql-error.log slow_query_log = 1 long_query_time = 1 slow_query_log_file = $my_dir/logs/mysql-slow.log # session memory configure. read_buffer_size = 8M read_rnd_buffer_size = 4M sort_buffer_size = 4M tmp_table_size = 32M max_heap_table_size = 32M join_buffer_size = 4M thread_cache_size = 64 #innodb engine configure. innodb_file_per_table = 1 innodb_buffer_pool_size = 5G innodb_buffer_pool_load_at_startup = 1 innodb_buffer_pool_dump_at_shutdown = 1 innodb_log_file_size = 2G innodb_log_buffer_size = 32M innodb_sort_buffer_size = 64M innodb_write_io_threads = 8 innodb_read_io_threads = 8 innodb_online_alter_log_max_size = 4G innodb_open_files = 65535 EOF #设置属主属组 chown -R mysql:mysql $my_dir echo "配置文件完毕" sleep 2 } myinit(){ echo "###############################开始配置初始化################################" sleep 2 #mysql初始化 $my_dir/mysql_install/bin/mysqld --defaults-file=$my_dir/conf/mysql.cnf --initialize-insecure if [ $? -eq 0 ];then echo "配置初始化成功" sleep 2 else echo "!!配置初始化失败,请检查!!" sleep 2 exit fi #配置环境变量 if [ -z "`grep -w "export PATH=$my_dir/mysql_install/bin:\$\PATH" /etc/profile`" ];then echo "export PATH=$my_dir/mysql_install/bin:\$PATH" >> /etc/profile source /etc/profile else : fi #配置使用systemctl控制 cat >/usr/lib/systemd/system/mysqld.service <<EOF [Unit] Description=MySQL Server Documentation=https://dev.mysql.com/doc/refman/en/using-systemd.html After=network.target [Install] WantedBy=multi-user.target [Service] User=mysql ExecStart=$my_dir/mysql_install/bin/mysqld --defaults-file=$my_dir/conf/mysql.cnf EOF systemctl daemon-reload } dir port user_dir envir inst conf myinit echo "########################安装完毕,请使用systemctl启动mysql###################" sleep 2
mysql库备份脚本:全库全量备份
#!/bin/bash DATE=`date +%F-%H-%M-%S` mysqldump -uroot -p"123Abc.." wordpress > /mysql/bak/wordpress/wp-$DATE.sql file_name=`ls -l /mysql/bak/wordpress/ | grep -v total | tail -1 | awk -F" " '{print $9}'` if [ $? -eq 0 ];then echo "`date +%F`:$file_name:successful" >> /mysql/bak/wordpress/log/wp_bak.log else echo "The wordpress sql is fails to backup,please check" >> /mysql/bak/wordpress/log/wp_bak_err.log fi file_num=`ls -l /mysql/bak/wordpress/*.sql | wc -l` file_1=`ls -l /mysql/bak/wordpress/*.sql | grep -v total | head -1 | awk -F" " '{print $9}'` if [ "$file_num" -gt 5 ];then rm -rf $file_1 fi
redis
redis安装脚本:将安装脚本复制到redis源码目录中执行,按照提示操作即可
#!/bin/bash #设置redis安装目录 while read -p"请输入redis安装路径(按q退出):" redis_dir;do if [ "$redis_dir" == "" ];then echo "值为空,请重新输入" elif [ "$redis_dir" == "q" ];then echo "现在退出redis安装程序" exit 10 else echo "安装目录设置完毕" break fi done #设置redis端口 while read -p"请输入redis安装端口:" redis_port;do if [ "$redis_port" == "" ];then echo "值为空,请重新输入" elif [ "$redis_dir" = "q" ];then echo "现在退出redis安装程序" exit 10 else # lsof -i:$redis_port &>/dev/null netstat -lntp | grep -w '$redis_port' &>/dev/null if [ $? -eq 0 ];then echo "端口已被占用,请重新输入" else echo "端口设置完毕" break fi fi done #建立redis用户 useradd redis -d $redis_dir/redis sys(){ #进行系统配置调优 grep '^vm.overcommit_memory.*$' /etc/sysctl.conf &>/dev/null if [ $? -eq 0 ];then vm_value=`grep '^vm.overcommit_memory.*$' /etc/sysctl.conf | awk -F"=" '{print $2}' | sed "s/^[ \t]*//g"` if [ "$vm_value" != 1 ];then sed -i "s/^vm.overcommit_memory.*$/vm.overcommit_memory = 1/g" /etc/sysctl.conf sysctl -p fi else echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf sysctl -p fi egrep -w "^never$" /sys/kernel/mm/transparent_hugepage/enabled &>/dev/null if [ $? -ne 0 ];then echo "never" >> /sys/kernel/mm/transparent_hugepage/enabled fi } sys redis_ins(){ #安装redis make clean make && make PREFIX=/$redis_dir/redis install if [ $? -eq 0 ];then echo "redis安装成功" else echo "redis安装失败,请检查" fi } redis_ins file_con(){ #复制配置文件及启停控制文件 cp redis.conf $redis_dir/redis/${redis_port}.conf cp utils/redis_init_script /etc/init.d/redis_$redis_port #启动文件 sed -i "s&^REDISPORT.*$&REDISPORT=$redis_port&g" /etc/init.d/redis_$redis_port sed -i "s&^EXEC.*$&EXEC=$redis_dir\/redis\/bin\/redis-server&g" /etc/init.d/redis_$redis_port sed -i "s&^CONF.*$&CONF=$redis_dir\/redis\/${redis_port}.conf&g" /etc/init.d/redis_$redis_port sed -i "s&^CLIEXEC.*$&CLIEXEC=$redis_dir\/redis\/bin\/redis-cli&g" /etc/init.d/redis_$redis_port sed -i "s&^PIDFILE.*$&PIDFILE=$redis_dir\/redis\/${redis_port}.pid&g" /etc/init.d/redis_$redis_port #redis配置文件 cd $redis_dir/redis mkdir log mkdir backup sed -i "s&^port.*$&port $redis_port&g" $redis_dir/redis/${redis_port}.conf sed -i "s&^daemonize.*$&daemonize yes&g" $redis_dir/redis/${redis_port}.conf sed -i "s&^pidfile.*$&pidfile $redis_dir\/redis\/${redis_port}.pid&g" $redis_dir/redis/${redis_port}.conf sed -i "s&^logfile.*$&logfile $redis_dir\/redis\/log\/redis.log&g" $redis_dir/redis/${redis_port}.conf chown -R redis:redis $redis_dir/redis } file_con #redis备份 rebak(){ #rdb备份 rerdb(){ sed -i "s/^save/#save/g" $redis_dir/redis/${redis_port}.conf #sed -i "/^#save/isave 900 1/" $redis_dir/redis/${redis_port}.conf sed -i "/# save \"\"/asave 900 1" $redis_dir/redis/${redis_port}.conf sed -i "s/^stop-writes-on-bgsave-error.*$/stop-writes-on-bgsave-error no/g" $redis_dir/redis/${redis_port}.conf sed -i "s/^rdbcompression.*$/rdbcompression yes/g" $redis_dir/redis/${redis_port}.conf sed -i "s/^dbfilename.*$/dbfilename dump.rdb/g" $redis_dir/redis/${redis_port}.conf sed -i "s&^dir.*$&dir $redis_dir\/redis\/backup&g" $redis_dir/redis/${redis_port}.conf } reaof(){ sed -i "s/^appendonly.*$/appendonly yes/g" $redis_dir/redis/${redis_port}.conf sed -i "s/^appendfsync/#appendfsync/g" $redis_dir/redis/${redis_port}.conf sed -i "/^#appendfsync/iappendfsync everysec" $redis_dir/redis/${redis_port}.conf sed -i "s&^dir.*$&dir $redis_dir\/redis\/backup&g" $redis_dir/redis/${redis_port}.conf } while read -p"是否设置备份[y/n]:" jubak;do if [ "$jubak" = "y" ];then cat <<-EOF 1:rdb 2:aof 3:ALL q:退出 EOF while read -p"请选择开启rdb备份/aof备份:" jumeth;do case "$jumeth" in 1) rerdb break ;; 2) reaof break ;; 3) rerdb reaof break ;; q) return 1 ;; *) echo "value is error" ;; esac done break elif [ "$jubak" = "n" ];then return 1 # break else echo "value is error" fi done echo "redis备份已设置完毕" } rebak echo "redis安装完毕,请切换到redis用户启动"