关于mysql:Linxu用户名验证登录MySQL管理数据库

4次阅读

共计 9126 个字符,预计需要花费 23 分钟才能阅读完成。

  • 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 :)

文章举荐:

面向金融级利用的 GreatSQL 正式开源
https://mp.weixin.qq.com/s/cI…

Changes in GreatSQL 8.0.25 (2021-8-18)
https://mp.weixin.qq.com/s/qc…

MGR 及 GreatSQL 资源汇总
https://mp.weixin.qq.com/s/qX…

GreatSQL MGR FAQ
https://mp.weixin.qq.com/s/J6…

在 Linux 下源码编译装置 GreatSQL/MySQL
https://mp.weixin.qq.com/s/WZ…

# 对于 GreatSQL

GreatSQL 是由万里数据库保护的 MySQL 分支,专一于晋升 MGR 可靠性及性能,反对 InnoDB 并行查问个性,是实用于金融级利用的 MySQL 分支版本。

Gitee:

https://gitee.com/GreatSQL/Gr…

GitHub:

https://github.com/GreatSQL/G…

Bilibili:

https://space.bilibili.com/13…

微信 &QQ 群:

可搜寻增加 GreatSQL 社区助手微信好友,发送验证信息“加群”退出 GreatSQL/MGR 交换微信群

QQ 群:533341697

微信小助手:wanlidbc

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

正文完
 0