A database server is an critical component of the network infrastructure necessary for today’s applications. Without the ability to store, retrieve, update, and delete data (when needed), the usefulness and scope of web and desktop apps becomes very limited.

安裝、管理和配置數據庫服務器(以使其正常運行)是每個系統管理員必須具備的基本技能。
在本文中,我們將簡要介紹如何安裝和保護 MariaDB 數據庫服務器,然後解釋如何配置它。
安裝和保護 MariaDB 服務器
在 CentOS 7.x 中,MariaDB 取代了 MySQL,在 Ubuntu 中(與 MariaDB 一起)。openSUSE 也是如此。
為了簡潔起見,在本教程中我們只使用 MariaDB,但請注意,除了有不同的名稱和開發理念外,這兩個 關係型數據庫管理系統(簡稱 RDBMSs)幾乎是相同的。
這意味著客戶端命令在 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 root 用戶輸入密碼。
安裝完成後,確保數據庫服務正在運行並已啟用以在啟動時啟動(在 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
腳本。此過程將允許您:
- 設置/重置 RDBMS root 用戶的密碼
- 刪除匿名登錄(因此僅允許具有有效帳戶的用戶登錄到 RDBMS)
- 禁用除本地主機外的機器的 root 訪問權限
- 刪除測試數據庫(任何人都可以訪問)
- 激活與 1 到 4 相關的更改。
有關此過程的更詳細描述,您可以參考 在 RHEL/CentOS/Fedora 和 Debian/Ubuntu 上安裝 MariaDB 數據庫 中的“安裝 MariaDB 數據庫”部分。
配置 MariaDB 服務器
默認配置選項按照給定順序從以下文件中讀取:/etc/mysql/my.cnf
、/etc/my.cnf
和 ~/.my.cnf
。
通常,只有 /etc/my.cnf
存在。我們將在這個文件中設置服務器範圍的設置(可以使用相同的設置在每個用戶的 ~/.my.cnf
中覆蓋)。
我們需要注意的第一件事是關於 my.cnf
的設置是組織成分類(或組)的,每個分類名稱都用方括號括起來。
服務器系統配置位於 [mysqld]
部分,通常您只會在下表中找到前兩個設置。其餘是其他常用選項(在指定時,我們將使用我們選擇的自定義值更改默認值):
Setting and description | Default value |
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).
我們將更改此設置以指示服務僅在其主要地址(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.
我們將將預設值(3306)更換為20500(但我們需要確保沒有其他東西在使用該端口): 儘管有些人會認為通過模糊來確保安全性並不是一種良好的做法,但將默認應用程序端口更改為更高端口是一種基本但有效的方法,可以阻止端口掃描。 |
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.
我們將將默認值更改為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.
除非您需要主機名來確定權限,否則建議禁用此變量(以加快連接和查詢速度)將其值設置為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.
您應該根據以下兩點來選擇與您的需求相匹配的查詢緩存大小:1)重複查詢的數量,以及2)這些重複查詢預計返回的記錄數。我們暫時將此值設置為100 MB: query_cache_size=100M |
query_cache_size=0 (which means it is disabled by default) |
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.
同樣,這取決於您預期的連接數。我們可以安全地將此值設置為最大連接數的一半: thread_cache_size=15 |
thread_cache_size=0 (disabled by default) |
在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 性能調整和優化提示
如往常一樣,如果您對本文有任何問題或意見,請隨時告訴我們。您還想使用其他伺服器設置嗎?請在下面的評論表單中與社區的其他成員分享。
Source:
https://www.tecmint.com/install-secure-performance-tuning-mariadb-database-server/