标签搜索

MySQL用户管理与授权相关命令

basil
2019-09-08 / 187 阅读

创建用户

  • 以root用户登录mysql

    mysql -uroot -p

  • 输入以下命令,新建一个登录名为hello,密码为helloworld的用户
   use mysql

   insert into user(Host,User,Password)values('localhost','hello',password('helloworld'));

注:Host字段的值为localhost时,只能本地访问,如果需要支持远程连接,需要把Host字段的值改为%

修改密码

  • 以root身份登录mysql -uroot -p执行以下命令,将hello用户密码改为123456
   update mysql.user set Password = password('123456') where User='hello' and Host='localhost';

删除用户

  • 以root身份登录mysql -uroot -p,删除hello用户
delete from mysql.user where User='hello' and Host='localhost';

flush privileges;

用户授权

  • 以root用户登录,创建数据库test
mysql -uroot -p

create database test
  • 授予hello用户拥有test数据库所有权限
grant all privileges on test.* to hello@localhost identified by 'helloworld';

flush privileges;

注:授予hello用户拥有部分权限,将第一行的privileges替换成select,delete,update,drop即可

  • 授予hello用户拥有所有数据库的权限
grant all privileges on *.* to hello@localhost identified by 'helloworld';

flush privileges;
1