返回顶部

博客:一键安装 mariadb 数据库脚本及event事件优缺点

 

1、写一个一键安装 mariadb 数据库脚本。

#!/bin/bash

#

#********************************************************************

#Author:wei

#QQ: 1655841639

#Date: 2020-09-26

#FileName:onekey_mariadb10.5.sh

#URL:  

#Description:The test script

#Copyright (C): 2020 All rights reserved

#********************************************************************

DIR=`pwd`

NAME="mariadb-10.5.5-linux-systemd-x86_64.tar.gz"

FULL_NAME=${DIR}/${NAME}

PASS='magedu'

 

#1.准备环境和相关包

rpm -qi libaio &> /dev/null ||  yum -y install libaio

rpm -qi numactl-libs &> /dev/null || yum -y install  numactl-libs

 

#2.准备用户

if id mysql ;then

    echo "用户已存在!"

else

    useradd -r  -s /bin/nologin  mysql

fi

 

 

#3.准备二进制文件

if [ -f ${FULL_NAME} ] ;then

    echo "安装文件已存在!"

else

    echo "安装文件不存在!"

fi

if [ -h /usr/local/mysql ];then

    echo "mysql已安装"

else

tar -xf /usr/local/mariadb-10.5.5-linux-systemd-x86_64.tar.gz   -C  /usr/local/

ln -sv /usr/local/mariadb-10.5.5-linux-systemd-x86_64  /usr/local/mysql

chown -R root:root /usr/local/mysql

fi

 

#4.准备配置文件

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

 

[client]

socket=/data/mysql/mysql.sock

EOF

 

#5.创建数据库文件

if [ ! -d /data/mysql ];then

    mkdir -pv /data/mysql && chown -R mysql.mysql /data/mysql

    /usr/local/mysql/scripts/mysql_install_db --datadir=/data/mysql --user=mysql

    #6.准备服务脚本

    cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld

    #7.设置环境变量

    echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh

    chmod a+x  /etc/profile.d/mysql.sh

    . /etc/profile.d/mysql.sh

    chkconfig --add mysqld

    service mysqld start

else

    echo "mysql 数据目录完成"

fi

echo "安装成功!"

 

 

#8.安全加固

#ln -sv /data/mysql/mysql.sock     /tmp/mysql.sock    #若安全脚本报错Can't connect to local MySQL server through socket '/tmp/mysql.sock',需要加上,因版本不同而异

 

/usr/local/mysql/bin/mysql_secure_installation  <<EOF

 

y

$PASS

$PASS

y

y

y

y

 

EOF

 

2、简述Event 事件介绍以及它的优缺点

答:

事件(event)MYSQL在相应的时刻调用的过程式数据哭对象。一个事件可以调用一次,也可周期性的启动,他由一个特定的线程来管理,也就是所谓的“事件调度器”

事件的优点:

一些数据定时性操作不再依赖外部程序,而直接使用数据库本身提供的功能,可以实现每秒钟执行一个任务,这在一些对实时性要求较高的环境下,就非常实用。

事件的缺点:

定时触发,不可直接调用

 

3.在 students 表中,查询年龄大于25岁,且为男性的同学的名字和年龄:

# select  name,age,gender from students  where gender='M' and  age > 25 ;

 +--------------+-----+--------+

| name | age | gender |
+--------------+-----+--------+
| Xie Yanke | 53 | M |
| Ding Dian | 32 | M |
| Yu Yutong | 26 | M |
| Shi Qing | 46 | M |
| Tian Boguang | 33 | M |
| Xu Xian | 27 | M |
| Sun Dasheng | 100 | M |
+--------------+-----+--------+
7 rows in set (0.00 sec)

4.在 students 表中,以 ClassID 为分组依据,查询显示每组的平均年龄

#select  classID,avg(age) from students group by classID;

#select  classID,avg(age) from students group by -classID desc;

+---------+----------+
| classID | avg(age) |
+---------+----------+
| 1 | 20.5000 |
| 2 | 36.0000 |
| 3 | 20.2500 |
| 4 | 24.7500 |
| 5 | 46.0000 |
| 6 | 20.7500 |
| 7 | 19.6667 |
| NULL | 51.6667 |
+---------+----------+
8 rows in set (0.04 sec)

 

 

5、显示第4题中平均年龄大于30的分组及平均年龄

#select  classID,avg(age) from students group by classID having   avg(age)  > 30 ;

#select  classID,avg(age) from students group by -classID desc  having  avg(age) > 30 ;

+---------+----------+
| classID | avg(age) |
+---------+----------+
| 2 | 36.0000 |
| 5 | 46.0000 |
| NULL | 51.6667 |
+---------+----------+
3 rows in set (0.00 sec)

 

posted @ 2020-09-26 12:37  九尾cat  阅读(205)  评论(0编辑  收藏  举报