mysql常用函数和变量
use mysql;
alter user “root”@“localhost” identified by “123”;
use mysql;
alter user “root”@“localhost” identified by “123”;
flush privileges;
SET PASSWORD Statement MySQL 5.7
MySQL 8.0 CREATE USER Statement
mysqladmin.exe -uroot -p原密码 password “新密码”
SET PASSWORD FOR ‘jeffrey’@‘localhost’ = ‘auth_string’;(高版本)
知道原密码的情况下:
低版本:
update mysql.user set password=password(‘新密码’) where user=’root’ and host=’127.0.0.1’; flush privileges; exit
高版本:
use mysql;
alter user "root"@"localhost" identified by "123";
flush privileges;
alter user "root"@"%" identified by "pentestroot";
注意:root用户对应的主机,
不知道密码的情况下:
1、找到mysql的配置文件 my.ini或者my.cnf注意系统之间的区别
2、打开该配置文件,在【mysqld】下面添加:skip-grant-tables
3、开启mysql服务,免密登录,修改密码
4、退出,删掉配置文件中skip-grant-tables
5、正常使用数据库~~~
添加账号并授权
create user “用户”@“主机IP” identified by “密码”;
主机IP:限源访问,mysql只能让指定的IP来访问
%:没有限制
grant 权限 on 库名.表名 to "用户"@"主机IP"
权限:insert drop update select all
grant insert,drop on *.* to "icq"@"192.168.179.100";
grant all on *.* to "icq"@"192.168.179.100";
数据库外连
知道原密码的情况下:
低版本:
update mysql.user set password=password(‘新密码’) where user=’root’ and host=’127.0.0.1’; flush privileges; exit
高版本:
use mysql;
alter user "root"@"localhost" identified by "123";
flush privileges;
alter user "root"@"%" identified by "pentestroot";
注意:root用户对应的主机,
不知道密码的情况下:
1、找到mysql的配置文件 my.ini或者my.cnf注意系统之间的区别
2、打开该配置文件,在【mysqld】下面添加:skip-grant-tables
3、开启mysql服务,免密登录,修改密码
4、退出,删掉配置文件中skip-grant-tables
5、正常使用数据库~~~
添加账号并授权
create user “用户”@“主机IP” identified by “密码”;
主机IP:限源访问,mysql只能让指定的IP来访问
%:没有限制
grant 权限 on 库名.表名 to "用户"@"主机IP"
权限:insert drop update select all
grant insert,drop on *.* to "icq"@"192.168.179.100";
grant all on *.* to "icq"@"192.168.179.100";
数据库外连
update user set host="%" where user=“root” and host=“localhost”
update user set host="%" where user=“icq” and host=“192.168.179.100”
flush privileges;
重启数据库服务器
修改配置文件,将bind-address 127.0.0.1 注释掉
select version();
select user();查看当前用户
select @@version;
select @@hostname;主机名
select @@tmpdir;查看临时目录
select @@basedir;数据库服务所在的位置
select @@datadir;数据库文件所在位置
H:\phpStudy\PHPTutorial\MySQL\data存放的文件的类型:
MYD:存放数据的地方
FRM:存储表结构
MYI:存储当前数据的配置信息和索引信息
select mid(字符串,开始位置,结束位置);截取字符串
select substr(“123”,1,2);同上
select substring(“123”,1,2);同上
select ord();显示ASCII
select concat(“123”,“456”);拼接字符串
select sleep(5);让数据库等待5s
select concat_ws(分隔符,字符串1,字符串2~~~~~);
select group_concat();分组并拼接
数据类型
int
整型
bigint
大整型
float
浮点型
date
日期
time
时间
char
字符
varchar
不定长字符(可变字符/边长字符)
text
文本
double
双精度浮点数
bit
比特
tinyint
0/1
创建表:
create table 表名(字段1 数据类型, 字段2 数据类型, 字段3 数据类型);
主键:在表中能够代表某个事物的身份对应的字段。主键是唯一的。
create table stu(id primary key auto increasement not null, name varchar(50),phoneNumber bigint(11),gender tinyint) DEFAULT CHARSET=utf8;
在创建表的时候给出的数据类型要有长度。
CREATE TABLE stu(
stuid bigint(20),
stuname VARCHAR(50),
stuClass VARCHAR(20),
gender TINYINT
);
CREATE TABLE teacher(
uid int(10),
tename VARCHAR(50),
teachClass VARCHAR(50),
gender TINYINT
);
约束
primary key
主键
CREATE TABLE demo(
id int,
pname VARCHAR(50),
cname VARCHAR(100),
PRIMARY KEY (id) -- 给id设置主键
);
auto_increment
自增
not null
不为空
unique
唯一标识数据库每个记录
CREATE TABLE demo1(
id int not null auto_increment, -- id不为空,且自增
pname VARCHAR(50),
cname VARCHAR(100),
PRIMARY KEY (id) -- 给id设置主键
UNIQUE (id)
);
注意要把约束放在数据类型后面
后续修改添加主键
ALTER TABLE stu add PRIMARY KEY (stuid);
使用表:
use table_name;
insert into stu(stuid, stuname, stuClass, gender) value(201602731,"demo","class-1",1);
查询数据
select 字段1,字段2 from 表名;
select 字段1,字段2,字段3 from 表名 where 字段名=指定值; (有条件)
select 字段1,字段2,字段3 from 表名 where 字段名=指定值 and 字段名2=指定值2; (有条件)
SELECT stuname from stu where gender=0 or stuClass="class-1";
限制查询
注意,limit a, b中a最大不超过表的记录数。
select * from stu LIMIT 1,1;
limit a, b 表示从第a行开始,选择b行的数据出来
limit b 表示从第0行开始,选择出b行的数据
更新
update 表名 set 字段1=新值,字段2=新值
(更新所有的字段)
update
表示 set 字段1=新值 where 条件;
UPDATE stu set stuname = "hacker" WHERE stuid="201503648";
drop table 表名;
delete from table; 删除表中所有内容
模糊查询
like
通配符:%
任意字符,_
单个字符
select * from stu where name like "hack%"
;
SELECT * FROM stu WHERE stuname LIKE "hack__";
联合查询
查询1 union 查询2
注意:两个查询出来的结果字段必须一样,否则报错
SELECT * FROM stu WHERE stuname ="hacker" union select 1,2,3,4;
SELECT * FROM stu WHERE stuname ="hacker" union select 1,2,3;
SELECT * FROM stu WHERE stuname ="hacker" union select 1,2,3 1222 - The used SELECT statements have a different number of columns 时间: 0.001s
select stuid, stuname FROM stu union select pname, cname from midori;
删除字段
alter table 表名 drop 字段名
ALTER TABLE stu drop gender;
添加字段
alter table 表名 add 字段名 数据类型
ALTER TABLE stu ADD xingbie int;
修改表名
alter table 原表名 rename to 新表名;
PHP连接mysql的方式
-
mysql (PHP<=5.5) 2012年开始不建议使用该扩展,安全性差
-
mysqli 对mysql的升级版,安全性高,只能用于操作mysql数据库
- 面向对象的使用方式
- 面向过程的使用方式
-
PDO (PHP data object)
目前支持连接的数据库类型有12中,安全性高,基于面向对象的使用方法。