• mysql授权远程用户连接(权限最小化原则)


    1、进入MySQL,创建一个新用户root,密码为root:

    格式:grant 权限 on 数据库名.表名 to 用户@登录主机 identified by “用户密码”; 
    grant select,update,insert,delete on . to root@192.168.1.12 identified by “root”;

    原先数据表结构

    mysql> use mysql;
    Database changed
    mysql> select host,user,password from user;
    +-----------+------+-------------------------------------------+
    | host      | user | password                                  |
    +-----------+------+-------------------------------------------+
    | localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
    +-----------+------+-------------------------------------------+

    执行上述语句后结果

    mysql> use mysql;
    Database changed
    mysql> select host,user,password from user;
    +--------------+------+-------------------------------------------+
    | host         | user | password                                  |
    +--------------+------+-------------------------------------------+
    | localhost    | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
    | 192.168.1.12 | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
    +--------------+------+-------------------------------------------+
    2 rows in set (0.00 sec)

    可以看到在user表中已有刚才创建的root用户。host字段表示登录的主机,其值可以用IP,也可用主机名, 
    有时想用本地IP登录,那么可以将以上的Host值改为自己的Ip即可。

    2、实现远程连接(授权法)

    将host字段的值改为%就表示在任何客户端机器上能以root用户登录到mysql服务器,建议在开发时设为%。 
    update user set host = ’%’ where user = ’root’;

    将权限改为ALL PRIVILEGES

    mysql> use mysql;
    Database changed
    mysql> grant all privileges  on *.* to root@'%' identified by "root";
    Query OK, 0 rows affected (0.00 sec) 
    
    mysql> select host,user,password from user;
    +--------------+------+-------------------------------------------+
    | host         | user | password                                  |
    +--------------+------+-------------------------------------------+
    | localhost    | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
    | 192.168.1.12 | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
    | %            | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
    +--------------+------+-------------------------------------------+
    3 rows in set (0.00 sec)

    这样机器就可以以用户名root密码root远程访问该机器上的MySql.

    3、实现远程连接(改表法)

    use mysql;

    update user set host = ‘%’ where user = ‘root’;

    这样在远端就可以通过root用户访问Mysql.

     mysql假如授权 grant all privileges on *.* to 'huowuzhao'@'192.168.%' identified by 'password';
    再授权grant all privileges on *.* to 'huowuzhao'@'192.168.222.%' identified by 'password';之后,之前授权的用户将失效,是因为mysql有权限最小化原则。
  • 相关阅读:
    ‘Host’ is not allowed to connect to this mysql server
    centos7安装mysql
    further configuration avilable 不见了
    Dynamic Web Module 3.0 requires Java 1.6 or newer
    hadoop启动 datanode的live node为0
    ssh远程访问失败 Centos7
    Linux 下的各种环境安装
    Centos7 安装 python2.7
    安装scala
    Centos7 安装 jdk 1.8
  • 原文地址:https://www.cnblogs.com/pythonal/p/6288943.html
Copyright © 2020-2023  润新知