一、MySQL5.0版本
1、创建用户和授权
语法
-- 创建用户语法
create user '用户名'@'localhost'; -- 创建用户,未设置密码
create user '用户名'@'localhost' identified by password '密码';--创建用户同时设置密码
-- 创建用户的同时设置密码,并且授权
grant 权限 on 数据库名.表名 to '用户名'@'host' identified by password '密码';
测试:
create user angus@localhost; -- 成功
create user angus@localhost identified by '123456'; --成功
grant select on db1.table1 to 'angus'@'localhost' identified by '123456'; --成功
2、修改用户密码
语法
--指定修改某个用户的密码
set password for '用户名'@'localhost' = password('新密码');
-- 修改当前登录用户的密码
set password = password('新密码');
测试
--指定修改某个用户的密码
set password for angus@localhost = password('11111'); --成功
-- 修改当前登录用户的密码
set password = password("11111111"); --成功
二、MySQL8.0版本
1、创建用户
语法:
--创建用户语法
create user '用户名'@'host'; -- 创建用户,未设置密码
create user '用户名'@'host' identified by password '密码';--创建用户同时设置密码
-- 创建用户成功后,再进行授权
grant 权限 on 数据库名.表名 to '用户名'@'localhost';
测试:
create user angus@localhost; -- 成功
create user angus@localhost identified by '123456';--成功
-- 直接创建用户的同时授权,运行失败
grant select on *.* to 'angus'@'localhost' identified by '123456';--失败
-- 先创建用户
create user 'angus'@'localhost' identified by '123456';--成功
-- 再授权
grant select,update on *.* to 'angus'@'localhost'; -- 成功
注意:MySQL8.0 不支持在创建用户的同时授权!
2、修改用户密码
语法:
-- MySQL 8.0后密码的加密方式改变,所以不能使用5.0的方式修改密码
alter user '用户名' identified by 'xxxx';
测试:
alter user 'angus'@'localhost' identified by '77777'; -- 执行成功