MySQL8学习

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

1. 用户操作

  1. 创建用户
    create user 'username'@'hostname' identified with mysql_native_password by 'password';//MySQL8以上(由于mysql8的密码存储方式改变,许多桌面化工具不支持,所以改回之前的加密方式)  
     create user 'username'@'hostname' identified by 'password';//MySQL8以下
  2. 修改用户密码
    alter user 'username'@'hostname' identified with mysql_native_password by 'password';  //mysql8以上  
     set password for 'mysqltest'@'localhost' = password('mysqltest');  //mysql8以下
  3. 删除用户
    drop user 'username'@'hostname';
  4. 允许远程登录/赋予权限
    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';//赋予用户赋予权限的命令  
  5. 查询权限
    show grants for 'username'@'hostname';//查询权限
  6. 撤销权限
    REVOKE privilege ON database_name.table_name FROM 'username'@'host'; 
  7. 使用角色
    create role 'role1','role2';//创建角色  
     grant select on database_name.table_name to role1;//赋予角色权限  
     grant role1 to user_name;//赋予用户角色  
  8. 远程登录
    mysql --host=hostname --port=3306 --user=username --password<=password>  
     mysql -h hostname -P 3306 -u username -p   
     -P:端口  
     -p:密码  
  9. 锁定/解锁用户
    alter user 'username'@'hostname' account lock/unlock;
  10. 设置过期密码
    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操作

  1. 查询数据库

    show databases;
  2. 使用数据库

    use 'database_name';
  3. 查询当前使用数据库

    select database();
  4. 创建数据库

    create database database_name;
  5. 查询表

    show tables;
  6. 描述表信息

    desc tablename;
  7. 创建表结构

    create table if not exists database_name.table_name(  
     	'id' int AUTO_INCR
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值