安装mariadb 数据库  (默认没有密码,直接mysql即可进入数据库管理控制台)

yum install mariadb mariadb-server mariadb-libs -y
systemctl start mariadb
netstat -tnlp |grep :3306

新建数据库

create database school default character set utf8;
CREATE TABLE student(
s_id varchar(),
s_name varchar() not null default '',
s_birth varchar() not null default '',
s_sex varchar() not null default '',
primary key(s_id)
); --课程表
create table course(
c_id varchar(),
c_name varchar() not null default '',
t_id varchar() not null,
primary key(c_id)
); --教师表
create table teacher(
t_id varchar(),
t_name varchar() not null default '',
primary key(t_id)
); --成绩表
create table score(
s_id varchar(),
c_id varchar(),
s_score int(),
primary key(s_id,c_id)
); --插入学生测试数据
insert into student values('','zhaolei','1990-1001-1001','male');
insert into student values('','lihang','1990-12-21','male');
insert into student values('','yanwen','1990-1005-20','male');
insert into student values('','hongfei','1990-1008-1006','male');
insert into student values('','ligang','1991-12-1001','male');
insert into student values('','zhousheng','1992-1003-1001','male');
insert into student values('','wangjun','1989-1007-1001','male');
insert into student values('','zhoufei','1990-1001-20','male'); --课程表测试数据
insert into course values('','chinese','');
insert into course values('','math','');
insert into course values('','english',''); --教师表测试数据
insert into teacher values('', 'aidisheng');
insert into teacher values('', 'aiyinsitan');
insert into teacher values('', 'qiansanqiang'); --成绩表测试
insert into score values('','',);
insert into score values('','',);
insert into score values('','',); insert into score values('','',);
insert into score values('','',);
insert into score values('','',); insert into score values('','',);
insert into score values('','',);
insert into score values('','',); insert into score values('','',);
insert into score values('','',);
insert into score values('','',); insert into score values('','',);
insert into score values('','',); insert into score values('','',);
insert into score values('','',); insert into score values('','',);
insert into score values('','',);

school.sql

导入数据

mysql school < school.sql

mysql 常用选项

mysql命令参数详解

    -u    用户名
-p 用户密码
-h 服务器ip地址
-D 连接的数据库
-N 不输出列信息
-B 使用tab键代替默认交互分隔符
-e 执行sql语句 其他选项
-E 垂直输出
-H 以HTML格式输出
-X 以XML格式输出

-D 指定数据库  -e 不用交互模式

 mysql -D school -e "select * from student;";

-N 不显示列信息,-B 去除多余信息, -D 指定操作的数据库

mysql -B -N -D school -e "select * from student;";

-E 垂直显示

mysql -E -B -N -D school -e "select * from student;";

-H 以HTML格式显示

mysql -H -B -N -D school -e "select * from student;";

-X 以xml格式显示

mysql  -X -B -N -D school -e "select * from student;" > result.xml

在 shell 脚本中操作mysql 数据库  

operate_mysql.sh

#!/bin/bash
# user="dbuser"
password="123456"
host="10.11.0.215"
db_name="$1" SQL="$2" mysql -h"$host" -u"$user" -p"$password" -D"$1" -B -e "$SQL"

执行 sql 语句

sh operate_mysql.sh school "select * from score"

插入数据

sh operate_mysql.sh school "insert into score values('1020','1002','100');"
sh operate_mysql.sh school "select * from score"

导出txt文本,-B去掉多余的符号可以导入到excel表格中

sh operate_mysql.sh school "select * from score" > result.txt

利用shell脚本将文本数据导入到mysql中

创建表结构和student一样结构的student1表

 create table student1 like student;

data.txt

1010    jerry    1991-12-13    male
1011 mike 1991-12-13 female
1012 tracy 1991-12-13 male
1013 kobe 1991-12-13 male
1014 allen 1991-12-13 female
1015 curry 1991-12-13 male
1016 tom 1991-12-13 female

编写导入数据脚本 import_mysql.sh

    #!/bin/bash
# user="dbuser"
password="123456"
host="10.11.0.215" mysql_conn="mysql -h"$host" -u"$user" -p"$password"" cat data.txt | while read id name birth sex
do
$mysql_conn -e "INSERT INTO school.student1 values('$id','$name','$birth','$sex')"
done

 

执行脚本

sh import_mysql.sh

导入数据可以用load,有时候有一些特殊需求比如插入s_id大于1014的行,这个时候就需要使用 shell 语句进行过滤了

import_mysql.sh

    #!/bin/bash
# user="dbuser"
password="123456"
host="10.11.0.215" mysql_conn="mysql -h"$host" -u"$user" -p"$password"" cat data.txt | while read id name birth sex
do
# 有插入条件
if [ $id -gt 1014 ];then
$mysql_conn -e "INSERT INTO school.student1 values('$id','$name','$birth','$sex')"
fi
done

执行脚本

sh import_mysql.sh
sh operate_mysql.sh school "select * from student1"

需求2:

data2.txt

2021|hao|1989-12-21|male
2022|zhang|1989-12-21|male
2023|ouyang|1989-12-21|male
2024|li|1989-12-21|female

cat import_mysql-2.sh

    #!/bin/bash
# user="dbuser"
password="123456"
host="10.11.0.215" # IFS是系统自带的变量,分隔符 input filre saperator
IFS="|" cat data2.txt | while read id name birth sex
do
# 注意,当使用|类似这种特殊符号时,需要将mysql命令不写成命令,否则会报错
mysql -u"$user" -p"$password" -h"$host" -e "INSERT INTO school.student2 values('$id','$name','$birth','$sex')"
done
# # 使用冒号: 分隔也没有问题

  

执行脚本  

sh import_mysql-2.sh
sh operate_mysql.sh school "select * from student2"

data3.txt

2025:hao:1989-12-21:male
2026:zhang:1989-12-21:male
2027:ouyang:1989-12-21:male
2028:li:1989-12-21:female 

import_mysql-2.sh

    #!/bin/bash
# user="dbuser"
password="123456"
host="10.11.0.215" #mysql_conn="mysql -h"$host" -u"$user" -p"$password"" # IFS是系统自带的变量,分隔符 input filre saperator
IFS=":" cat data3.txt | while read id name birth sex
do
mysql -u"$user" -p"$password" -h"$host" -e "INSERT INTO school.student2 values('$id','$name','$birth','$sex')"
done

执行脚本

sh import_mysql-2.sh
sh operate_mysql.sh school "select * from student2"

  

Shell 脚本操作数据库实战的更多相关文章

  1. shell脚本操作数据库

    #!/bin/bash HOST_NAME="localhost" PORT=3306 USERNAME="root" PASSWORD="root& ...

  2. shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中

    shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中 利用shell脚本将文本数据导入到mysql中 需求1:处理文本中的数据,将文本中的数据插入到mys ...

  3. shell编程系列22--shell操作数据库实战之shell脚本与MySQL数据库交互(增删改查)

    shell编程系列22--shell操作数据库实战之shell脚本与MySQL数据库交互(增删改查) Shell脚本与MySQL数据库交互(增删改查) # 环境准备:安装mariadb 数据库 [ro ...

  4. shell编程系列25--shell操作数据库实战之备份MySQL数据,并通过FTP将其传输到远端主机

    shell编程系列25--shell操作数据库实战之备份MySQL数据,并通过FTP将其传输到远端主机 备份mysql中的库或者表 mysqldump 常用参数详解: -u 用户名 -p 密码 -h ...

  5. shell编程系列23--shell操作数据库实战之mysql命令参数详解

    shell编程系列23--shell操作数据库实战之mysql命令参数详解 mysql命令参数详解 -u 用户名 -p 用户密码 -h 服务器ip地址 -D 连接的数据库 -N 不输出列信息 -B 使 ...

  6. shell脚本操作mysql数据库

    shell脚本操作mysql数据库,使用mysql的-e参数可以执行各种sql的(创建,删除,增,删,改.查)等各种操作 mysql  -hhostname -Pport -uusername -pp ...

  7. shell 脚本操作informix数据库

    shell 脚本操作informix数据库的简单模板: functionName(){ dbaccess << ! database 库名; sql语句; ! } 栗子1:更新数据 fun ...

  8. shell脚本操作mysql库

    shell脚本操作mysql数据库-e参数执行各种sql(指定到处编码--default-character-set=utf8 -s,去掉第一行的字段名称信息-N) 2011-05-11 18:18: ...

  9. Saiku数据库迁移后的刷新脚本-Shell脚本读取数据库中的数据(二十三)

    Saiku数据库迁移后的刷新脚本 之前有谈过对saiku中的数据进行刷新,因为saiku默认会从缓存中查询数据,但是配置不使用缓存又会效率低下... 所以这里就需要做一个数据刷新,每次ETL之后都需要 ...

随机推荐

  1. IDEA中spring约束文件报红 的解决办法

  2. Spring cloud微服务安全实战-6-9sentinel之熔断降级

    来讲一下降级规则 服务会互相调用,服务A会有一些服务之间的依赖. 假设服务D的响应时间变长了.A调用D就会卡住了. 熔断,某一个服务出现问题,会把服务拖死.如果A出现,会把依赖A的那些服务拖死. 主要 ...

  3. Qt编写气体安全管理系统5-数据监控

    一.前言 本项目对设备的监控有四种视图模式,可以任意切换,数据监控.地图监控.设备监控.曲线监控,其中数据监控是最常用的,所以在主界面导航中也排在第一位,综合观察分析了很多气体安全或者组态监控软件,大 ...

  4. matlab基本函数inf, isempty, round, floor, fix

    一起来学演化计算-matlab基本函数inf, isempty, round, floor ,fix 觉得有用的话,欢迎一起讨论相互学习~Follow Me inf matlab中 inf无穷大量+∞ ...

  5. Python - Django - 上传文件

    upload.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  6. prometheus数据格式

    注意区分以下两种“数据格式”: 1.自定义exporter的时候所需要遵循的给prometheus提供数据的数据格式: https://yunlzheng.gitbook.io/prometheus- ...

  7. Yii2打印原始sql语句

    $query = User::find() ->where(['id'=>[1,2,3,4]) ->select(['username']) // get the AR raw sq ...

  8. mysql数据库建表授权操作

    1.create schema [数据库名称] default character set utf8 collate utf8_general_ci;--创建数据库 采用create schema和c ...

  9. Convolutional neural network (CNN) - Pytorch版

    import torch import torch.nn as nn import torchvision import torchvision.transforms as transforms # ...

  10. 015 Android md5密码加密及其工具类

    1.md5加密介绍 MD5算法是广泛使用的杂凑函数,也就是哈希函数,英文全拼是:Message Digest Algorithm,对应的中文名字是消息摘要算法. MD5加密:将字符串转换成 32位的字 ...