1、写一个一键安装 mariadb 数据库脚本。
[root@centos8 ~]# cat install_mariadb.sh
#!/bin/bash
#Version: 1.1
#**********************************************************************
. /etc/init.d/functions
MARIADB='mariadb-10.5.5-linux-x86_64.tar.gz'
#安装前准备
functions_bigin() {
#安装包
yum -y install libaio ncurses-compat-libs &> /dev/null
NAME=$1
FILE=`echo ${NAME} |sed -nr 's/(.*[0-9]).*/\1/p'`
#检查二进制文件
cd /usr/local
[ -f ${NAME} ] && action "mariadb文件" || { echo "请把准备的二进制文件放在/usr/local下";exit; }
#创建用户
id mysql &> /dev/null|| useradd -r -d /data/mysql mysql
action "mysql用户"
#解压二进制文件
cd /usr/local
tar xf ${NAME}
ln -s ${FILE} mysql
chown -R root:root /usr/local/mysql/
action "二进制解压"
}
#安装mariadb
install_mariadb() {
#配置文件
cd /usr/local/mysql
cat << EOF > /etc/my.cnf
[mysqld]
datadir=/data/mysql
skip_name_resolve=on
EOF
action "配置文件"
#创建数据库文件
ln -s /usr/local/mysql/bin/* /usr/bin/
scripts/mysql_install_db --datadir=/data/mysql --user=mysql &> /dev/null
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld &> /dev/null
service mysqld start &> /dev/null
action "mysql安装"
#执行安全脚本
/usr/local/mysql/bin/mysql_secure_installation << EOF &> /dev/null
y
n
y
y
y
y
EOF
action "mysql安全脚本"
#关闭无秘登录,并设置密码
mysql -e 'ALTER USER root@localhost IDENTIFIED VIA mysql_native_password USING PASSWORD("123456")'
}
#检查是否以及安装mysql
[ -h /usr/local/mysql ] && { echo "mysql 已经安装";exit; }
#开始安装mysql
functions_bigin $MARIADB
install_mariadb
2、简述Event 事件介绍以及它的优缺点
优点:一些对数据定时性操作不再依赖外部程序,而直接使用数据库本身提供的功能,可以实现每秒钟执行一个任务,这在一些对实时性要求较高的环境下就非常实用
缺点:定时触发,不可以直接调用
3、在 students 表中,查询年龄大于25岁,且为男性的同学的名字和年龄:
[root@centos8 ~]# mysql -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 18
Server version: 10.5.5-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| hellodb |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.001 sec)
MariaDB [(none)]> use hellodb
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [hellodb]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.000 sec)
MariaDB [hellodb]> select * from students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.001 sec)
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.001 sec)
4、在 students 表中,以 ClassID 为分组依据,查询显示每组的平均年龄
MariaDB [hellodb]> select classid,avg(age) as 'average age' from students group by classid;
+---------+-------------+
| classid | average age |
+---------+-------------+
| NULL | 63.5000 |
| 1 | 20.5000 |
| 2 | 36.0000 |
| 3 | 20.2500 |
| 4 | 24.7500 |
| 5 | 46.0000 |
| 6 | 20.7500 |
| 7 | 19.6667 |
+---------+-------------+
8 rows in set (0.001 sec)
5、显示第4题中平均年龄大于30的分组及平均年龄
MariaDB [hellodb]> select classid,avg(age) as 'average age' from students group by classid having avg(age)>30;
+---------+-------------+
| classid | average age |
+---------+-------------+
| NULL | 63.5000 |
| 2 | 36.0000 |
| 5 | 46.0000 |
+---------+-------------+
3 rows in set (0.001 sec)