美文网首页
Linxu用户名验证登录MySQL管理数据库

Linxu用户名验证登录MySQL管理数据库

作者: GreatSQL | 来源:发表于2022-04-20 14:02 被阅读0次
    • GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源。

    前情介绍:

    我们都知道登录MySQL数据库时,连接层接入数据库需要经过mysql.user表中,用户名密码的验证才能登录数据库。

    如果mysql.user中不存在此用户或者密码不正确,则会返回错误提示。假如mysql.user数据库表中没有对应的账号,我们能不能登录数据库呢?

    今天我们来介绍一下如何来使用Linux操作系统用户,通过验证插件映射MySQL内的账号,登录数据库管理的方法。

    操作环境:

    操作系统:centos 7.6

    MySQL版本:MySQL Enterprise Server 8.0.27

    我们边操作边介绍其工作过程。

    1、首先建立对应的PAM文件

    PAM验证文件配置目录在linux上的/etc/pam.d/ 目录下

    [root@localhost ~]# ls /etc/pam.d/
    atd          crond                gdm-autologin           gdm-pin        mysql-pam   password-auth     postlogin     rhn_register  smartcard-auth     subscription-manager      su-l            vlock
    chfn         cups                 gdm-fingerprint         gdm-smartcard  mysql-pam2  password-auth-ac  postlogin-ac  runuser       smartcard-auth-ac  subscription-manager-gui  system-auth     vmtoolsd
    chsh         fingerprint-auth     gdm-launch-environment  liveinst       other       pluto             ppp           runuser-l     sshd               sudo                      system-auth-ac  xserver
    config-util  fingerprint-auth-ac  gdm-password            login          passwd      polkit-1          remote        setup         su                 sudo-i                    systemd-user
    

    编辑文件内容如下:

    [root@localhost ~]#touch  /etc/pam.d/mysql-pam
    [root@localhost ~]# vim/etc/pam.d/mysql-pam
    #%PAM-1.0
    auth include password-auth 
    account include password-auth
    

    1.1 什么是PAM?

    PAM全称Pluggable Authentication Modules可插入的验证模块,其用途是能够使应用程序与认证机制分离。

    MySQL默认登录校验一般是通过内部的mysql.user表进行用户名、密码的匹配验证,而PAM则是通过配置系统/etc/pam.d/下的配置文件,进行身份识别和验证的。

    用户调用某个应用程序,比如MySQL客户端登录时,PAM应用程序调用后台的PAM库进行验证工作,接着PAM库在目录/etc/pam.d/目录下面查找相应的mysql中对应配置文件,该文件告诉PAM应用程序使用何种验证机制以便PAM库装在所需要的验证模块,这些模块可以让PAM库与应用程序中的转换函数进行通信

    1.2 其中共有四个模块:

    模块 作用
    auth(验证模块) 用于验证用户或设置/销毁凭证
    account(账户管理模块) 执行访问、账户及凭证有效期、密码限制/规则等操作
    session(会话管理模块) 初始化或终止会话
    passwd(密码模块) 执行密码更改或更新操作

    比如我们经常连接Linux系统所用的ssh协议,其验证配置文件就使用了上述的验证、账户、会话以及密码四部分,内容如下:

    [root@localhost pam.d]# cat /etc/pam.d/sshd 
    #%PAM-1.0
    auth       required pam_sepermit.so
    auth       substack     password-auth
    auth       include      postlogin
    # Used with polkit to reauthorize users in remote sessions
    -auth      optional     pam_reauthorize.so prepare
    account    required     pam_nologin.so
    account    include      password-auth
    password   include      password-auth
    # pam_selinux.so close should be the first session rule
    session    required     pam_selinux.so close
    session    required     pam_loginuid.so
    # pam_selinux.so open should only be followed by sessions to be executed in the user context
    session    required     pam_selinux.so open env_params
    session    required     pam_namespace.so
    session    optional     pam_keyinit.so force revoke
    session    include      password-auth
    session    include      postlogin
    # Used with polkit to reauthorize users in remote sessions
    -session   optional     pam_reauthorize.so prepare
    [root@localhost pam.d]# 
    

    1.3 其验证的流程是:

    应用程序MySQL客户端--->PAM API--->读取PAM配置文件---->配置文件中模块鉴别--->鉴别成功--->将权限授予用户--->执行操作

    或者->鉴别失败--->拒绝服务,阻止操作

    而我们此次配置MySQL的pam认证方式,仅用四个模块中的auth和account两个模块,做身份鉴别和验证

    [root@localhost ~]# cat /etc/pam.d/mysql-pam
    #%PAM-1.0
    auth include password-auth 
    account include password-auth
    

    2、创建操作系统用户rsmith 、aa 用于做登录验证PAM

    2.1第一个系统用户rsmith

    [root@localhost ~]# useradd rsmith
    [root@localhost ~]# id rsmith
    uid=1002(rsmith) gid=1002(rsmith) 组=1002(rsmith)
    [root@localhost ~]# 
    

    为系统用户rsmith设置操作系统密码:

    [root@localhost ~]# passwd rsmith
    更改用户 rsmith 的密码 。
    新的 密码:
    无效的密码: 密码少于 8 个字符
    重新输入新的 密码:
    passwd:所有的身份验证令牌已经成功更新。
    

    2.2 第二个系统用户aa

    [root@localhost ~]# useradd aa
    [root@localhost ~]# passwd aa
    更改用户 aa 的密码 。
    新的 密码:
    重新输入新的 密码:
    passwd:所有的身份验证令牌已经成功更新。
    [root@localhost ~]# 
    

    3、安装用于PAM验证插件authentication_pam

    mysql> install plugin authentication_pam soname 'authentication_pam.so';
    
    # 查看插件状态是否可用
    3.1 mysql> select plugin_name,plugin_status from information_schema.plugins where plugin_name like '%pam%';  
    +--------------------+---------------+
    | PLUGIN_NAME        | PLUGIN_STATUS |
    +--------------------+---------------+
    | authentication_pam | ACTIVE        |
    +--------------------+---------------+
    

    4、创建操作系统映射的MySQL数据库用户

    [root@localhost ~]# mysql -uroot -p -hlocalhost -S /usr/local/mysql/data/mysql.sock 
    

    4.1 创建 accounting@localhost数据库用户,指定使用 /etc/pam.d/mysql-pam 文件

    mysql> create user accounting@localhost identified with authentication_pam  AS 'mysql-pam';
    Query OK, 0 rows affected (0.00 sec)
    
    # 授权accounting@localhost只读权限
    mysql> grant select on *.* to accounting@localhost ;
    Query OK, 0 rows affected (0.00 sec)
    

    4.2 创建 user1@localhost数据库用户

    mysql> create user user1@localhost identified with mysql_no_login; --禁止直接登录,# 只允许通过代理用户登录
    Query OK, 0 rows affected (0.00 sec)
    
    # 授权user1@localhost可进行DML的增删改操作
    mysql> grant insert,delete,update on test.* to user1@localhost;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    

    4.3 创建匿名账号代理

    其次,我们可以创建一个匿名代理账号,仅限代理用户,具有 PAM 组映射。

    对于这种情况,创建一个或多个定义不同权限集的 MySQL 帐户。建议将其设置为no_login即不允许直接使用这些帐户进行数据库连接。

    然后定义一个通过 PAM 进行身份验证的默认用户,该用户使用某种映射方案(通常基于用户所属的外部 PAM 组)将所有外部用户名映射到少数 MySQL拥有权限集的帐户。

    任何连接客户端都会映射到其中一个 MySQL 帐户并使用其权限。

    mysql> create user ''@'%' identified with authentication_pam as 'mysql-pam,rsmith=accounting,aa=user1';
    Query OK, 0 rows affected (0.00 sec)
    

    解释:其中mysql-pam为pam执行的密码身份验证,rsmith=accounting是将系统rsmith用户组的用户映射数据库accounting用户。

    所有rsmith系统用户组的用户均已可使用accounting的权限操作数据库,系统aa用户组映射数据库user1,其aa组的用户可以使用user1的权限进行数据库操作.

    授proxy user匿名账户

    mysql> grant proxy on  accounting@localhost to ''@'%';
    Query OK, 0 rows affected (0.00 sec)
    mysql> grant proxy on  user1@localhost to ''@'%';
    

    5、客户端使用明文验证登录

    [root@localhost ~]# mysql --enable-cleartext-plugin -ursmith -p   -S /usr/local/mysql/data/mysql.sock
    Enter password:   <----输入rsmith的操作系统密码登录
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 35
    Server version: 8.0.27-commercial MySQL Enterprise Server - Commercial
    owners.
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    

    5.1 查看当前登录账号信息

    mysql> select user(),current_user(),@@proxy_user;
    +------------------+----------------------+--------------+
    | user()           | current_user()       | @@proxy_user |
    +------------------+----------------------+--------------+
    | rsmith@localhost | accounting@localhost | ''@'%'       |
    +------------------+----------------------+--------------+
    1 row in set (0.00 sec)
    

    5.2验证用户权限

    尝试创建数据库

    mysql> create database testpam;
    ERROR 1044 (42000): Access denied for user 'accounting'@'localhost' to database 'testpam'
    
    mysql> select * from mysql.user where user like '%accoun%'\G
    *************************** 1. row ***************************
                        Host: localhost
                        User: accounting
                 Select_priv: Y         --->只读权限
                 Insert_priv: N
                 Update_priv: N
                 Delete_priv: N
                 Create_priv: N
                   Drop_priv: N
                 Reload_priv: N
               Shutdown_priv: N
                Process_priv: N
                   File_priv: N
                  Grant_priv: N
             References_priv: N
                  Index_priv: N
                  Alter_priv: N
                Show_db_priv: N
                  Super_priv: N
       Create_tmp_table_priv: N
            Lock_tables_priv: N
                Execute_priv: N
             Repl_slave_priv: N
            Repl_client_priv: N
            Create_view_priv: N
              Show_view_priv: N
         Create_routine_priv: N
          Alter_routine_priv: N
            Create_user_priv: N
                  Event_priv: N
                Trigger_priv: N
      Create_tablespace_priv: N
                    ssl_type: 
                  ssl_cipher: 0x
                 x509_issuer: 0x
                x509_subject: 0x
               max_questions: 0
                 max_updates: 0
             max_connections: 0
        max_user_connections: 0
                      plugin: authentication_pam    --->  验证插件
       authentication_string: mysql-pam      --->验证文件
            password_expired: N
       password_last_changed: NULL
           password_lifetime: NULL
              account_locked: N
            Create_role_priv: N
              Drop_role_priv: N
      Password_reuse_history: NULL
         Password_reuse_time: NULL
    Password_require_current: NULL
             User_attributes: NULL
    1 row in set (0.00 sec)
    
    mysql> 
    

    我们可以看到操作系统用户rsmith以accounting@localhost连接到数据库,因只具有accounting只读select权限,所以create database失败。

    5.3登录aa系统账号,验证其权限

    [root@localhost ~]# mysql --enable-cleartext-plugin -uaa -p -S /usr/local/mysql/data/mysql.sock

    Enter password:  ----输入aa用户的操作系统密码
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    mysql> select user(),current_user(),@@proxy_user;
    +--------------+-----------------+--------------+
    | user()       | current_user()  | @@proxy_user |
    +--------------+-----------------+--------------+
    | aa@localhost | user1@localhost | ''@'%'       |
    +--------------+-----------------+--------------+
    1 row in set (0.00 sec)
    
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | test               |
    +--------------------+
    2 rows in set (0.00 sec)
    mysql> insert into test.t1(id,name) values(1,'aa');
    Query OK, 1 row affected (0.00 sec)
    mysql> 
    

    aa映射为数据库的user1@localhost,user1其具备insert,update,delete等权限,所以可以正常执行。

    6、新添加系统用户到PAM组同样具备数据库操作权限

    6.1创建新操作系统用户

    [root@localhost ~]# useradd  bb -g aa
    [root@localhost ~]# passwd bb
    更改用户 bb 的密码 。
    新的 密码:
    重新输入新的 密码:
    passwd:所有的身份验证令牌已经成功更新。
    

    6.2登录做PAM身份验证

    [root@localhost ~]# mysql --enable-cleartext-plugin -ubb -p -S /usr/local/mysql/data/mysql.sock
    Enter password: ----bb用户操作系统密码
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 48
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> select user(),current_user(),@@proxy_user;
    +--------------+-----------------+--------------+
    | user()       | current_user()  | @@proxy_user |
    +--------------+-----------------+--------------+
    | bb@localhost | user1@localhost | ''@'%'       |
    +--------------+-----------------+--------------+
    1 row in set (0.00 sec)
    

    可正常登录MySQL数据库,但对应库内不存在rsmith、aa、bb等用户,全部映射为accounting@localhost和user1@localhost用户,并具备其数据库操作权限。

    全文总结:

    当前的pam验证方式仅在MySQL的企业版中支持,社区版本中暂时不支持authentication_pam.so插件,所以可以下载企业版玩下试试。

    其特点和使用场景总结为如下2点:

    • 1、针对不同登录到Linux操作系统用户,将数据库用户授予不同的权限,当外部用户连接时这里指的是操作系统用户,映射具有不同权限的MySQL内部账户进行代理,以达到不同操作系统用户登录数据库时,分配不同的数据库权限,更灵活。

    比如上文中的Linux中aa组成员登录MySQL时,映射mysql.user中的user1,并且具有user1的select只读权限进行数据库操作,系统用户rsmith登录时映射MySQL库中accounting数据库用户,且只有只读权限。

    • 2、使 MySQL 服务器能够使用PAM进行身份验证更灵活。使系统能够使用标准接口来访问各种身份验证方法。典型应用场景,支持传统的 Unix 密码和 LDAP(Lightweight Directory Access Protocol轻型目录访问协议),LDAP典型如windows server的AD域。

    ====end===

    Enjoy GreatSQL :)

    本文由博客一文多发平台 OpenWrite 发布!

    相关文章

      网友评论

          本文标题:Linxu用户名验证登录MySQL管理数据库

          本文链接:https://www.haomeiwen.com/subject/ewjvertx.html