文章目录
一、数据库的连接
数据库的连接 :
1.密文连接
mysql -uroot -p 回车后
输入密码 建议使用
2.明文连接
mysql -uroot -p密码 不推荐使用,后期入侵查看日志可发现密码
3.远程连接
mysql -h想登陆的数据库的IP地址 -uroot -p 回车后输入密码
二、用户管理
1.创建用户
查看mysql数据库通过哪些客户端主机登录 (查看用户)
use mysql
select user,host,authentication_string from user;
localhost与127.0.0.1在mysql看来是俩个
方法一:
create user ‘用户名’@‘localhost(允许登录的ip)’ identified by ‘密码’;
方法二:插入
insert into mysql.user (user,host,password) values(‘用户名’,‘ip地址’,password(‘密码’));
查看严格模式 show variables like ‘%sql_mode%’;
缺陷:如果当前mysql数据库的sql-mode设置了严格模式(如TRADITIONAL/STRICT_TRANS_TABLES),则无法使用方法二插入数据。
方法三:赋值权限的时候会创建用户
grant all on security.* to ‘用户名’@‘localhost’ identified by ‘密码’;
2.删除用户
方法一:使用delete语句删
delete from mysql.usr where user=‘用户名’;
delete from mysql.usr where user=‘用户名’ and host=‘地址’;
上例中,使用了delete语句删除user表中的用户对应的记录,也就相当于删除了用户,但是,如果某些数据库的权限给了这个用户,那么这mysql.db表中,这个用户对应的权限将不会被删除,当管理员想再次创建这个用户时,会出现问题,如果管理员想要使用create user命令再次创建这个用户时,会出现如下问题:
用户删除了但是权限没有删除,权限存放在mysql.db文件中,查看权限的存放位置 select * from mysql.db limit 1 \G
解决办法一
打开权限存放表,把开启的权限删除
delete from mysql.db where user =‘用户’ 删除权限后如何执行创建
create user ‘用户’@'地址’即可
解决办法二
使用grant命令授权这个用户能够访问任意数据库即可
grant all privileges on * .* ‘用户’@'地址’赋值给这个用户所有数据库权限,将原来的权限覆盖掉
然后执行flush privileges 刷新权限
然后再创建用户 create user ‘用户’@‘地址’
方法二:使用drop
使用drop user命令删除对应用户,用户与相应权限也将删除
drop user ‘用户名’@‘地址’;
3.重命名用户
rename user oldname to newname;
三、密码管理
方法一:
管路员可以在系统命令行中使用MySQLadmin命令设置mysql用户的密码
进入php/mysql/bin下 进入d盘D: cd :
1.mysqladmin -u用户名 -p旧密码 password =password(‘新密码’)
2.mysqladmin.exe -u用户名 -p旧密码 password 新密码
方法二:
使用root用户登录后 使用 set password命令来设置用户密码
mysql -uroot -p
set password for ‘用户’@‘地址’ = password(‘新密码’);
方法三:
在使用mysql5.6之前版本可以用下列方法修改
登录root后
update mysql.user set password = password(‘密码’)where user=‘用户名’ and host=‘地址’;修改完成后刷新权限
flush privileges;
5.7之后 方法一样把password改为authentication_string
update mysql.user set authentication_string = password(‘密码’)where user=‘用户名’ and host=‘地址’;修改完成后刷新权限
flush privileges;
方法四:
上个博客说到了 绕过的思想
四、授权管理
grant all(增删改查权限) privileges on 数据库.*(数据库权限) to ‘用户名’@‘地址’ identified by ‘密码’ all为增删改查的权限 *
给远程用户授权(一定不给mysql远程登录的权限)
grant all privileges on 数据库.* to’用户名’@’%’ identified by ‘密码’
%为远程登录
我们可以缩小用户能够连接数据库的IP地址范围,
grant all privileges on security.* to ‘bobo’@‘192.168.%.%’ identified by ‘密码’;
授权查询权限
grant select privileges on security.* to ‘xiaobobo’@‘localhost’ identified by ‘密码’
一个库授予多个用户
grant all on security.* to xiaobobo@localhost,bobo@127.0.0.0
只对每张表的某个字段授权
grant select (name,age) on security.students to xiaobobo@localhost;
管理员可以对用户进行一些其他的限制
max_queries_per_hour :限制用户每小时执行的查询语句数量
例:grant select on xxx.* to xiaobobo@‘192.168.%.%’ identified by ‘密码’ with max_queries_per_hour 20;
查看授权
1.show grants for ‘用户’@‘地址’;
2.权限在mysql.db中 必须要root
select * from mysql.db where db=‘数据库’\G
删除授权
revoke 要移除的权限 on 数据库.表 from 用户@地址;
五、创建数据库(重点字符集)
create database xiaobobo;创建小博博数据库
create database if not exists xiaobobo character set utf8mb4; 如果名为小博博的数据库不存在,则创建,并且设置字符集为utf8mb4
show create database xiaobobo; 查看创建数据库
status 查看数据库与使用的数据库的关系
先使用一张表 use 然后查看表中的表名 show tables;然后查询select
六、修改数据库
查看字符集 show variables like ‘%character%’;
alter database xiaobobo default character set utf8mb4;
七、删除数据库
drop database xiaobobo;删除小博博数据库
八、创建表(学生表)
desc 表名; 显示表的结构
创建表
create table student (
id int(10) unsigned(无符号) not null primary key(主键) auto_increment(自增),
name varchar(50) not null comment ‘学生姓名’, comment注释作用
sgender char(2) default “M” check(sgender in(“M”,“F”)), check为约束条件
address varchar(100) not null
)ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4;
check() check函数只在8.0有效 5.7和5.5 无效
显示创表语句
show create table student;
九、删除表
drop table student;
truncate student;清空表中内容
十、修改表
添加字段
alter table student add column age int;在学生表中添加age列
alter table student add column age int not null default 0; 不能为空默认为0
alter table student add age int after name(first); 在age后面添加年龄字段(添加到第一个)
删除字段
alter table student drop age;在学生表中删除age字段
修改字段
俩种语法modify与change
change需要写俩次
alter table student change age age char(10);
modify不能用于修改字段名称
alter table student modfiy age int(10);