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

本文介绍了如何使用Shell脚本与MySQL数据库进行交互,包括数据库的安装、创建、数据导入,以及通过Shell命令进行增删改查操作。详细展示了创建数据库、表以及插入测试数据的过程,并讲解了如何为数据库添加用户并设置权限。最后,演示了使用不同选项查询数据库的示例。
摘要由CSDN通过智能技术生成

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

Shell脚本与MySQL数据库交互(增删改查)

# 环境准备:安装mariadb 数据库

[root@localhost shell]#yum install mariadb mariadb-server mariadb-libs -y

[root@localhost shell]# systemctl start mariadb

[root@localhost shell]# netstat-tnlp |grep :3306tcp0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 102146/mysqld

# 默认没有密码,直接mysql即可进入数据库管理控制台

# 新建数据库

create database school default character set utf8;

# 导入测试数据--建表--学生表

CREATE TABLE student(

s_id varchar(20),

s_name varchar(20) not null default '',

s_birth varchar(20) not null default '',

s_sex varchar(10) not null default '',

primary key(s_id)

);--课程表

create table course(

c_id varchar(20),

c_name varchar(20) not null default '',

t_id varchar(20) not null,

primary key(c_id)

);--教师表

create table teacher(

t_id varchar(20),

t_name varchar(20) not null default '',

primary key(t_id)

);--成绩表

create table score(

s_id varchar(20),

c_id varchar(20),

s_scoreint(3),

primary key(s_id,c_id)

);--插入学生测试数据

insert into student values('1001','zhaolei','1990-1001-1001','male');

insert into student values('1002','lihang','1990-12-21','male');

insert into student values('1003','yanwen','1990-1005-20','male');

insert into student values('1004','hongfei','1990-1008-1006','male');

insert into student values('1005','ligang','1991-12-1001','male');

insert into student values('1006','zhousheng','1992-1003-1001','male');

insert into student values('1007','wangjun','1989-1007-1001','male');

insert into student values('1008','zhoufei','1990-1001-20','male');--课程表测试数据

insert into course values('1001','chinese','1002');

insert into course values('1002','math','1001');

insert into course values('1003','english','1003');--教师表测试数据

insert into teacher values('1001', 'aidisheng');

insert into teacher values('1002', 'aiyinsitan');

insert into teacher values('1003', 'qiansanqiang');--成绩表测试

insert into score values('1001','1001',80);

insert into score values('1001','1002',90);

insert into score values('1001','1003',99);

insert into score values('1002','1001',70);

insert into score values('1002','1002',60);

insert into score values('1002','1003',80);

insert into score values('1003','1001',80);

insert into score values('1003','1002',80);

insert into score values('1003','1003',80);

insert into score values('1004','1001',50);

insert into score values('1004','1002',30);

insert into score values('1004','1003',20);

insert into score values('1005','1001',76);

insert into score values('1005','1002',87);

insert into score values('1006','1001',31);

insert into score values('1006','1002',34);

insert into score values('1007','1001',58);

insert into score values('1007','1002',88);

# 添加指定数据权限的用户

MariaDB [school]> grant all on school.* to dbuser@'%' identified by '123456';

#%默认是不允许localhost登录的,需要单独添加localhost的权限

MariaDB [mysql]> grant all on school.* to dbuser@'localhost' identified by '123456';

[root@localhost~]# mysql -h 10.11.0.215 -u dbuser -p123456

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MariaDB connectionid is 15Server version:5.5.60-MariaDB MariaDB Server

Copyright (c)2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type'help;' or '\h' for help. Type '\c' to clearthe current input statement.

MariaDB [(none)]>mysql的常用选项

[root@localhost~]# mysql -udbuser -p123456 -h10.11.0.215 -D school -e "select * from student;";+------+-----------+----------------+-------+

| s_id | s_name | s_birth | s_sex |

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

| 1001 | zhaolei | 1990-1001-1001 | male |

| 1002 | lihang | 1990-12-21 | male |

| 1003 | yanwen | 1990-1005-20 | male |

| 1004 | hongfei | 1990-1008-1006 | male |

| 1005 | ligang | 1991-12-1001 | male |

| 1006 | zhousheng | 1992-1003-1001 | male |

| 1007 | wangjun | 1989-1007-1001 | male |

| 1008 | zhoufei | 1990-1001-20 | male |

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

[root@localhost~]# mysql -udbuser -p123456 -h10.11.0.215 -B -N -D school -e "select * from student;";1001 zhaolei 1990-1001-1001male1002 lihang 1990-12-21male1003 yanwen 1990-1005-20male1004 hongfei 1990-1008-1006male1005 ligang 1991-12-1001male1006 zhousheng 1992-1003-1001male1007 wangjun 1989-1007-1001male1008 zhoufei 1990-1001-20male

#-E 垂直显示

[root@localhost~]# mysql -udbuser -p123456 -h10.11.0.215 -E -B -N -D school -e "select * from student;";*************************** 1. row ***************************

1001zhaolei1990-1001-1001male*************************** 2. row ***************************

1002lihang1990-12-21male*************************** 3. row ***************************

1003yanwen1990-1005-20male*************************** 4. row ***************************

1004hongfei1990-1008-1006male*************************** 5. row ***************************

1005ligang1991-12-1001male*************************** 6. row ***************************

1006zhousheng1992-1003-1001male*************************** 7. row ***************************

1007wangjun1989-1007-1001male*************************** 8. row ***************************

1008zhoufei1990-1001-20male

#-H 以HTML格式显示

# mysql-udbuser -p123456 -h10.11.0.215 -H -B -N -D school -e "select * from student;";

1001zhaolei1990-1001-1001male
1002lihang1990-12-21male
1003yanwen1990-1005-20male
1004hongfei1990-1008-1006male
1005ligang1991-12-1001male
1006zhousheng1992-1003-1001male
1007wangjun1989-1007-1001male
1008zhoufei1990-1001-20male
5e406b93d604d65e2b0012025bf4d9a5.png

#-X 以xml格式显示

[root@localhost~]# mysql -udbuser -p123456 -h10.11.0.215 -H -B -N -D school -e "select * from student;" >result.html

[root@localhost~]# mysql -udbuser -p123456 -h10.11.0.215 -X -B -N -D school -e "select * from student;" > result.xml

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值