#CREATE USER 'admin'@'localhost' IDENTIFIED BY '123456';
可以查看创建账户状态
#show create user admin@'localhost';
赋予权限 all所有权限增删改查 第一个*是数据库级授权 ,第二个*是表级授权,只能本地登陆
#grant all privileges on *.* to 'admin'@'localhost';
可以查看用户赋予的特权
#show grants for 'admin'@'localhost';
查询当前登陆用户名
#select current_user();
赋予远程连接权限blog数据库中blog用户
#grant all privileges on blog.* to 'blog'@'%';
取消远程连接权限blog数据库中blog用户
#revoke all on blog.* from 'blog'@'%';
创建一个用户admin2只能在192.168.31段网络远程访问密码[Mima123];
#create user 'admin2'@'192.168.31.%' identified by '[Mima123]';
赋予权限并创建用户
#grant all on blog.* to 'blog2'@'192.168.31.%' identified by '[Mima123]';
privileges权限有
select,(常用)
insert,(常用)
update,(常用)
delete,(常用)
create,(常用)
drop, (常用)
references,
index,
alter,
create temporary tables,
lock tables,
execute,
create view,
show view,
create routine,
alter routine,
event,
trigger
Example 数据库创建、用户创建赋予权限操作
#create database test_example; #创建数据库
#use test_example; #进入数据库
#create table t1(id int, user varchar(12),pwd varchar(12)); #创建表t1
#create table t2(id int, name varchar(12),phone varchar(12)); #创建表t2
#desc t1; desc t2; #查看表信息
####grant all on test_example.* to 'test1'@'192.168.31.179' identified by '[Mima123]'; #创建用户test1密码[Mima123],并赋予在192.168.31.179远程连接test_example数据库所有表的权限
#CREATE USER 'test1'@'192.168.31.179' IDENTIFIED BY '[Mima123]'; #用这个比较好
#select user,host from mysql.user;
#show grants for 'test1'@'192.168.31.179'; #显示用户权限
#revoke all on test_example.* from 'test1'@'192.168.31.179'; #移除权限
赋予test1在192.168.31.179主机连接数据库test_example中t1表select、update权限,但在插入的时候只能插入id、user字段(插入完成后可以更新pwd字段)
#grant select,insert(id,user),update on test_example.t1 to 'test1'@'192.168.31.179';
赋予test1在192.168.31.179主机连接数据库test_example中t2表select、update权限,但在插入的时候只能插入id、phone字段(插入完成后可以更新name字段)
#grant select,insert(id,phone),update,delete on test_example.t2 to 'test1'@'192.168.31.179';
Example end