安装MariaDB脚本

#!/bin/bash
*************************
source /etc/init.d/functions
DIR=/usr/local
ID=mysql
VERSION=mariadb-10.5.5-linux-systemd-x86_64
PACKAGE=https://mirrors.tuna.tsinghua.edu.cn/mariadb//mariadb-10.5.5/bintar-linux-systemd-x86_64/mariadb-10.5.5-linux-systemd-x86_64.tar.gz
MYDIR=/data/mysql
DBPATH(){
    
    

if [ -f /etc/profile.d/mysql.sh ];then

    . /etc/profile.d/mysql.sh

else
    
    echo PATH=$DIR/mysql/bin:$PATH > /etc/profile.d/mysql.sh

    . /etc/profile.d/mysql.sh

fi

}

INSTALLDB(){
    
    

    DBPATH

    cat > /etc/my.cnf <<EOF
[mysqld]
datadir=/data/mysql
skip_name_resolve=1    
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
EOF

    mkdir -p /data/mysql

    chown mysql.mysql /data/mysql

    /$DIR/mysql/scripts/mysql_install_db --datadir=/data/mysql --user=mysql &> /dev/null

}

SERVERVICE(){
    
    

    cp $DIR/mysql/support-files/systemd/mariadb.service /usr/lib/systemd/system/
    systemctl daemon-reload
    systemctl enable --now mariadb

}

SECURE(){
    
    

   expect &> /dev/null <<EOF

spawn mysql_secure_installation

expect {
        "Enter current password for root" { send "\n";exp_continue }
        "Switch to unix_socket authentication" { send "no\n";exp_continue }
        "Change the root password" { send "y\n";exp_continue }
        "New password" { send "123456\n";exp_continue } 
        "Re-enter new password" { send "123456\n";exp_continue } 
        "Remove anonymous users" { send "y\n";exp_continue } 
        "Disallow root login remotely" { send "y\n";exp_continue } 
        "Remove test database and access to it" { send "y\n";exp_continue } 
        "Reload privilege tables now" { send "y\n" } 
}
expect eof 

EOF

}

yum -y install autoconf libaio perl-Data-Dumper ncurses-compat-libs expect &> /dev/null

if `id $ID &> /dev/null`;then

    echo '用户已存在,跳过用户创建过程'

else
    
    groupadd $ID

    useradd -r -g $ID -s /sbin/false $ID

fi

if [ -h $DIR/mysql ];then

    echo '数据库已安装,勿重复安装'
    
    exit

else
    
    if [ ! -f $VERSION.tar.gz ];then
    
        curl $PACKAGE -O /root &> /dev/null
    
    else 
    
        echo '二进制包已存在,跳过下载源码包步骤'
    
    fi
    
    if [ ! -d $DIR/$VERSION ];then

        tar -xf $VERSION.tar.gz -C $DIR

        ln -s $DIR/$VERSION $DIR/mysql

        chown -R mysql.mysql $DIR/mysql/

        INSTALLDB

        SERVERVICE

        SECURE && action "数据库已安装并安全初始化成功"

    else 
        
        ln -s $DIR/$VERSION $DIR/mysql

        chown -R mysql.mysql $DIR/mysql/

        INSTALLDB

        SERVERVICE

        SECURE && action "数据库已安装并安全初始化成功"

    fi

fi

Event 事件介绍以及优缺点

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

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

MariaDB [hellodb]> select name,gender,age from students where age > 25 and gender = 'M';
+--------------+--------+-----+
| name         | gender | age |
+--------------+--------+-----+
| Xie Yanke    | M      |  53 |
| Ding Dian    | M      |  32 |
| Yu Yutong    | M      |  26 |
| Shi Qing     | M      |  46 |
| Tian Boguang | M      |  33 |
| Xu Xian      | M      |  27 |
| Sun Dasheng  | M      | 100 |
+--------------+--------+-----+
7 rows in set (0.000 sec)

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

MariaDB [hellodb]> select classid 班级,avg(age) 平均年龄 from students group by classid;
+--------+--------------+
| 班级   | 平均年龄     |
+--------+--------------+
|   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.000 sec)

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

MariaDB [hellodb]> select classid 班级,avg(age) 平均年龄 from students group by classid having 平均年龄 > 30;
+--------+--------------+
| 班级   | 平均年龄     |
+--------+--------------+
|   NULL |      63.5000 |
|      2 |      36.0000 |
|      5 |      46.0000 |
+--------+--------------+
3 rows in set (0.000 sec)

猜你喜欢

转载自blog.csdn.net/u014578909/article/details/108842894