Skip to content

数据库基础

MySQL

docker run -d -p 3307:3306 --name mysql5.7 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

ssh连接MySQL数据库

mysql -uroot -p123456

登陆 MySQL 服务后,使用create命令创建数据库

CREATE DATABASE test;

选择指定数据库 use DATABASE;

use test;

创建数据表 CREATE TABLE table_name (column_name column_type);

CREATE TABLE users (
    `id` INT UNSIGNED AUTO_INCREMENT,
    `username` VARCHAR(100) NOT NULL, 
    `password` VARCHAR(100) NOT NULL, 
    PRIMARY KEY ( `id` )
);

插入数据

INSERT INTO users (id,username,password) VALUES (1,"admin","1234567890");

Redis

部署

docker run -d --name redis -p 6379:6379 redis

postgreSql

部署

docker run --name postgres -e POSTGRES_PASSWORD=123456 -p 5432:5432 -d postgres docker exec -it 381 bash

基本语句

进入PostgreSql psql -U postgres

创建数据库 CREATE DATABASE DB1;

查看数据库

\l

切换数据库

\c db1

新建表 create table ffffflllllaaaaagggggg(dkkk CHAR(50));

查看当前数据库存在的表

\d

查看表格信息 \d tablename

插入数据 insert into ffffflllllaaaaagggggg values ('flag{testtesttesttest}');

查询 select * from ffffflllllaaaaagggggg;

CREATE DATABASE fl4g;
\c fl4g;
create table ffffflllllaaaaagggggg(dkkk CHAR(50));
insert into ffffflllllaaaaagggggg values ('flag{testtesttesttest}');

CREATE DATABASE hint;
\c hint;
create table hint(username CHAR(5),ssrf CHAR(50));
insert into hint values ('admin','ffffffllllll444444gg_ssrf.php');
insert into hint values ('aaest','hacker.php');

CREATE DATABASE ctf;
\c ctf
create table ffffflllllaaaaagggggg(dkkk CHAR(50));
create table users(username CHAR(50),password CHAR(50));
insert into users values ('admin','sdjfhedkjvcaekrjf');

MongoDb

docker pull mongo:latest
docker run -itd --name mongo -p 27017:27017 mongo --auth

root创建

mongo

use admin
db.createUser({ user: 'root', pwd: 'xxxxxxxxx', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })

db.auth("root","xxxxxxxxx")

普通用户创建

db.createUser(
  {
    user: "test",
    pwd: "test",
    roles: [ { role: "readWrite", db: "app" }
             ]
  }
)

MongoDB 比较运算符 \$eq\$gt

$eq = (等于)
$gt > (大于)
$gte >= (大于等于)
$lt < (小于)
$lte <= (小于等于)
$ne != (不等于)
$in in (in)
$nin !in (not in)

SQLite

创建一个新的数据库 sqlite3 testDB.db

.databases 命令来检查它是否在数据库列表中 .quit 命令退出 sqlite sqlite3 testDB.db .dump > testDB.sql 导出完整的数据库在一个文本文件 .schema tablename 命令得到表的完整信息 创建表

CREATE TABLE user(
   ID INT PRIMARY KEY     NOT NULL,
   USERNAME           TEXT    NOT NULL,
   PASSWORD        CHAR(50)
);
CREATE TABLE flag(
   flag   TEXT    NOT NULL
);

删除表

DROP TABLE database_name.table_name;

Insert 语句

INSERT INTO TABLE_NAME [(column1, column2, column3,...columnN)]  
VALUES (value1, value2, value3,...valueN);

例如

INSERT INTO user VALUES (1,"admin","admin123");
INSERT INTO user VALUES (2,"dkk","test");
INSERT INTO flag VALUES ("flag{test}");

Select 语句 SELECT * FROM table_name; 例如

sqlite> select * from user;
1|admin|admin123
2|dkk|test
sqlite> select * from flag;
flag{test}

Schema 信息 点命令只在 SQLite 提示符中可用,所以当您进行带有 SQLite 的编程时,您要使用下面的带有 sqlite_master 表的 SELECT 语句来列出所有在数据库中创建的表:

SELECT tbl_name FROM sqlite_master WHERE type = 'table';      //列出所有表
SELECT group_concat(tbl_name) FROM sqlite_master WHERE type = 'table';

SELECT sql FROM sqlite_master WHERE type = 'table' AND tbl_name = 'user';      //查看表的具体信息


Last update: 2021-04-25