- 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 vlockchfn cups gdm-fingerprint gdm-smartcard mysql-pam2 password-auth-ac postlogin-ac runuser smartcard-auth-ac subscription-manager-gui system-auth vmtoolsdchsh fingerprint-auth gdm-launch-environment liveinst other pluto ppp runuser-l sshd sudo system-auth-ac xserverconfig-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.0auth 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.0auth required pam_sepermit.soauth substack password-authauth include postlogin# Used with polkit to reauthorize users in remote sessions-auth optional pam_reauthorize.so prepareaccount required pam_nologin.soaccount include password-authpassword include password-auth# pam_selinux.so close should be the first session rulesession required pam_selinux.so closesession required pam_loginuid.so# pam_selinux.so open should only be followed by sessions to be executed in the user contextsession required pam_selinux.so open env_paramssession required pam_namespace.sosession optional pam_keyinit.so force revokesession include password-authsession 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.0auth include password-auth account include password-auth
2、创立操作系统用户rsmith 、aa 用于做登录验证PAM
2.1第一个零碎用户rsmith
[root@localhost ~]# useradd rsmith[root@localhost ~]# id rsmithuid=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.sockEnter password: <----输出rsmith的操作系统明码登录Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 35Server version: 8.0.27-commercial MySQL Enterprise Server - Commercialowners.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: NULLPassword_require_current: NULL User_attributes: NULL1 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.sockEnter password: ----bb用户操作系统明码Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 48Type '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 公布!