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一起)仍然可以找到MySQL。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
脚本。此过程将允许您:
- 为RDBMS根用户设置/重置密码。
- 移除匿名登录(仅允许具有有效帐户的用户登录RDBMS)。
- 禁用除本地主机之外的机器对root访问。
- 移除测试数据库(任何人都可以访问)。
- 激活与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 | 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.
我们将用20500替换默认值(3306)(但我们需要确保没有其他东西在使用该端口): 虽然有些人会认为通过混淆来保障安全不是一个好的做法,但将默认应用程序端口更改为更高的端口是一种基本但有效的方法,可以阻止端口扫描。 |
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/