آنها می گویند بهترین رمز عبور رمزی است که لازم نیست به خاطر بسپارید. در مورد MySQL این به لطف افزونه امکان پذیر است
هر دوی این افزونه ها اصلاً جدید نیستند؛ در همین وبلاگ در مورد آنها زیاد گفته شده است، مثلاً در مقاله ای درباره
همانطور که گفتم این خبری نیست و هنگام نصب MySQL با استفاده از بسته های .deb که توسط تیم دبیان پشتیبانی می شود، یک کاربر root برای احراز هویت سوکت ایجاد می شود. این برای MySQL و MariaDB صادق است.
root@app:~# apt-cache show mysql-server-5.7 | grep -i maintainers
Original-Maintainer: Debian MySQL Maintainers <[email protected]>
Original-Maintainer: Debian MySQL Maintainers <<a href="mailto:[email protected]">[email protected]</a>>
با بسته های Debian برای MySQL، کاربر ریشه به صورت زیر احراز هویت می شود:
root@app:~# whoami
root=
root@app:~# mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.7.27-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> select user, host, plugin, authentication_string from mysql.user where user = 'root';
+------+-----------+-------------+-----------------------+
| user | host | plugin | authentication_string |
+------+-----------+-------------+-----------------------+
| root | localhost | auth_socket | |
+------+-----------+-------------+-----------------------+
1 row in set (0.01 sec)
در مورد بسته .deb برای MariaDB هم همینطور است:
10.0.38-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04
MariaDB [(none)]> show grants;
+------------------------------------------------------------------------------------------------+
| Grants for root@localhost |
+------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED VIA unix_socket WITH GRANT OPTION |
| GRANT PROXY ON ''@'%' TO 'root'@'localhost' WITH GRANT OPTION |
+------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
بستههای .deb از مخزن رسمی Percona نیز احراز هویت کاربر ریشه را در سوکت auth-socket و برای سرور Percona پیکربندی میکنند. بیایید یک مثال با
root@app:~# whoami
root
root@app:~# mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 9
Server version: 8.0.16-7 Percona Server (GPL), Release '7', Revision '613e312'
Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> select user, host, plugin, authentication_string from mysql.user where user ='root';
+------+-----------+-------------+-----------------------+
| user | host | plugin | authentication_string |
+------+-----------+-------------+-----------------------+
| root | localhost | auth_socket | |
+------+-----------+-------------+-----------------------+
1 row in set (0.00 sec)
پس جادو چیست؟ این افزونه بررسی میکند که کاربر لینوکس با کاربر MySQL با استفاده از گزینه سوکت SO_PEERCRED برای جمعآوری اطلاعات درباره کاربری که برنامه کلاینت را اجرا میکند مطابقت داشته باشد. بنابراین، این افزونه فقط در سیستمهایی قابل استفاده است که از گزینه SO_PEERCRED پشتیبانی میکنند، مانند لینوکس. گزینه سوکت SO_PEERCRED به شما این امکان را میدهد که از uid فرآیند مرتبط با سوکت مطلع شوید. و سپس او قبلاً نام کاربری مرتبط با این uid را دریافت می کند.
در اینجا یک مثال با کاربر "vagrant" آورده شده است:
vagrant@mysql1:~$ whoami
vagrant
vagrant@mysql1:~$ mysql
ERROR 1698 (28000): Access denied for user 'vagrant'@'localhost'
از آنجایی که هیچ کاربر "ولگرد" در MySQL وجود ندارد، دسترسی به ما ممنوع است. بیایید چنین کاربری ایجاد کنیم و دوباره امتحان کنیم:
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'vagrant'@'localhost' IDENTIFIED VIA unix_socket;
Query OK, 0 rows affected (0.00 sec)
vagrant@mysql1:~$ mysql
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 45
Server version: 10.0.38-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
MariaDB [(none)]> show grants;
+---------------------------------------------------------------------------------+
| Grants for vagrant@localhost |
+---------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'vagrant'@'localhost' IDENTIFIED VIA unix_socket |
+---------------------------------------------------------------------------------+
1 row in set (0.00 sec)
اتفاق افتاد!
خوب، در مورد توزیع غیر دبیان که در آن به طور پیش فرض ارائه نشده است، چطور؟ بیایید سرور Percona برای MySQL 8 نصب شده در CentOS 7 را امتحان کنیم:
mysql> show variables like '%version%comment';
+-----------------+---------------------------------------------------+
| Variable_name | Value |
+-----------------+---------------------------------------------------+
| version_comment | Percona Server (GPL), Release 7, Revision 613e312 |
+-----------------+---------------------------------------------------+
1 row in set (0.01 sec)
mysql> CREATE USER 'percona'@'localhost' IDENTIFIED WITH auth_socket;
ERROR 1524 (HY000): Plugin 'auth_socket' is not loaded
آزاردهنده. چه چیزی کم بود؟ افزونه بارگیری نشده است:
mysql> pager grep socket
PAGER set to 'grep socket'
mysql> show plugins;
47 rows in set (0.00 sec)
بیایید یک افزونه به فرآیند اضافه کنیم:
mysql> nopager
PAGER set to stdout
mysql> INSTALL PLUGIN auth_socket SONAME 'auth_socket.so';
Query OK, 0 rows affected (0.00 sec)
mysql> pager grep socket; show plugins;
PAGER set to 'grep socket'
| auth_socket | ACTIVE | AUTHENTICATION | auth_socket.so | GPL |
48 rows in set (0.00 sec)
حالا همه چیزهایی که نیاز داریم داریم. بیایید دوباره تلاش کنیم:
mysql> CREATE USER 'percona'@'localhost' IDENTIFIED WITH auth_socket;
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'percona'@'localhost';
Query OK, 0 rows affected (0.01 sec)
اکنون می توانید با استفاده از نام کاربری "percona" وارد شوید.
[percona@ip-192-168-1-111 ~]$ whoami
percona
[percona@ip-192-168-1-111 ~]$ mysql -upercona
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 19
Server version: 8.0.16-7 Percona Server (GPL), Release 7, Revision 613e312
Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> select user, host, plugin, authentication_string from mysql.user where user ='percona';
+---------+-----------+-------------+-----------------------+
| user | host | plugin | authentication_string |
+---------+-----------+-------------+-----------------------+
| percona | localhost | auth_socket | |
+---------+-----------+-------------+-----------------------+
1 row in set (0.00 sec)
و دوباره کار کرد!
سوال: آیا می توان با همان ورود به سیستم percona، اما به عنوان یک کاربر متفاوت، وارد سیستم شد؟
[percona@ip-192-168-1-111 ~]$ logout
[root@ip-192-168-1-111 ~]# mysql -upercona
ERROR 1698 (28000): Access denied for user 'percona'@'localhost'
نه ، این کار نخواهد کرد
نتیجه
MySQL از چندین جنبه کاملاً انعطاف پذیر است که یکی از آنها روش احراز هویت است. همانطور که از این پست می بینید، بر اساس کاربران سیستم عامل بدون رمز عبور می توان دسترسی داشت. این می تواند در سناریوهای خاصی مفید باشد، و یکی از آنها هنگام مهاجرت از RDS/Aurora به MySQL معمولی با استفاده از
منبع: www.habr.com