需求: 我想要一个mysql 用户 此用户对某库某表有权限控制 ,此用户可以远程连接操作。
分析: 首先要有一个用户: 用户创建 -> 用户删除。
如何给用户权限: 添加权限-> 撤消权限-> 查看权限
实现:
1.用户创建: 两种方式
(1) mysql.user表保存的是用户登录信息,插表法:直接添加无权限
insert into mysql.user(host,user,password) values('%','test',PASSWORD('pwd'));
(2) 命令方式 添加并赋权
grant select on 数据库.表 to '用户名'@'登录主机' identified by '密码' 注:数据库.表 可用通配符*.* 登录主机 可以通配符%
2.用户删除
delete from mysql.user where user='' and host='';
3.用户密码修改:
update mysql.user set password=PASSWORD('111111') where user='root';
4.添加权限
grant 权限 on 数据库.表 to '用户名'@'登录主机';
权限: select ,update,delete,insert(表数据)、create,alert,drop(表结构)、references(外键)、create temporary tables(创建临时表)、index(操作索引)、create view,show view(视图)、create routine,alert routine,execute(存储过程)、all,all privileges(所有权限)
数据库:数据库名或者*(所有数据库)
表:表名或者*(某数据库下所有表)
主机:主机名或者%(任何其他主机)
例:grant selec,insert,update,delete on *.* to 'test'@'%';
5.撤消权限
revoke 权限 on 数据库.表 from '用户名'@'登录主机';//将to改为from
例:revoke all on *.* from ‘test’@’%’;
6.查看权限
show grants for test@localhost;//@localhost指定用户指定host 也可以不指定
7.远程访问控制
use mysql;
update db set host = '%' where user = '用户名'; (如果写成 host=localhost 那此用户就不具有远程访问权限)
flush privileges;
grant all privileges on *.* to 'myuser'@'%' identified by 'mypassword' with grant option;
8.创建远程访问用户
//grant usage on *.* to 创建的用户名 @"%" identified by "密码"; 无权限
//grant all privileges on *.* to 创建的用户名 @"%" 赋权限
grant all privileges on *.* to 创建的用户名 @"%" identified by "密码";
flush privileges; * 刷新刚才的内容*
host格式: 172.17.% 172.17.11.% 可用通配符%
使用用户
用户登录
格式: mysql -h主机地址 -u用户名 -p用户密码
mysql -h10.10.10.110 -uroot -p123
用户退出
exit quit