1. 用户操作
- 创建用户
create user 'username'@'hostname' identified with mysql_native_password by 'password';//MySQL8以上(由于mysql8的密码存储方式改变,许多桌面化工具不支持,所以改回之前的加密方式) create user 'username'@'hostname' identified by 'password';//MySQL8以下 - 修改用户密码
alter user 'username'@'hostname' identified with mysql_native_password by 'password'; //mysql8以上 set password for 'mysqltest'@'localhost' = password('mysqltest'); //mysql8以下 - 删除用户
drop user 'username'@'hostname'; - 允许远程登录/赋予权限
alter user set user.host = '%' where name = 'root';//mysql8以上远程登录 grant all privileges on *.* to 'username'@'hostname';//mysql8以上赋予权限 grant all privileges on *.* to 'username'@'hostname' identified by 'password' with grant option;//mysql8以下赋予权限且远程登录 grant select/insert/update/delete on *.* to 'username'@'hostname';//赋予单项权限 grant select(id,salary) on database_name.table_name to 'username'@'hostname';//指定查询某列 grant grant option on *.* to 'username'@'hostname';//赋予用户赋予权限的命令 - 查询权限
show grants for 'username'@'hostname';//查询权限 - 撤销权限
REVOKE privilege ON database_name.table_name FROM 'username'@'host'; - 使用角色
create role 'role1','role2';//创建角色 grant select on database_name.table_name to role1;//赋予角色权限 grant role1 to user_name;//赋予用户角色 - 远程登录
mysql --host=hostname --port=3306 --user=username --password<=password> mysql -h hostname -P 3306 -u username -p -P:端口 -p:密码 - 锁定/解锁用户
alter user 'username'@'hostname' account lock/unlock; - 设置过期密码
create user 'username'@'hostname' identified with mysql_native_password by 'password' password expire; ALTER user 'username'@'hostname' identified with mysql_native_password by 'password' password expire [interval 90 day];
2. DML操作
-
查询数据库
show databases; -
使用数据库
use 'database_name'; -
查询当前使用数据库
select database(); -
创建数据库
create database database_name; -
查询表
show tables; -
描述表信息
desc tablename; -
创建表结构
create table if not exists database_name.table_name( 'id' int AUTO_INCR

这篇博客详细介绍了MySQL8的用户操作,包括创建、修改和删除用户,权限管理等。还涵盖了DML操作,如查询、创建和修改数据库及表,以及DDL操作,如加载数据、存储过程和函数的创建。此外,还讨论了触发器、视图和进阶用法,如JSON解析和窗口函数。
最低0.47元/天 解锁文章
432

被折叠的 条评论
为什么被折叠?



