MySQL如何新增用户(权限),改密码,删除用户

查看MYSQL数据库中所有用户
mysql>use mysql;
mysql>select host, user from user;
或者
mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
查看数据库中具体某个用户的权限
mysql>show grants for root@localhost;

1、使用 root 管理员登陆 mysql
mysql -uroot -p123456; 或者 mysql -h192.168.1.1 -ua1 -p123456;
2、创建新用户
create user a1@localhost identified by '123456'; #创建只能本地登陆的用户
create user a1@% identified by '123456'; #创建可以外网登陆的用户
'%' - 所有情况都能访问
‘localhost’ - 本机才能访问
111.222.33.44 - 指定 ip 才能访问
注:修改密码
update mysql.user set password=password('新密码') where user='user1';
这个时候访问,是除了默认生成的两个数据库,看不到任何其它的数据库:
3、给某用户添加权限
用update命令:
mysql>use mysql;
mysql>update user set host = '%' where user = 'a1'; #给a1增加任何ip所有权限

或者用grant 命令:
grant all on 想授权的数据库. to a1@'%'; #给任何ip所有权限
grant all on 想授权的数据库.
to a1@localhost; #给localhost所有权限
grant all on 想授权的数据库. to a1@localhost identified by '123456'; #同时创建用户并授权 注意:用以上命令授权的用户不能给其它用户授权,如果想让该用户可以授权,用以下命令:
grant on 想授权的数据库.
to a1@localhost with grant option;
grant select on 想授权的数据库.* to a1@localhost identified by '123456'; #给select权限
4、删除用户
delete from mysql.user where user='a1';
四、刷新
flush privileges;
分享:

猜你喜欢

转载自blog.51cto.com/hzcto/2415604