前情介绍:

我们都晓得登陆MySQL数据库时,联接层接入数据库须要经过mysql.user表中,用户名密码的验证就能登入数据库。

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

明天我们来介绍一下怎样来使用Linux操作系统用户,通过验证插件映射MySQL内的帐号,登陆数据库管理的方式。

操作环境:

操作系统:centos7.6

MySQL版本:MySQLEnterpriseServer8.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?

登录linux_登录linux服务器_linux mysql登录

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

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顾客端--->PAMAPI--->读取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用于做登陆验证PAM2.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数据库用户

登录linux服务器_linux mysql登录_登录linux

[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用户组映射数据库user1linux makefile,其aa组的用户可以使用user1的权限进行数据库操作.

授proxyuser匿名帐户

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

登录linux_linux mysql登录_登录linux服务器

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联接到数据库linux mysql登录,因只具有accounting只读select权限红旗linux5.0,所以createdatabase失败。

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等权限linux mysql登录,所以可以正常执行。

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点:

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

====End===

EnjoyGreatSQL:)

点击小程序参与中奖

登录linux服务器_登录linux_linux mysql登录

点击小程序留言

《深入浅出MGR》视频课程

戳此小程序即可直达B站

本文原创地址:https://www.linuxprobe.com/rhzlxtxtgpyz.html编辑:刘遄,审核员:暂无