一、连接mysql(我用的工具是xshell6)
命令:
mysql -uroot -p
二、创建子账号
创建一个用户名为test,密码为123456的子账号。
命令:
create user 'test'@'localhost' identified by '123456'; #这种创建方式只能本地登录 create user 'test'@'%' identified by '123456'; #这种创建方式可以远程登录,即别的地方可以登录
三、测试刚刚创建的子账号是否可以登录
命令:
mysql -utest -p
四、给子账号添加权限
1.创建一个测试数据库testdemo(登录root账号)
命令:
create database testdemo default charset utf8 collate utf8_general_ci;
2.添加权限
添加权限第1步:
"localhost"表示对本地主机授权,此时使用子账号本地登录拥有testdemo的操作权限,远程登录依然没有testdemo的操作权限。
all privileges表示所有操作权限,也可以填写部分权限,比如把all privileges改为create,update,delete,select等。
命令:
//赋予test子账号对testdemo数据库所有操作权限 grant all privileges on testdemo.* to "test"@"localhost" identified by "123456"; //如果不想赋予所有权限 //赋予test子账号对testdemo数据库select,update权限 grant select,update on testdemo.* to "test"@"localhost" identified by "123456"; //注意:mysql8.0之后赋予权限命令有变化,如下 grant all privileges on testdemo.* to 'test'@'localhost'; //赋予权限后要刷新系统权限表,使配置生效 flush privileges;
备注:也可能不是localhost表示本地,要去mysql.user表中查看
查询语句为:
select host,user,grant_priv,super_priv from mysql.user;
添加权限第2步:
“%” 表示对所有非本地主机授权,不包括localhost。此时使用子账号远程登录对testdemo数据库才有操作权限。
命令:
grant all privileges on testdemo.* to "test"@"%" identified by "123456"; flush privileges; #刷新系统权限表
使用子账号远程登录的结果:
五、删除子账号及权限
1.删除远程test账号(此时本地的test账号依然可以使用)
命令:
drop user test@"%";
2.删除本地test账号
命令:
drop user test@"localhost";
至此整个流程完成!
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论