Mysql 添加帐户/修改账户/分配权限/开启远程访问

Song2052 次浏览0个评论2018年05月02日

1.1 登录MYSQL:

@>mysql -u root -p
@>密码

1.2 创建用户:

mysql> insert into mysql.user(Host,User,Password) values("localhost","test",password("1234"));

这样就创建了一个名为:test密码为:1234的用户。

注意:此处的localhost,是指该用户只能在本地登录,不能在另外一台机器上远程登录。如果想远程登录的话,将localhost改为%,表示在任何一台电脑上都可以登录。也可以指定某台机器可以远程登录。

1.3 删除用户

mysql5之前删除用户时必须先使用revoke删除用户权限,然后删除用户,mysql5之后drop命令可以删除用户的同时删除用户的相关权限。

mysql>drop user newuser;

1.4 更改密码

mysql> set password for zx_root =password('xxxxxx');
# 也可以使用下面这种方法
mysql> update mysql.user set password=password('xxxx') where user='otheruser';

1.5 查看用户权限

mysql> show grants for zx_root;

1.6 赋予权限

mysql> grant select on dmc_db.*  to zx_root;

grant可以给普通数据用户赋予,查询、插入、更新、删除 数据库中所有表数据的权利。

grant select on testdb.* to common_user@"localhost";
grant insert on testdb.* to common_user@"localhost";
grant update on testdb.* to common_user@"localhost";
grant delete on testdb.* to common_user@"localhost";

也可以使用all代表4种状态

grant all on testdb.* to dba@"localhost";

1.7 回收权限

如果权限不存在会报错,可以查询、插入、更新、删除4种权限

mysql> revoke select on dmc_db.* from zx_root;

1.8 grant和revoke可以在几个层次上控制访问权限

  • 1:整个服务器,使用 grant ALL 和revoke ALL
  • 2:整个数据库,使用on database.*
  • 3:特点表,使用on database.table
  • 4:特定的列
  • 5:特定的存储过程

注意:修改完权限以后 一定要刷新服务,或者重启服务,刷新服务用:

FLUSH PRIVILEGES;

提交评论

请登录后评论

用户评论

    当前暂无评价,快来发表您的观点吧...

更多相关好文

    当前暂无更多相关好文推荐...