Linux系统下shell脚本实战之mariadb创建数据库和表

Posted 江湖有缘

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux系统下shell脚本实战之mariadb创建数据库和表相关的知识,希望对你有一定的参考价值。

Linux系统下shell脚本实战之mariadb创建数据库和表

一、脚本要求

向mariadb数据库创建测试数据库和表

二、环境准备

1.配置yum仓库

# vim /etc/yum.repos.d/mariadb.repo
# MariaDB 10.6 CentOS repository list - created 2021-12-27 11:21 UTC
# https://mariadb.org/download/
[mariadb]
name = MariaDB
baseurl = https://mirrors.xtom.com.hk/mariadb/yum/10.6/centos7-amd64
gpgkey=https://mirrors.xtom.com.hk/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1

2.安装mariadb

yum -y install mariadb-server

3.设置服务自启

[root@compute-node1 yum.repos.d]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@compute-node1 yum.repos.d]# systemctl start mariadb

4.进入数据库

[root@compute-node1 yum.repos.d]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \\g.
Your MariaDB connection id is 3
Server version: 10.6.8-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)]> 

5.修改数据库root密码

[root@compute-node1 scripts]# mysqladmin -uroot password '123'

6.设置root远程登录

MariaDB [(none)]> GRANT USAGE ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root' @'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> set password for 'root'@'%'=password('123');
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> 

7.测试远程登录

[root@compute-node1 scripts]# mysql -uroot -p123
Welcome to the MariaDB monitor.  Commands end with ; or \\g.
Your MariaDB connection id is 7
Server version: 10.6.8-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)]> 

三、编写shell脚本

[root@compute-node1 ~]# cd /data/scripts/
[root@compute-node1 scripts]# cat mysql_create.sh 
#!/bin/bash
########################################
#Author:jeven
#time:Thu 30 Jun 2022 18:16:39 PM CST
#filename:mysql_create.sh
#Script description:
########################################
USER=root
PASS=123

mysql -u $USER -p$PASS <<EOF  2> /dev/null
CREATE DATABASE students;
EOF

if [ $? -eq 0 ];then
       echo Create DB
else
       echo DB alreadly exsist
fi
mysql -u $USER -p$PASS students  <<EOF  2> /dev/null
CREATE TABLE students(
id int,
name varchar(100),
mark int,
dept varchar(4)
);
EOF

if [ $? -eq 0 ];then 
   echo Create table students
else
   echo Table alreadly exsist
fi


mysql -u $USER -p$PASS students  <<EOF
DELETE FROM students;
EOF

四、执行脚本

sh ./mysql_create.sh

五、检查运行结果

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| students           |
| sys                |
| test               |
+--------------------+
MariaDB [students]> show tables;
+--------------------+
| Tables_in_students |
+--------------------+
| students           |
+--------------------+
1 row in set (0.000 sec)
MariaDB [students]>  describe students;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | YES  |     | NULL    |       |
| name  | varchar(100) | YES  |     | NULL    |       |
| mark  | int(11)      | YES  |     | NULL    |       |
| dept  | varchar(4)   | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.001 sec)

以上是关于Linux系统下shell脚本实战之mariadb创建数据库和表的主要内容,如果未能解决你的问题,请参考以下文章

Linux之Shell脚本实战监控系统的磁盘空间使用率

Linux系统shell脚本之用户管理脚本实战

Linux下Shell脚本实战之监测磁盘空间

Linux系统shell脚本实战之解决生产ddos攻击

Linux之shell脚本实战统计 Linux 进程相关数量信息

Linux之shell脚本实战批量上传docker镜像到华为云容器镜像仓库