mysql -h localhost -u User -p passWord DataBase -e "sql"
# -h 连接服务器ip
# -u 用户
# -p 密码
# DataBase 连接数据库
# -e 执行sql
1. 新建用户
① CREATE USER
create user 'userName'@'localhost' identified by 'passWord';
-- 如果只指定用户名部分,localhost 则默认为‘%’,即为对所有的主机开放权限
-- passWord 为明文密码,不指定则登录无需密码
② 插入user表新增用户
insert into mysql.user(host,user,authentication_string) values ('localhost','userName',md5('passWord'));
2. 删除用户
drop user userName[@'localhost'] [,userName ];
-- drop user userName 删除mysql用户
-- drop user 'userName'@'localhost' 删除用户和主机部分
delete from mysql.user where host='localhost' and user='userName'
3. 修改密码
-- 修改root用户密码
update mysql.user set authentication_string=md5("passWord") where user='root' and host='localhost';
flush privileges;
-- 需要重新刷新加载权限
4. 权限控制
① 添加权限
grant privType [(columns)] [,privType [(columns)]]
on [objectType] teble1 [,table2]
to 'userName'@'localhost' [with grant option]
-- privType 表示权限类型
-- columns 表示列,不指定则为全表
-- objectType 指定授权作用对象 TABLE(表)、FUNCTION(函数)和PROCEDURE(存储过程)
-- userName 授权用户
-- with 关键字后可以跟一个或多个参数,① GRANT OPTION:被授权的用户可以将这些权限赋予别的用户。② MAX_QUERIES_PER_HOUR count:设置每个小时可以执行count次查询。③ MAX_UPDATES_PER_HOUR count:设置每小时可以执行count次更新。④ MAX_CONNECTIONS_PER_HOUR count:设置每小时可以建立count个连接。⑤ MAX_USER_CONNECTIONS count:设置单个用户可以同时建立count个连接。
② 权限回收
revoke all privType [(columns)] [,privType [(columns)]]
on table1 [,table2]
form 'userName'@'localhost';