mysql创建用户和管理用户权限

一、创建用户:

mysql>create user 'username'@'host' identified by 'password';

username 创建的用户名

host 指定该用户在哪个主机上可以登录,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机登录,可以使用通配符%

password 该用户登录密码,密码可以为空,如果为空该用户可以不需要密码登录服务器

  

二、grant 说明:

grant给用户添加权限,权限会自动叠加,不会覆盖之前授予的权限,使用 grant 给用户授权后,不需要使用 flush priveleges 刷新权限

mysql>grant 权限1,权限2...权限n on 数据库名称.表名称 to 用户名@用户地址 identified by ‘连接密码’ with grant option;

  1. 权限1,权限2...权限n代表select,instert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等14个权限
  2. 当权限1,权限2...权限n被 all privileges 或者 all 代替,表示赋予用户全部权限
  3. 当数据库名称.表名称被 *.* 代替,表示赋予用户操作服务器上所有数据库所有表的权限
  4. 用户地址可以是localhost,也可以是ip地址,机器人名字,域名。也可以用 ‘%’ 表示从任何地址连接
  5. ‘连接密码’不能为空,否则创建失败
  6. with grant option允许用户将自己的权限授予其他用户(

    如果带了 with grant option,那么用户testuser1可以将select ,update权限传递给其他用户( 如testuser2)
    grant select,update on bd_corp to testuser2
    如果没带with grant option,那么用户testuser1不能给testuser2授权

e.g:

mysql>grant select,insert,update,delete,create,drop on testdb.users  to [email protected] identified by '123456'

给来自10.240.171.103的用户testuser分配对数据库testdb的users表进行select,insert,update,create,drop的操作权限,并设定登录密码为123456

扫描二维码关注公众号,回复: 1782364 查看本文章

mysql>grant all privileges on testdb.* [email protected] identified by '123456'

给来着10.240.171.103的用户testuser分配对数据库testdb所有表进行所有操作的权限,并设定登录密码为123456

mysql>grant all privileges on *.*  to [email protected] identified by '123'

给来自10.240.171.103的用户testuser分配可对所有数据库的所有表进行所有操作的权限,并设定登录密码为123

mysql>flush privileges   

mysql新设置用户或更改密码后需要用flush privileges 刷新mysql的系统权限相关表,否则会出现拒绝访问,还有一种方法,就是重启mysql服务器,使新设置生效

关于root用户的访问设置:

设置所有用户可以远程访问mysql,修改my.cnf配置文件,将bind-address=127.0.0.1前面加“#”注释掉,这样就允许其他机器人远程访问本机mysql了;

mysql>grant all privileges on *.* to root@'%' identified by '123456';    //设置用户root可以远程访问mysql

关闭root用户远程访问权限

mysql>delete from user where user="root" and host="%";   //禁止root用户在远程机器上访问mysql

三、revoke撤销权限:

mysql>revoke all privileges on test.* from 'testuser'@'%'

revoke只是撤销grant创建的权限,但是testuser用户没有被删除,必须手工从user表删除

四、设置与更改用户密码

mysql>set password for 'username'@'host'=password('newpassword')  //如果是当前登录用户用set password=password('newpassword')

猜你喜欢

转载自www.cnblogs.com/QAroad/p/9242810.html