1、写一个一键安装 mariadb 数据库脚本。
# 实现二进制安装mariadb 的一键安装脚本
#!/bin/bash
#
#*************************************************
#Author: dawn
#Date: 2020-09-25
#FileName: test.sh
#URL: https://blog.csdn.net/xiao_dan_
#Desc: The test script
#Copyright (C):2020 All rights reserved
#*************************************************
. /etc/init.d/functions
src_dir=`pwd`
mariadb_path=''
install_dir=/usr/local/
begin_color='echo -e \033[1;31m'
end_color='\033[0m'
read -p "请输入压缩包名称:" package
read -p "请输入数据库root 的口令(可选):" passwd
chk_envir() {
if [ $UID -ne 0 ];then
action "当前用户不是root,安装失败" false
exit 1
fi
if ! [ -e ${src_dir}/${package} ];then
${begin_color}"缺少${package}文件"${end_color}
${begin_color}"请将软件安装包放在${SRC_DIR}目录下"${end_color}
exit
elif [ -e /usr/local/mariadb ];then
action "数据库已存在,安装失败" false
exit
else
mariadb_path=`echo ${package} | sed -nr 's#^(.*)\..*\..*#\1#p'`
return
fi
}
install_mariadb() {
${begin_color}"开始安装mariadb 数据库..."${end_color}
# 创建mysql 系统用户
id mysql &> /dev/null || { groupadd mysql; useradd -r -g mysql -s /sbin/nologin mysql; action "创建mysql用户"; }
# 安装必要的依赖包
yum -y -q install libaio numactl-libs libncurses*
# 将安装包解压到/usr/local 目录下
tar -xf ${package} -C ${install_dir}
cd ${install_dir}
# 创建mysql 的软链接
ln -sv ${mariadb_path} mysql
chown -R root:root ${install_dir}mysql
# 设置环境变量
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
chmod +x /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
# 创建数据存放位置
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql
# 设置mysql 的配置文件
cat > /etc/my.cnf << EOF
[mysqld]
datadir = /data/mysql
innodb_file_per_table = on
skip_name_resolve = on
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
EOF
# 创建数据库文件(初始化数据库)
./mysql/scripts/mysql_install_db --datadir=/data/mysql --user=mysql
# 创建mysql 服务
cp ./mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
service mysqld start
[ $? -ne 0 ] && { ${begin_color}"数据库启动失败,退出!"${end_color};exit; }
# 如果输入了密码,则执行数据库加固操作
if ! ${passwd} &> /dev/null ; then
/usr/local/mysql/bin/mysql_secure_installation << EOF
y
${passwd}
${passwd}
y
y
y
y
EOF
action "数据库安装完成,root 口令已修改"
else
action "数据库安装完成"
${begin_color}"请执行exec mysql_secure_installation 初始化数据库并修改root 口令"${end_color}
fi
}
chk_envir
install_mariadb
2、简述Event 事件介绍以及它的优缺点
事件是MySQL 在相应的时刻调用的过程式数据库对象。一个事件可调用一次,也可周期性的启动。由一个特定的线程来管理,即“事件调度器”。跟触发器类似,都是在某些事情发生的时候启动。当数据库上启动一条语句的时候,触发器就启动了。而事件是根据调度事件来启动的。事件被称为临时性触发器。MySQL 的事件调度器可精确到每秒钟执行一个任务。
优点:一些对数据定实行操作不再依赖外部程序,而直接使用数据库本身提供的功能,可实现每秒钟执行一个任务,在对实时性要求较高的环境下非常实用。
缺点:定时触发,不可直接调用
3、在 students 表中,查询年龄大于25岁,且为男性的同学的名字和年龄:
select name,age from students where gender="M" and age > 25;
4、在 students 表中,以 ClassID 为分组依据,查询显示每组的平均年龄
select classid,avg(age) as 平均年龄 from students group by classid;
5、显示第2题中平均年龄大于30的分组及平均年龄
select classid,avg(age) as 平均年龄 from students group by classid having avg(age) > 30;