ค้นหาเว็บไซต์

วิธีการติดตั้ง การรักษาความปลอดภัย และการปรับแต่งประสิทธิภาพของเซิร์ฟเวอร์ฐานข้อมูล MariaDB


เซิร์ฟเวอร์ฐานข้อมูลเป็นองค์ประกอบสำคัญของโครงสร้างพื้นฐานเครือข่ายที่จำเป็นสำหรับแอปพลิเคชันในปัจจุบัน หากไม่มีความสามารถในการจัดเก็บ ดึงข้อมูล อัปเดต และลบข้อมูล (เมื่อจำเป็น) ประโยชน์และขอบเขตของแอปบนเว็บและเดสก์ท็อปก็จะถูกจำกัดอย่างมาก

นอกจากนี้ การรู้วิธีการติดตั้ง จัดการ และกำหนดค่าเซิร์ฟเวอร์ฐานข้อมูล (เพื่อให้ทำงานตามที่คาดหวัง) ถือเป็นทักษะสำคัญที่ผู้ดูแลระบบทุกคนต้องมี

ในบทความนี้ เราจะทบทวนวิธีการติดตั้งและรักษาความปลอดภัยเซิร์ฟเวอร์ฐานข้อมูล MariaDB โดยย่อ จากนั้นเราจะอธิบายวิธีการกำหนดค่า

การติดตั้งและการรักษาความปลอดภัยเซิร์ฟเวอร์ MariaDB

ใน CentOS 7.x นั้น MariaDB ได้เข้ามาแทนที่ MySQL ซึ่งยังคงพบได้ใน Ubuntu (พร้อมกับ MariaDB) เช่นเดียวกับ openSUSE

เพื่อความกระชับ เราจะใช้ MariaDB ในบทช่วยสอนนี้เท่านั้น แต่โปรดทราบว่านอกจากจะมีชื่อและปรัชญาการพัฒนาที่แตกต่างกันแล้ว ทั้ง ระบบการจัดการฐานข้อมูลเชิงสัมพันธ์ (RDBMS สั้น ๆ ) เกือบจะเหมือนกัน

ซึ่งหมายความว่าคำสั่งฝั่งไคลเอ็นต์เหมือนกันทั้งบน MySQL และ MariaDB และไฟล์การกำหนดค่าได้รับการตั้งชื่อและอยู่ในตำแหน่งเดียวกัน

ในการติดตั้ง MariaDB ให้ทำ:

--------------- On CentOS/RHEL 7 and Fedora 23 --------------- 
yum update && yum install mariadb mariadb-server # CentOS 

--------------- On Debian and Ubuntu --------------- 
sudo aptitude update && sudo aptitude install mariadb-client mariadb-server 

--------------- On openSUSE --------------- 
zypper update && zypper install mariadb mariadb-tools # openSUSE

โปรดทราบว่าใน Ubuntu คุณจะถูกขอให้ป้อนรหัสผ่านสำหรับผู้ใช้รูท RDBMS

เมื่อติดตั้งแพ็คเกจข้างต้นแล้ว ตรวจสอบให้แน่ใจว่าบริการฐานข้อมูลกำลังทำงานอยู่และเปิดใช้งานแล้วเพื่อเริ่มต้นการบูต (ใน CentOS และ openSUSE คุณจะต้องดำเนินการนี้ด้วยตนเอง ในขณะที่ Ubuntu กระบวนการติดตั้งจะจัดการให้คุณแล้ว):

--------------- On CentOS/RHEL 7 and Fedora 23 --------------- 
systemctl start mariadb && systemctl enable mariadb 

--------------- On openSUSE --------------- 
systemctl start mysql && systemctl enable mysql

จากนั้นเรียกใช้สคริปต์ mysql_secure_installation กระบวนการนี้จะช่วยให้คุณ:

  1. ตั้งค่า/รีเซ็ตรหัสผ่านสำหรับผู้ใช้รูท RDBMS
  2. ลบการเข้าสู่ระบบที่ไม่ระบุชื่อ (ทำให้เฉพาะผู้ใช้ที่มีบัญชีที่ถูกต้องเท่านั้นที่จะเข้าสู่ระบบ RDBMS)
  3. ปิดการใช้งานการเข้าถึงรูทสำหรับเครื่องอื่นที่ไม่ใช่ localhost
  4. ลบฐานข้อมูลทดสอบ (ซึ่งทุกคนสามารถเข้าถึงได้)
  5. เปิดใช้งานการเปลี่ยนแปลงที่เกี่ยวข้องกับ 1 ถึง 4

สำหรับคำอธิบายโดยละเอียดเพิ่มเติมของกระบวนการนี้ คุณสามารถดูส่วนหลังการติดตั้งในการติดตั้งฐานข้อมูล MariaDB ใน RHEL/CentOS/Fedora และ Debian/Ubuntu

การกำหนดค่าเซิร์ฟเวอร์ MariaDB

ตัวเลือกการกำหนดค่าเริ่มต้นจะอ่านจากไฟล์ต่อไปนี้ตามลำดับที่กำหนด: /etc/mysql/my.cnf, /etc/my.cnf และ ~ /.my.cnf.

ส่วนใหญ่แล้วจะมีเพียง /etc/my.cnf เท่านั้น ในไฟล์นี้เราจะตั้งค่าการตั้งค่าทั่วทั้งเซิร์ฟเวอร์ (ซึ่งสามารถแทนที่ได้ด้วยการตั้งค่าเดียวกันใน ~/.my.cnf สำหรับผู้ใช้แต่ละคน)

สิ่งแรกที่เราต้องทราบเกี่ยวกับ my.cnf คือการตั้งค่าจะถูกจัดเป็นหมวดหมู่ (หรือกลุ่ม) โดยที่ชื่อแต่ละหมวดหมู่จะอยู่ในวงเล็บเหลี่ยม

การกำหนดค่าระบบเซิร์ฟเวอร์ระบุไว้ในส่วน [mysqld] ซึ่งโดยทั่วไปคุณจะพบเพียงการตั้งค่าสองรายการแรกในตารางด้านล่าง ที่เหลือคือตัวเลือกอื่นๆ ที่ใช้บ่อย (ตามที่ระบุไว้ เราจะเปลี่ยนค่าเริ่มต้นด้วยค่าที่กำหนดเองที่เราเลือก):

Setting and description

ค่าเริ่มต้น

datadir is the directory where the data files are stored.

datadir=/var/lib/mysql

socket indicates the name and location of the socket file that is used for local client connections. Keep in mind that a socket file is a resource that is utilized to pass information between applications.

socket=/var/lib/mysql/mysql.sock

bind_address is the address where the database server will listen on for TCP/IP connections. If you need your server to listen on more than one IP address, leave out this setting (0.0.0.0 which means it will listen on all IP addresses assigned to this specific host).

We will change this to instruct the service to listen only on its main address (192.168.0.13):

bind_address=192.168.0.13

bind_address=0.0.0.0

port represents the port where the database server will be listening.

We will replace the default value(3306) with 20500 (but we need to make sure nothing else is using that port):
port=20500

While some people will argue that security through obscurity is not good practice, changing the default application ports for higher ones is a rudimentary -yet effective- method to discourage port scans.

port=3306

innodb_buffer_pool_size is the buffer pool (in bytes) of memory that is allocated for data and indexes that are accessed frequently when using Innodb (which is the default in MariaDB) or XtraDB as storage engine.

We will replace the default value with 256 MB:

innodb_buffer_pool_size=256M

innodb_buffer_pool_size=134217728

skip_name_resolve indicates whether hostnames will be resolved or not on incoming connections. If set to 1, as we will do in this guide, only IP addresses.

Unless you require hostnames to determine permissions, it is advisable to disable this variable (in order to speed up connections and queries) by setting its value to 1:

skip_name_resolve=1

skip_name_resolve=0

query_cache_size represents the size (in bytes) available to the query cache in disk, where the results of SELECT queries are stored for future use when an identical query (to the same database and using the same protocol and same character set) is performed.

You should choose a query cache size that matches your needs based on 1) the number of repetitive queries, and 2) the approximate number of records those repetitive queries are expected to return. We will set this value to 100 MB for the time being:

query_cache_size=100M

query_cache_size=0 (ซึ่งหมายความว่าถูกปิดใช้งานโดยค่าเริ่มต้น)

max_connections is the maximum number of simultaneous client connections to the server. We will set this value to 30:
max_connections=30Each connection will use a thread, and thus will consume memory. Take this fact into account while setting max_connections.

max_connections=151

thread_cache_size indicates the numbers of threads that the server allocates for reuse after a client disconnects and frees thread(s) previously in use. In this situation, it is cheaper (performance-wise) to reuse a thread than instantiating a new one.

Again, this depends on the number of connections you are expecting. We can safely set this value to half the number of max_connections:

thread_cache_size=15

thread_cache_size=0 (ปิดใช้งานโดยค่าเริ่มต้น)

ใน CentOS เราจะต้องบอก SELinux เพื่ออนุญาตให้ MariaDB ฟังบนพอร์ตที่ไม่ได้มาตรฐาน (20500 ) ก่อนที่จะเริ่มบริการใหม่:

yum install policycoreutils-python
semanage port -a -t mysqld_port_t -p tcp 20500

จากนั้นรีสตาร์ทบริการ MariaDB

การปรับแต่งประสิทธิภาพของ MariaDB

เพื่อช่วยเราตรวจสอบและปรับแต่งการกำหนดค่าตามความต้องการเฉพาะของเรา เราสามารถติดตั้ง mysqltuner (สคริปต์ที่จะให้คำแนะนำในการปรับปรุงประสิทธิภาพของเซิร์ฟเวอร์ฐานข้อมูลของเราและเพิ่มความเสถียร):

wget https://github.com/major/MySQLTuner-perl/tarball/master
tar xzf master

จากนั้นเปลี่ยนไดเร็กทอรีเป็นโฟลเดอร์ที่แยกมาจาก tarball (เวอร์ชันที่แน่นอนอาจแตกต่างกันในกรณีของคุณ):

cd major-MySQLTuner-perl-7dabf27

และเรียกใช้ (คุณจะได้รับแจ้งให้ป้อนข้อมูลประจำตัวของบัญชี MariaDB ผู้ดูแลระบบของคุณ)

./mysqltuner.pl

ผลลัพธ์ของสคริปต์ในตัวมันเองน่าสนใจมาก แต่ข้ามไปที่ด้านล่างสุดซึ่งมีรายการตัวแปรที่จะปรับพร้อมค่าที่แนะนำ:

การตั้งค่า query_cache_type ระบุว่าแคชการสืบค้นถูกปิดใช้งาน (0) หรือเปิดใช้งาน (1) ในกรณีนี้ mysqltuner แนะนำให้เราปิดการใช้งาน

เหตุใดเราจึงควรปิดการใช้งานตอนนี้? เหตุผลก็คือแคชการสืบค้นมีประโยชน์เป็นส่วนใหญ่ในสถานการณ์ที่มีการอ่านสูง/เขียนน้อย (ซึ่งไม่ใช่กรณีของเรา เนื่องจากเราเพิ่งติดตั้งเซิร์ฟเวอร์ฐานข้อมูล)

คำเตือน: ก่อนที่จะทำการเปลี่ยนแปลงการกำหนดค่าของเซิร์ฟเวอร์ที่ใช้งานจริง ขอแนะนำให้คุณปรึกษาผู้ดูแลระบบฐานข้อมูลที่เชี่ยวชาญเพื่อให้แน่ใจว่าคำแนะนำที่ได้รับจาก mysqltuner จะไม่ส่งผลกระทบในทางลบ ในการตั้งค่าที่มีอยู่

สรุป

ในบทความนี้ เราได้อธิบายวิธีกำหนดค่าเซิร์ฟเวอร์ฐานข้อมูล MariaDB หลังจากที่เราติดตั้งและรักษาความปลอดภัยแล้ว ตัวแปรการกำหนดค่าที่แสดงอยู่ในตารางด้านบนเป็นเพียงการตั้งค่าบางประการที่คุณอาจต้องการพิจารณาขณะเตรียมเซิร์ฟเวอร์สำหรับการใช้งานหรือเมื่อปรับแต่งในภายหลัง โปรดดูเอกสารอย่างเป็นทางการของ MariaDB ก่อนทำการเปลี่ยนแปลงหรือดูเคล็ดลับการปรับแต่งประสิทธิภาพ MariaDB ของเรา:

อย่าพลาด: 15 เคล็ดลับการปรับแต่งและเพิ่มประสิทธิภาพ MariaDB ที่มีประโยชน์

เช่นเคย อย่าลังเลที่จะแจ้งให้เราทราบหากคุณมีคำถามหรือความคิดเห็นเกี่ยวกับบทความนี้ มีการตั้งค่าเซิร์ฟเวอร์อื่น ๆ ที่คุณต้องการใช้หรือไม่? รู้สึกอิสระที่จะแบ่งปันกับส่วนที่เหลือของชุมชนโดยใช้แบบฟอร์มความคิดเห็นด้านล่าง