1、写一个一键安装 mariadb 数据库脚本。
#!/bin/bash
#二进制一键安装Mariadb-10.5.5 for Centos7
WGETFILE=https://mariadb.nethub.com.hk//mariadb-10.5.5/bintar-linux-systemd-x86_64/mariadb-10.5.5-linux-systemd-x86_64.tar.gz
WORKING_DIR=`pwd`
DATA_DIR="/data/mysql"
BASE_DIR="/usr/local"
FILE='mariadb-10.5.5-linux-systemd-x86_64.tar.gz'
#安装前检查
before_install (){
if [ -e "$FILE" ];then
echo "安装包存在,开始安装"
else
echo "检测到当前目录安装包不存在,尝试从互联网下载"
curl $WGETFILE &> /dev/null
[ $? -eq 0 ] && wget -v $WGETFILE ||exit
fi
if [ ! -d "/usr/local/mysql" ];then
return
else
echo "发现/usr/local目录下存在mysql文件夹,好像已经安装了MYSQL,退出安装,请检查"
exit
fi
}
install () {
echo "安装...."
tar -xvf $FILE -C /usr/local/
yum -y install libaio numactl-libs devel wget
yum -y install cmake ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel
yum -y install bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel gcc gcc-c++ perl-Data-Dumper
groupadd mysql
useradd -g mysql mysql
cd /usr/local
Mariad_DIR=`echo $FILE| sed -nr 's/^(.*[0-9]).*/\1/p'`
ln -s /usr/local/$Mariad_DIR /usr/local/mysql
chown -R root.root /usr/local/mysql/
echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
ln -s /usr/local/mysql/bin/* /usr/bin/
cat > /etc/my.cnf <<-EOF
[mysqld]
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
EOF
cd /usr/local/mysql
scripts/mysql_install_db --user=mysql --datadir=/data/mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start
}
before_install
install
echo "安装完毕"
2、简述Event 事件介绍以及它的优缺点
事件是MySQL在相应的时刻调用的过程式数据库对象。一个事件可调用一次,也可周期性的
启动,它由一个特定的线程来管理的,也就是所谓的“事件调度器”。
事件和触发器类似,都是在某些事情发生的时候启动。当数据库上启动一条语句的时候,触发器就启动
了,而事件是根据调度事件来启动的。由于它们彼此相似,所以事件也称为临时性触发器。
事件取代了原先只能由操作系统的计划任务来执行的工作,而且MySQL的事件调度器可以精确到每秒钟
执行一个任务,而操作系统的计划任务(如:Linux下的CRON或Windows下的任务计划)只能精确到
每分钟执行一次。
事件的优缺点
优点:一些对数据定时性操作不再依赖外部程序,而直接使用数据库本身提供的功能,可以实现每秒钟
执行一个任务,这在一些对实时性要求较高的环境下就非常实用
缺点:定时触发,不可以直接调用
3、在 students 表中,查询年龄大于25岁,且为男性的同学的名字和年龄:
MariaDB [hellodb]> select name,age from students where age > 25 and gender='M';
+--------------+-----+
| name | age |
+--------------+-----+
| Xie Yanke | 53 |
| Ding Dian | 32 |
| Yu Yutong | 26 |
| Shi Qing | 46 |
| Tian Boguang | 33 |
| Xu Xian | 27 |
| Sun Dasheng | 100 |
+--------------+-----+
7 rows in set (0.00 sec)
4、在 students 表中,以 ClassID 为分组依据,查询显示每组的平均年龄
MariaDB [hellodb]> select AVG(age),classid from students group by classid;
+----------+---------+
| AVG(age) | classid |
+----------+---------+
| 63.5000 | NULL |
| 20.5000 | 1 |
| 36.0000 | 2 |
| 20.2500 | 3 |
| 24.7500 | 4 |
| 46.0000 | 5 |
| 20.7500 | 6 |
| 19.6667 | 7 |
+----------+---------+
8 rows in set (0.00 sec)
5、显示第2题中平均年龄大于30的分组及平均年龄
MariaDB [hellodb]> select classid,AVG(age) from students group by classid having AVG(age) > 30 ;
+---------+----------+
| classid | AVG(age) |
+---------+----------+
| NULL | 63.5000 |
| 2 | 36.0000 |
| 5 | 46.0000 |
+---------+----------+
3 rows in set (0.00 sec)