lr mysql增删改查脚本_shell脚本--脚本与MySQL数据库交互(增删改查)

student.sql 下载

mysql 命令参数详解

-u 用户名

-p 密码

-h 服务器ip

-D 连接的数据库

-N 不输出列信息

-B 使用tab键代替默认交互分隔符

-e 执行sql语句

其他选项

-E 垂直输出

-H 以HTML格式输出

-X 以XML格式输出

1.安装服务

apt-get install mariadb-server

2.启用启动服务

sudo systemctl start mariadb

sudo systemctl enable mariad

3.后续设置命令

sudo mysql_secure_installation

root身份登录

$ sudo mysql

创建school数据库

create database school default character set utf8;

创建低权限的用户

grant all on school.* to chencl@'%' identified by '123456';

创建低权限的用户

grant all on school.* to chencl@'localhost' identified by '123456';

使用创建的低权限登录数据库

mysql -uchencl -p123456 -h localhost

导入数据到school数据库中

$ mysql -uchencl -p123456 -h localhost school < /home/chencl/桌面/student.sql

登录数据库

$ mysql -uchencl -p123456 -h localhost

显示所有的数据库

show databases;

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

| Database |

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

| information_schema |

| school |

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

2 rows in set (0.00 sec)

切换到school库中

use school;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

查询数据库school中的所有的表

show tables;

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

| Tables_in_school |

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

| course |

| score |

| student |

| teacher |

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

4 rows in set (0.00 sec)

查询 course 表的所有内容

select * from course;

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

| c_id | c_name | t_id |

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

| 1001 | chinese | 1002 |

| 1002 | math | 1001 |

| 1003 | english | 1003 |

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

3 rows in set (0.00 sec)

查询表的描述 表结构

describe course;

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

| Field | Type | Null | Key | Default | Extra |

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

| c_id | varchar(20) | NO | PRI | NULL | |

| c_name | varchar(20) | NO | | | |

| t_id | varchar(20) | NO | | NULL | |

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

3 rows in set (0.00 sec)

mysql的常用选项:

-E 垂直显示

mysql -uchencl -p123456 -h localhost -E -B -N -D school -e "select * from student;";

*************************** 1. row ***************************

1001

zhaolei

1990-1001-1001

male

-H 以HTML格式显示

mysql -uchencl -p123456 -h localhost -H -B -N -D school -e "select * from student;" > result.html

8b07f7d9731ba27f2aa16c0096f34306.png

-X 为XML文件格式

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

1001

zhaolei

1990-1001-1001

male

编写脚本处理SQL数据  opera.sh

sh opera.sh "select * from score" school

sh opera.sh "insert into score values('1020','1002','100')" school

opera.sh 内容

#!/bin/bash

user="chencl"

password="123456"

host="localhost"

SQL="$1"

DBNAME="$2"

mysql -u"$user" -p"$password" -h"$host" -D "$DBNAME" -B -e "$SQL"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值