برای مشاهده یافته ها از کلید Enter و برای خروج از کلید Esc استفاده کنید.

بهینه سازی و افزایش سرعت و امنیت سرویس MySQL و MariaDB روی لینوکس

قبلاً در مطلب راهنمای نصب MariaDB روی سرور CentOS با نحوه نصب و پیکربندی سرویس MariaDB که یک انشعاب از MySQL است، آشنا شدیم. در این مطلب قصد داریم به صورت تخصصی‌تر و با استفاده از یک ابزار قدرتمند، سرویس MySQL یا MariaDB یا Percona را بهینه و امن کنیم.

هرچند ابزار معرفی شده در این مطلب با MySQL یا MariaDB یا Percona سازگار است، ولی این مطلب منطبق بر MariaDB روی CentOS 8 نگارش شده است. البته جای نگرانی نیست، چون تفاوت فقط در مسیرهای فایل های پیکربندی می‌باشد.

پیش‌نیازها

  1. داشتن اطلاعات دسترسی به حساب کاربری root در mysql یا mariadb
  2. داشتن دسترسی root سیستم عامل یا دسترسی sudo برای ویرایش فایل های پیکربندی mysql یا mariadb
  3. فعال بودن Perl نسخه ۵.۶ یا جدیدتر

نصب بسته های پیش‌نیاز

برای ادامه کار بهینه سازی می بایست ابزارهای پیشنیاز روی سیستم عامل نصب شود. در این راهنما دستورات مربوط به سیستم عامل CentOS 8 ارائه شده است. با دستورات زیر می توانید پیش‌نیاز های لازم برای ادامه کار را نصب کنید:

sudo dnf update -y
sudo dnf install vim wget -y

دریافت ابزار MySQLTuner

ابزاری که برای بهینه سازی سرویس MySQL یا MariaDB یا Percona استفاده می کنیم، یک اسکریپت مبتنی بر زبان perl است که آخرین نسخه آن با استفاده از دستورات زیر دریافت می شود:

wget http://mysqltuner.pl/ -O mysqltuner.pl
wget https://raw.githubusercontent.com/major/MySQLTuner-perl/master/basic_passwords.txt -O basic_passwords.txt
wget https://raw.githubusercontent.com/major/MySQLTuner-perl/master/vulnerabilities.csv -O vulnerabilities.csv
chmod +x mysqltuner.pl

اجرای اسکریپت MySQLTuner

برای اجرای اسکریپت MySQLTuner کافیست در مسیری که این ابزار دانلود شده است، دستور زیر را اجرا کنید و حساب کاربر root سرویس MySQL یا MariaDB یا Percona و رمز مربوطه را اجرا کنید و منتظر نتیجه اجرا باشید:

sudo ./mysqltuner.pl

اگر همه چیز درست باشد، نتیجه چیزی شبیه به زیر است:

[root@CentOS8 ~]# ./mysqltuner.pl
 >>  MySQLTuner 1.7.19 - Major Hayden <major@mhtx.net>
 >>  Bug reports, feature requests, and downloads at http://mysqltuner.com/
 >>  Run with '--help' for additional options and output filtering

[--] Skipped version check for MySQLTuner script
Please enter your MySQL administrative login: root
Please enter your MySQL administrative password: [OK] Currently running supported MySQL version 10.3.17-MariaDB
[OK] Operating on 64-bit architecture

-------- Log file Recommendations ------------------------------------------------------------------
[OK] Log file /var/log/mariadb/mariadb.log exists
[--] Log file: /var/log/mariadb/mariadb.log(3K)
[OK] Log file /var/log/mariadb/mariadb.log is readable.
[OK] Log file /var/log/mariadb/mariadb.log is not empty
[OK] Log file /var/log/mariadb/mariadb.log is smaller than 32 Mb
[!!] /var/log/mariadb/mariadb.log contains 5 warning(s).
[!!] /var/log/mariadb/mariadb.log contains 2 error(s).
[--] ۱ start(s) detected in /var/log/mariadb/mariadb.log
[--] ۱) ۲۰۲۰-۰۶-۰۳  ۵:۴۴:۴۲ ۰ [Note] /usr/libexec/mysqld: ready for connections.
[--] ۱ shutdown(s) detected in /var/log/mariadb/mariadb.log
[--] ۱) ۲۰۲۰-۰۶-۰۳  ۵:۴۴:۴۱ ۰ [Note] /usr/libexec/mysqld: Shutdown complete

-------- Storage Engine Statistics -----------------------------------------------------------------
[--] Status: +ARCHIVE +Aria +BLACKHOLE +CSV +FEDERATED +InnoDB +MEMORY +MRG_MyISAM +MyISAM +PERFORMANCE_SCHEMA +SEQUENCE
[--] Data in InnoDB tables: 24.4M (Tables: 263)
[OK] Total fragmented tables: 0

-------- Analysis Performance Metrics --------------------------------------------------------------
[--] innodb_stats_on_metadata: OFF
[OK] No stat updates during querying INFORMATION_SCHEMA.

-------- Security Recommendations ------------------------------------------------------------------
[OK] There are no anonymous accounts for any database users
[OK] All database users have passwords assigned
[--] There are 620 basic passwords in the list.

-------- CVE Security Recommendations --------------------------------------------------------------
[OK] NO SECURITY CVE FOUND FOR YOUR VERSION

-------- Performance Metrics -----------------------------------------------------------------------
[--] Up for: 6m 53s (1K q [3.765 qps], 671 conn, TX: 682K, RX: 402K)
[--] Reads / Writes: 96% / 4%
[--] Binary logging is disabled
[--] Physical Memory     : 3.7G
[--] Max MySQL memory    : 6.1G
[--] Other process memory: 0B
[--] Total buffers: 928.0M global + 34.7M per thread (151 max threads)
[--] P_S Max memory usage: 104M
[--] Galera GCache Max memory usage: 0B
[OK] Maximum reached memory usage: 1.1G (29.13% of installed RAM)
[!!] Maximum possible memory usage: 6.1G (165.74% of installed RAM)
[!!] Overall possible memory usage with other process exceeded memory
[OK] Slow queries: 0% (0/1K)
[OK] Highest usage of available connections: 1% (2/151)
[OK] Aborted connections: 0.30%  (2/671)
[!!] name resolution is active : a reverse name resolution is made for each new connection and can reduce performance
[!!] Query cache may be disabled by default due to mutex contention.
[!!] Query cache efficiency: 5.7% (48 cached / 837 selects)
[OK] Query cache prunes per day: 0
[OK] Sorts requiring temporary tables: 0% (0 temp sorts / 48 sorts)
[!!] Joins performed without indexes: 3
[!!] Temporary tables created on disk: 38% (37 on disk / 96 total)
[OK] Thread cache hit rate: 99% (3 created / 671 connections)
[OK] Table cache hit rate: 98% (346 open / 352 opened)
[!!] table_definition_cache(400) is lower than number of tables(422)
[OK] Open file limit used: 5% (59/1K)
[OK] Table locks acquired immediately: 100% (642 immediate / 642 locks)

-------- Performance schema ------------------------------------------------------------------------
[--] Memory used by P_S: 104.0M
[--] Sys schema isn't installed.

-------- ThreadPool Metrics ------------------------------------------------------------------------
[--] ThreadPool stat is enabled.
[--] Thread Pool Size: 4 thread(s).
[--] Using default value is good enough for your version (10.3.17-MariaDB)

-------- MyISAM Metrics ----------------------------------------------------------------------------
[!!] Key buffer used: 18.2% (24M used / 134M cache)
[OK] Key buffer size / total MyISAM indexes: 128.0M/118.0K
[!!] Read Key buffer hit rate: 80.0% (10 cached / 2 reads)

-------- InnoDB Metrics ----------------------------------------------------------------------------
[--] InnoDB is enabled.
[--] InnoDB Thread Concurrency: 0
[OK] InnoDB File per table is activated
[OK] InnoDB buffer pool / data size: 128.0M/24.4M
[!!] Ratio InnoDB log file size / InnoDB Buffer pool size (75 %): 48.0M * 2/128.0M should be equal to 25%
[OK] InnoDB buffer pool instances: 1
[--] Number of InnoDB Buffer Pool Chunk : 1 for 1 Buffer Pool Instance(s)
[OK] Innodb_buffer_pool_size aligned with Innodb_buffer_pool_chunk_size & Innodb_buffer_pool_instances
[!!] InnoDB Read buffer efficiency: 88.22% (17313 hits/ 19625 total)
[!!] InnoDB Write Log efficiency: 77.78% (14 hits/ 18 total)
[OK] InnoDB log waits: 0.00% (0 waits / 32 writes)

-------- AriaDB Metrics ----------------------------------------------------------------------------
[--] AriaDB is enabled.
[OK] Aria pagecache size / total Aria indexes: 128.0M/1B
[!!] Aria pagecache hit rate: 94.5% (653 cached / 36 reads)

-------- TokuDB Metrics ----------------------------------------------------------------------------
[--] TokuDB is disabled.

-------- XtraDB Metrics ----------------------------------------------------------------------------
[--] XtraDB is disabled.

-------- Galera Metrics ----------------------------------------------------------------------------
[--] Galera is disabled.

-------- Replication Metrics -----------------------------------------------------------------------
[--] Galera Synchronous replication: NO
[--] No replication slave(s) for this server.
[--] Binlog format: MIXED
[--] XA support enabled: ON
[--] Semi synchronous replication Master: OFF
[--] Semi synchronous replication Slave: OFF
[--] This is a standalone server

-------- Recommendations ---------------------------------------------------------------------------
General recommendations:
    Control warning line(s) into /var/log/mariadb/mariadb.log file
    Control error line(s) into /var/log/mariadb/mariadb.log file
    MySQL was started within the last 24 hours - recommendations may be inaccurate
    Reduce your overall MySQL memory footprint for system stability
    Dedicate this server to your database for highest performance.
    Configure your accounts with ip or subnets only, then update your configuration with skip-name-resolve=1
    We will suggest raising the 'join_buffer_size' until JOINs not using indexes are found.
             See https://dev.mysql.com/doc/internals/en/join-buffer-size.html
             (specially the conclusions at the bottom of the page).
    When making adjustments, make tmp_table_size/max_heap_table_size equal
    Reduce your SELECT DISTINCT queries which have no LIMIT clause
    Consider installing Sys schema from https://github.com/mysql/mysql-sys for MySQL
    Consider installing Sys schema from https://github.com/FromDual/mariadb-sys for MariaDB
    Before changing innodb_log_file_size and/or innodb_log_files_in_group read this: https://bit.ly/2TcGgtU
Variables to adjust:
  *** MySQL's maximum memory usage is dangerously high ***
  *** Add RAM before increasing MySQL buffer variables ***
    query_cache_size (=0)
    query_cache_type (=0)
    query_cache_limit (> 32M, or use smaller result sets)
    join_buffer_size (> 16.0M, or always use indexes with JOINs)
    tmp_table_size (> 16M)
    max_heap_table_size (> 16M)
    table_definition_cache(400) > 422 or -1 (autosizing if supported)
    innodb_log_file_size should be (=16M) if possible, so InnoDB total log files size equals to 25% of buffer pool size.

اعمال تنظیمات پیشنهادی

این ابزار به خودی خود، هیچ تغییری روی پیکربندی سرویس MySQL یا MariaDB یا Percona ایجاد نمی کند و فقط توصیه هایی برای بهینه سازی ارائه می کند. این توصیه ها در قسمت انتهایی خروجی این ابزار ارائه می شود. نمونه از این خروجی در تصویر زیر نشان داده شده است:

MySQLTuner

 

شما می توانید توصیه های ارائه شده را در فایل پیکربندی MySQL یا MariaDB یا Percona اعمال کنید، به عنوان مثال فایل پیکربندی MariaDB 10.3 که روی CentOS 8 نصب شده است، در مسیر زیر قراردارد:

/etc/my.cnf.d/mariadb-server.cnf

نکات مهم و کلیدی در استفاده از این ابزار

برای استفاده از این ابزار، پیشنهاد می شود به نکات زیر توجه کنید:

  1. قبل از اجرای این اسکریپت، سرویس MySQL یا MariaDB یا Percona حداقل ۲۴ ساعت فعال بوده و همچنین بهتر است زیر بار واقعی قرار داشته باشد.
  2. اگر از MariaDB 10.x استفاده می کنید، performance_schema را فعال کنید. برای اینکار عبارت performance_schema = on را در بخش [mysqld] در فایل پیکربندی mariadb قرار دهید.
  3. مشکلاتی که در طراحی پایگاه داده وجود دارد با بهینه سازی سرویس حل نمی شود، پس روی اصلاح ساختار پایگاه داده و مدل داده ها تمرکز کنید که تاثیر بسیار چشم‌گیرتری در افزایش کارایی خواد داشت.
  4. خیلی وقت‌ها خود شما از ابزار بهتر می‌فهمید! پس ممکن است تشخیض شما در اعمال برخی از تنظیمات بهتر از ابزار باشد. به خودتان اعتماد کنید.

 

نسخه های سازگار با این اسکریپت

اسکریپت MySQLTuner با نسخه های زیر از MySQL یا MariaDB یا Percona سازگار است:

MySQL 8 (full support, password checks don’t work)MySQL 5.7 (full support)
MySQL 5.6 (full support)
MySQL 5.5 (full support)
MariaDB 10.4 (full support)
MariaDB 10.3 (full support)
MariaDB 10.2 (full support)
MariaDB 10.1 (full support)
MariaDB 10.0 (full support, 6 last month support)
MariaDB 5.5 (full support, no more MariaDB support)
Percona Server 8.0 (full support, password checks don’t work)
Percona Server 5.7 (full support)
Percona Server 5.6 (full support)
Percona XtraDB cluster (partial support, no test environment)
Mysql Replications (partial support, no test environment)
Galera replication (partial support, no test environment)
MySQL 3.23, 4.0, 4.1, 5.0, 5.1, 5.5 (partial support – deprecated version)

خبر بد برای کاربران ویندوز!

درحال حاضر این اسکریپت با سیستم عامل windows سازگار نیست.