MySQL远程连接、用户授权

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

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

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

授权格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by “密码”;

2.1 首先为用户创建一个数据库(testDB):

mysql>create database testDB;

2.2 授权test用户拥有testDB数据库的所有权限(某个数据库的所有权限):

mysql>grant all privileges on testDB.* to test@localhost identified by ‘1234’;

mysql>flush privileges;//刷新系统权限表,即时生效

2.3 如果想指定某库的部分权限给某用户本地操作,可以这样来写:

mysql>grant select,update on testDB.* to test@localhost identified by ‘1234’;

mysql>flush privileges;

2.4 授权test用户拥有所有数据库的某些权限的远程操作:

mysql>grant select,delete,update,create,drop on . to test@”%” identified by “1234”;

test用户对所有数据库都有select,delete,update,create,drop 权限。

2.5 查看用户所授予的权限:

mysql> show grants for test@localhost;

mysql>Delete FROM user Where User=’test’ and Host=’localhost’;

mysql>flush privileges;

删除账户及权限:>drop user 用户名@’%’;

drop user 用户名@ localhost;

mysql>update mysql.user set password=password(‘新密码’) where User=”test” and Host=”localhost”;

mysql>flush privileges;

revoke 跟 grant 的语法差不多,只需要把关键字 “to” 换成 “from” 即可:

mysql>grant all on . to dba@localhost;

mysql>revoke all on . from dba@localhost;

6.1 grant, revoke 用户权限后,该用户只有重新连接 MySQL 数据库,权限才能生效。

6.2. 如果想让授权的用户,也可以将这些权限 grant 给其他用户,需要选项 “grant option”

mysql>grant select on testdb.* to dba@localhost with grant option;

mysql>grant select on testdb.* to dba@localhost with grant option;

这个特性一般用不到。实际中,数据库权限最好由 DBA 来统一管理。

补充:

mysql授权表共有5个表:user、db、host、tables_priv和columns_priv。

授权表的内容有如下用途:
user表
user表列出可以连接服务器的用户及其口令,并且它指定他们有哪种全局(超级用户)权限。在user表启用的任何权限均是全局权限,并适用于所有数据库。例如,如果你启用了DELETE权限,在这里列出的用户可以从任何表中删除记录,所以在你这样做之前要认真考虑。

db表
db表列出数据库,而用户有权限访问它们。在这里指定的权限适用于一个数据库中的所有表。

host表
host表与db表结合使用在一个较好层次上控制特定主机对数据库的访问权限,这可能比单独使用db好些。这个表不受GRANT和REVOKE语句的影响,所以,你可能发觉你根本不是用它。

tables_priv表
tables_priv表指定表级权限,在这里指定的一个权限适用于一个表的所有列。

columns_priv表
columns_priv表指定列级权限。这里指定的权限适用于一个表的特定列

Original: https://www.cnblogs.com/48xz/p/16166962.html
Author: HammerZe
Title: MySQL远程连接、用户授权

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/643396/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球