目录
[1.7.忘记root 密码并找回](#1.7.忘记root 密码并找回)
[2.1.all privilege包含的权限](#2.1.all privilege包含的权限)
一.用户管理
1.1.新建用户
格式:
create user '用户名'@'来源地址' [identified by [passwd] '密码'];
用户名:指定将创建的用户名;
来源地址:指定新创建的用户可在哪些主机上登录,可使用IP地址、网段、主机名的形式,本地用户可用localhost,允许任意主机登录可用通配符%
密码:
- 若使用明文密码,直接输入'密码',插入到数据库时由Mysql自动加密;
- 若使用加密密码,需要先使用SELECT PASSWORD('密码'); 获取密文,再在语句中添加 PASSWORD '密文';
- 若省略"IDENTIFIED BY"部分,则用户的密码将为空(不建议使用)
明文密码的创建:
#创建用户zhuzi只允许从本机登录密码为123456
create user 'zhangrui'@'localhost' identified by '123456';
#登录测试
mysql -u zhangrui -p123456


加密创建:
#获取密文
select password('123456');
#创建用户时密文复制粘贴过来
create user 'shen'@'localhost' identified by password '密文';
create user 'shen'@'localhost' identified by password '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9';
#登录验证
mysql -u shen -p123456


1.2.查看用户
用户数据位于mysql库的user表中
#切换库
use mysql;
#查看user表中的用户信息:指定字段用户,登录认证字符串,登录主机,查看数据记录
select User,authentication_string,Host from user;

1.3.重命名用户rename
格式:
rename user '旧用户名'@'登录主机' to '新用户名'@'登录主机';
示例:
#将用户shen重命名为chao
rename user 'shen'@'localhost' to 'chao'@'localhost';
#查看用户信息验证
use mysql;
select User,authentication_string,Host from user;

1.4.删除用户
格式:
drop user '用户名'@'localhost';
示例:
#删除用户chao
drop user 'chao'@'localhost';
use mysql
#切换数据库
#验证查看
select User,authentication_string,Host from user;

1.5.修改当前登录用户密码
#原密码为123456 修改为369369
set password = password('369369');

1.6.修改其他用户密码
格式:
set password for '用户名'@'localhost' = password('新密码');
示例:
#修改用户zhangrui密码由123456改为abc123
set password for 'zhangrui'@'localhost' = password('abc123');

1.7.忘记root 密码并找回
找回root密码
1)修改配置文件,在[mysqld]下添加skip-grant-tables
2)重启mysqld服务,使用 mysql 命令直接进入免密进入mysql
3)执行 update mysql.user set authentication_string=password('....') where user='root';
4)还原配置文件,重启服务,使用账号密码验证登录
vim /etc/my.conf
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=4096
default-storage-engine=INNODB
max_allowed_packet=32M
server-id = 1
skip-grant-tables
systemctl restart mysqld




注意:修改密码尽量选择字母和数字组合这种比较复杂的密码,因为太简单的密码不会修改成功
二.数据库用户授权
2.1.all privilege包含的权限
all privilege 权限如下:
insert(插入数据)
select(查询数据)
update(更新表的数据)
delete(删除表中数据)
create(创建库,表)
drop(删除库,表)
refemces
index(建立索引)
alter(更改表属性)
create temp orary tables
lock tables(锁表)
execute
create view(创建视图)
show view(显示视图)
create routine(创建存储过程)
alter routine(修改存储过程)e
vent(手件)
tigger on(创建触发器)
2.2.授予权限
grant语句:专门用来设置数据库用户的访问权限
-
当指定的用户名不存在时,grant语句将会创建新的用户
-
当指定的用户名存在时,grant语句用于修改用户信息
格式:
grant 权限列表 on 数据库名.表名 to '用户名'@'来源地址' [identified by '密码'];
- 权限列表:用于列出授权使用的各种数据库操作,以逗号进行分隔,如"select,insert,update"。使用"all"表示所有权限,可授权执行任何操作。
- 数据库名.表名:用于指定授权操作的数据库和表的名称,其中可以使用通配符""。例如,使用"kgc."表示授权操作的对象为 kgc数据库中的所有表。
- '用户名@来源地址':用于指定用户名称和允许访问的客户机地址,即谁能连接、能从哪里连接。来源地址可以是域名、IP 地址,还可以使用"%"通配符,表示某个区域或网段内的所有地址,如"%.accp.com"、"192.168.79.%"等。
- identified by:用于设置用户连接数据库时所使用的密码字符串。在新建用户时,若省略"IDENTIFIED BY"部分, 则用户的密码将为空。
①允许指定用户查询指定数据库表
允许用户zhangrui在本地查询test数据库中所有表的数据记录,但禁止查询其他数据库中的表的记录
#使用root用户登入数据库
mysql -u root -p369369
#授权*表示库下的所有表
grant select on test.* to 'zhangrui'@'localhost' identified by 'abc123';
#退出数据库
quit
#使用zhuo用户登入
mysql -u zhangrui -pabc123
#查看所有库
show databases;
#切换库
use test;
#查看所有表
show tables;



②允许用户在所有终端远程连接并拥有所有权限
允许用户zhuo在所有终端远程连接mysql,并拥有所有权限
格式:
grant all on *.* to '用户名'@'%' identified by '用户密码';
示例:
grant all on *.* to 'zhangrui'@'%' identified by 'abc123';

2.3.查看权限
格式:
show grants for 用户名@来源地址;
示例:
show grants for 'zhangrui'@'localhost';

2.4.撤销权限
#撤销所有权限格式:
revoke all on *.* from '用户名'@'来源地址';
#撤销test库权限格式:
revoke select on test.* from '用户名'@'来源地址';
示例:撤销用户zhangrui的所有权限
#查看用户zhuo的权限
show grants for 'zhangrui'@'localhost';
#撤销test库权限格式:
revoke select on test.* from 'zhangrui'@'localhost';
#撤销用户zhuo所有主机登录的所有权限
revoke all on *.* from 'zhangrui'@'%';
