网站搜索

如何安装、安全和性能调优 MariaDB 数据库服务器


数据库服务器是当今应用程序所需的网络基础设施的关键组件。如果无法存储、检索、更新和删除数据(需要时),Web 和桌面应用程序的实用性和范围就会变得非常有限。

此外,了解如何安装、管理和配置数据库服务器(使其按预期运行)是每个系统管理员必须具备的基本技能。

在本文中,我们将简要回顾如何安装和保护 MariaDB 数据库服务器,然后我们将解释如何配置它。

安装和保护 MariaDB 服务器

CentOS 7.x中,MariaDB取代了MySQL,它仍然可以在Ubuntu中找到(与MariaDB一起)。 openSUSE 也是如此。

为简洁起见,我们在本教程中仅使用 MariaDB,但请注意,除了具有不同的名称和开发理念之外,这两个关系数据库管理系统RDBMS)简称强>)几乎相同。

这意味着 MySQLMariaDB 上的客户端命令是相同的,并且配置文件的命名和位置相同。

要安装 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 用户的密码。

安装上述软件包后,请确保数据库服务正在运行并且已激活以在启动时启动(在 CentOSopenSUSE 中,您将需要手动执行此操作,而在 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 root 用户的密码
  2. 删除匿名登录(从而仅允许具有有效帐户的用户登录到 RDBMS)
  3. 禁用 localhost 以外的计算机的 root 访问权限
  4. 删除测试数据库(任何人都可以访问)
  5. 激活与 1 到 4 相关的更改。

有关此过程的更详细描述,您可以参考在 RHEL/CentOS/Fedora 和 Debian/Ubuntu 中安装 MariaDB 数据库中的安装后部分。

配置 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.

端口=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 性能调整和优化技巧

一如既往,如果您对本文有任何疑问或意见,请随时告诉我们。您还喜欢使用其他服务器设置吗?请随意使用下面的评论表与社区其他人分享。