7月 012020
 

MariaDB SQL Mode对数据库操作语法的影响

https://mariadb.com/kb/en/sql-mode/#setting-sql_mode

自MariaDB 10.2.4版本后的默认SQL模式

STRICT_TRANS_TABLES, ERROR_FOR_DIVISION_BY_ZERO , NO_AUTO_CREATE_USER, NO_ENGINE_SUBSTITUTION

当SQL模式中配置NO_AUTO_CREATE_USER启用,执行GRANT授权时只有同时指定验证信息才能成功创建用户账户,否则就需要使用CREATE USER单独创建用户账户。

https://mariadb.com/kb/en/grant/#implicit-account-creation
https://mariadb.com/kb/en/create-user/
https://mariadb.com/kb/en/set-password/

查看当前数据库系统的SQL MODE设置

MariaDB [mysql]> select @@sql_mode;
+-------------------------------------------------------------------------------------------+
| @@sql_mode                                                                                |
+-------------------------------------------------------------------------------------------+
| STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------+
1 row in set (0.000 sec)

MariaDB [mysql]> select @@global.sql_mode;
+-------------------------------------------------------------------------------------------+
| @@global.sql_mode                                                                         |
+-------------------------------------------------------------------------------------------+
| STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------+
1 row in set (0.000 sec)

MariaDB [mysql]>

错误用法

MariaDB [mysql]> create database example;
Query OK, 1 row affected (0.000 sec)

MariaDB [mysql]> grant all on example.* to test_user@localhost;
ERROR 1133 (28000): Can't find any matching row in the user table
MariaDB [mysql]>

正确用法一

MariaDB [mysql]> grant all on example.* to test_user@localhost identified by 'testpwd';
Query OK, 0 rows affected (0.001 sec)

MariaDB [mysql]>

正确用法二

MariaDB [mysql]> create user test_user@localhost;
Query OK, 0 rows affected (0.001 sec)

MariaDB [mysql]> set password for test_user@localhost=password('testpwd');
Query OK, 0 rows affected (0.001 sec)

MariaDB [mysql]> grant all on example.* to test_user@localhost;
Query OK, 0 rows affected (0.000 sec)

MariaDB [mysql]>
6月 302020
 

MariaDB备份工具软件包信息

[root@centos-s-2vcpu-2gb-sfo3-01 ~]# dnf info mariadb-backup
Last metadata expiration check: 0:02:04 ago on Tue 30 Jun 2020 02:14:23 AM UTC.
Installed Packages
Name         : mariadb-backup
Epoch        : 3
Version      : 10.3.17
Release      : 1.module_el8.1.0+257+48736ea6
Architecture : x86_64
Size         : 28 M
Source       : mariadb-10.3.17-1.module_el8.1.0+257+48736ea6.src.rpm
Repository   : @System
From repo    : AppStream
Summary      : The mariabackup tool for physical online backups
URL          : http://mariadb.org
License      : GPLv2 with exceptions and LGPLv2 and BSD
Description  : MariaDB Backup is an open source tool provided by MariaDB for performing
             : physical online backups of InnoDB, Aria and MyISAM tables.
             : For InnoDB, "hot online" backups are possible.

[root@centos-s-2vcpu-2gb-sfo3-01 ~]#

备份工具命令行参数

[root@centos-s-2vcpu-2gb-sfo3-01 ~]# mariabackup
mariabackup based on MariaDB server 10.3.17-MariaDB Linux (x86_64)
Open source backup tool for InnoDB and XtraDB

Copyright (C) 2009-2015 Percona LLC and/or its affiliates.
Portions Copyright (C) 2000, 2011, MySQL AB & Innobase Oy. All Rights Reserved.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You can download full text of the license on http://www.gnu.org/licenses/gpl-2.0.txt

Usage: mariabackup [--defaults-file=#] [--backup | --prepare | --copy-back | --move-back] [OPTIONS]

Default options are read from the following files in the given order:
/etc/my.cnf ~/.my.cnf
The following groups are read: xtrabackup mariabackup mysqld server mysqld-10.3 mariadb mariadb-10.3 client-server galera
The following options may be given as the first argument:
--print-defaults          Print the program argument list and exit.
--no-defaults             Don't read default options from any option file.
The following specify which files/extra groups are read (specified before remaining options):
--defaults-file=#         Only read default options from the given file #.
--defaults-extra-file=#   Read this file after the global files are read.
--defaults-group-suffix=# Additionally read default groups with # appended as a suffix.
  -V, --verbose       display verbose output
  -v, --version       print xtrabackup version information
  --target-dir=name   destination directory
  --backup            take backup to target-dir
  --prepare           prepare a backup for starting mysql server on the backup.
  --export            create files to import to another database when prepare.
  --print-param       print parameter of mysqld needed for copyback.
  --use-memory=#      The value is used instead of buffer_pool_size
  --throttle=#        limit count of IO operations (pairs of read&write) per
                      second to IOS values (for '--backup')
  --log[=name]        Ignored option for MySQL option compatibility
  --log-copy-interval=#
                      time interval between checks done by log copying thread
                      in milliseconds (default is 1 second).
  --extra-lsndir=name (for --backup): save an extra copy of the
                      xtrabackup_checkpoints file in this directory.
  --incremental-lsn=name
                      (for --backup): copy only .ibd pages newer than specified
                      LSN 'high:low'. ##ATTENTION##: If a wrong LSN value is
                      specified, it is impossible to diagnose this, causing the
                      backup to be unusable. Be careful!
  --incremental-basedir=name
                      (for --backup): copy only .ibd pages newer than backup at
                      specified directory.
  --incremental-dir=name
                      (for --prepare): apply .delta files and logfile in the
                      specified directory.
  --tables=name       filtering by regexp for table names.
  --tables-file=name  filtering by list of the exact database.table name in the
                      file.
  --databases=name    filtering by list of databases.
  --databases-file=name
                      filtering by list of databases in the file.
  --tables-exclude=name
                      filtering by regexp for table names. Operates the same
                      way as --tables, but matched names are excluded from
                      backup. Note that this option has a higher priority than
                      --tables.
  --databases-exclude=name
                      Excluding databases based on name, Operates the same way
                      as --databases, but matched names are excluded from
                      backup. Note that this option has a higher priority than
                      --databases.
  --stream=name       Stream all backup files to the standard output in the
                      specified format.Supported format is 'xbstream'.
  --compress[=name]   Compress individual backup files using the specified
                      compression algorithm. Currently the only supported
                      algorithm is 'quicklz'. It is also the default algorithm,
                      i.e. the one used when --compress is used without an
                      argument.
  --compress-threads=#
                      Number of threads for parallel data compression. The
                      default value is 1.
  --compress-chunk-size=#
                      Size of working buffer(s) for compression threads in
                      bytes. The default value is 64K.
  --incremental-force-scan
                      Perform a full-scan incremental backup even in the
                      presence of changed page bitmap data
  --close-files       do not keep files opened. Use at your own risk.
  --core-file         Write core on fatal signals
  --copy-back         Copy all the files in a previously made backup from the
                      backup directory to their original locations.
  --move-back         Move all the files in a previously made backup from the
                      backup directory to the actual datadir location. Use with
                      caution, as it removes backup files.
  --galera-info       This options creates the xtrabackup_galera_info file
                      which contains the local node state at the time of the
                      backup. Option should be used when performing the backup
                      of MariaDB Galera Cluster. Has no effect when backup
                      locks are used to create the backup.
  --slave-info        This option is useful when backing up a replication slave
                      server. It prints the binary log position and name of the
                      master server. It also writes this information to the
                      "xtrabackup_slave_info" file as a "CHANGE MASTER"
                      command. A new slave for this master can be set up by
                      starting a slave server on this backup and issuing a
                      "CHANGE MASTER" command with the binary log position
                      saved in the "xtrabackup_slave_info" file.
  --no-lock           Use this option to disable table lock with "FLUSH TABLES
                      WITH READ LOCK". Use it only if ALL your tables are
                      InnoDB and you DO NOT CARE about the binary log position
                      of the backup. This option shouldn't be used if there are
                      any DDL statements being executed or if any updates are
                      happening on non-InnoDB tables (this includes the system
                      MyISAM tables in the mysql database), otherwise it could
                      lead to an inconsistent backup. If you are considering to
                      use --no-lock because your backups are failing to acquire
                      the lock, this could be because of incoming replication
                      events preventing the lock from succeeding. Please try
                      using --safe-slave-backup to momentarily stop the
                      replication slave thread, this may help the backup to
                      succeed and you then don't need to resort to using this
                      option.
  --safe-slave-backup Stop slave SQL thread and wait to start backup until
                      Slave_open_temp_tables in "SHOW STATUS" is zero. If there
                      are no open temporary tables, the backup will take place,
                      otherwise the SQL thread will be started and stopped
                      until there are no open temporary tables. The backup will
                      fail if Slave_open_temp_tables does not become zero after
                      --safe-slave-backup-timeout seconds. The slave SQL thread
                      will be restarted when the backup finishes.
  --rsync             Uses the rsync utility to optimize local file transfers.
                      When this option is specified, innobackupex uses rsync to
                      copy all non-InnoDB files instead of spawning a separate
                      cp for each file, which can be much faster for servers
                      with a large number of databases or tables.  This option
                      cannot be used together with --stream.
  --force-non-empty-directories
                      This option, when specified, makes --copy-back or
                      --move-back transfer files to non-empty directories. Note
                      that no existing files will be overwritten. If
                      --copy-back or --nove-back has to copy a file from the
                      backup directory which already exists in the destination
                      directory, it will still fail with an error.
  --no-version-check  This option disables the version check which is enabled
                      by the --version-check option.
  --no-backup-locks   This option controls if backup locks should be used
                      instead of FLUSH TABLES WITH READ LOCK on the backup
                      stage. The option has no effect when backup locks are not
                      supported by the server. This option is enabled by
                      default, disable with --no-backup-locks.
  --decompress        Decompresses all files with the .qp extension in a backup
                      previously made with the --compress option.
  -u, --user=name     This option specifies the MySQL username used when
                      connecting to the server, if that's not the current user.
                      The option accepts a string argument. See mysql --help
                      for details.
  -H, --host=name     This option specifies the host to use when connecting to
                      the database server with TCP/IP.  The option accepts a
                      string argument. See mysql --help for details.
  -P, --port=#        This option specifies the port to use when connecting to
                      the database server with TCP/IP.  The option accepts a
                      string argument. See mysql --help for details.
  -p, --password=name This option specifies the password to use when connecting
                      to the database. It accepts a string argument.  See mysql
                      --help for details.
  --protocol=name     The protocol to use for connection (tcp, socket, pipe,
                      memory).
  -S, --socket=name   This option specifies the socket to use when connecting
                      to the local database server with a UNIX domain socket.
                      The option accepts a string argument. See mysql --help
                      for details.
  --incremental-history-name=name
                      This option specifies the name of the backup series
                      stored in the PERCONA_SCHEMA.xtrabackup_history history
                      record to base an incremental backup on. Xtrabackup will
                      search the history table looking for the most recent
                      (highest innodb_to_lsn), successful backup in the series
                      and take the to_lsn value to use as the starting lsn for
                      the incremental backup. This will be mutually exclusive
                      with --incremental-history-uuid, --incremental-basedir
                      and --incremental-lsn. If no valid lsn can be found (no
                      series by that name, no successful backups by that name)
                      xtrabackup will return with an error. It is used with the
                      --incremental option.
  --incremental-history-uuid=name
                      This option specifies the UUID of the specific history
                      record stored in the PERCONA_SCHEMA.xtrabackup_history to
                      base an incremental backup on.
                      --incremental-history-name, --incremental-basedir and
                      --incremental-lsn. If no valid lsn can be found (no
                      success record with that uuid) xtrabackup will return
                      with an error. It is used with the --incremental option.
  --remove-original   Remove .qp files after decompression.
  --ftwrl-wait-query-type=name
                      This option specifies which types of queries are allowed
                      to complete before innobackupex will issue the global
                      lock. Default is all.. One of: ALL, UPDATE, SELECT
  --kill-long-query-type=name
                      This option specifies which types of queries should be
                      killed to unblock the global lock. Default is "all".. One
                      of: ALL, UPDATE, SELECT
  --history[=name]    This option enables the tracking of backup history in the
                      PERCONA_SCHEMA.xtrabackup_history table. An optional
                      history series name may be specified that will be placed
                      with the history record for the current backup being
                      taken.
  --kill-long-queries-timeout=#
                      This option specifies the number of seconds innobackupex
                      waits between starting FLUSH TABLES WITH READ LOCK and
                      killing those queries that block it. Default is 0
                      seconds, which means innobackupex will not attempt to
                      kill any queries.
  --ftwrl-wait-timeout=#
                      This option specifies time in seconds that innobackupex
                      should wait for queries that would block FTWRL before
                      running it. If there are still such queries when the
                      timeout expires, innobackupex terminates with an error.
                      Default is 0, in which case innobackupex does not wait
                      for queries to complete and starts FTWRL immediately.
  --ftwrl-wait-threshold=#
                      This option specifies the query run time threshold which
                      is used by innobackupex to detect long-running queries
                      with a non-zero value of --ftwrl-wait-timeout. FTWRL is
                      not started until such long-running queries exist. This
                      option has no effect if --ftwrl-wait-timeout is 0.
                      Default value is 60 seconds.
  --debug-sleep-before-unlock=#
                      This is a debug-only option used by the XtraBackup test
                      suite.
  --safe-slave-backup-timeout=#
                      How many seconds --safe-slave-backup should wait for
                      Slave_open_temp_tables to become zero. (default 300)
  --binlog-info[=name]
                      This option controls how XtraBackup should retrieve
                      server's binary log coordinates corresponding to the
                      backup. Possible values are OFF, ON, LOCKLESS and AUTO.
                      See the XtraBackup manual for more information. One of:
                      off, lockless, on, auto
  --secure-auth       Refuse client connecting to server if it uses old
                      (pre-4.1.1) protocol.
                      (Defaults to on; use --skip-secure-auth to disable.)
  --ssl               Enable SSL for connection (automatically enabled with
                      other flags).
  --ssl-ca=name       CA file in PEM format (check OpenSSL docs, implies
                      --ssl).
  --ssl-capath=name   CA directory (check OpenSSL docs, implies --ssl).
  --ssl-cert=name     X509 cert in PEM format (implies --ssl).
  --ssl-cipher=name   SSL cipher to use (implies --ssl).
  --ssl-key=name      X509 key in PEM format (implies --ssl).
  --ssl-crl=name      Certificate revocation list (implies --ssl).
  --ssl-crlpath=name  Certificate revocation list path (implies --ssl).
  --ssl-verify-server-cert
                      Verify server's "Common Name" in its cert against
                      hostname used when connecting. This option is disabled by
                      default.
  -h, --datadir=name  Path to the database root.
  -t, --tmpdir=name   Path for temporary files. Several paths may be specified,
                      separated by a colon (:), in this case they are used in a
                      round-robin fashion.
  --parallel=#        Number of threads to use for parallel datafiles transfer.
                      The default value is 1.
  --extended-validation
                      Enable extended validation for Innodb data pages during
                      backup phase. Will slow down backup considerably, in case
                      encryption is used. May fail if tables are created during
                      the backup.
  --encrypted-backup  In --backup, assume that nonzero key_version implies that
                      the page is encrypted. Use --backup
                      --skip-encrypted-backup to allow copying unencrypted that
                      were originally created before MySQL 5.1.48.
                      (Defaults to on; use --skip-encrypted-backup to disable.)
  --log[=name]        Ignored option for MySQL option compatibility
  --log-bin[=name]    Base name for the log sequence
  --innodb[=name]     Ignored option for MySQL option compatibility
  --innodb-adaptive-hash-index
                      Enable InnoDB adaptive hash index (enabled by default).
                      Disable with --skip-innodb-adaptive-hash-index.
                      (Defaults to on; use --skip-innodb-adaptive-hash-index to disable.)
  --innodb-autoextend-increment=#
                      Data file autoextend increment in megabytes
  --innodb-data-file-path=name
                      Path to individual files and their sizes.
  --innodb-data-home-dir=name
                      The common part for InnoDB table spaces.
  --innodb-doublewrite
                      Enable InnoDB doublewrite buffer during --prepare.
  --innodb-io-capacity[=#]
                      Number of IOPs the server can do. Tunes the background IO
                      rate
  --innodb-file-io-threads=#
                      Number of file I/O threads in InnoDB.
  --innodb-read-io-threads=#
                      Number of background read I/O threads in InnoDB.
  --innodb-write-io-threads=#
                      Number of background write I/O threads in InnoDB.
  --innodb-file-per-table
                      Stores each InnoDB table to an .ibd file in the database
                      dir.
  --innodb-flush-method=name
                      With which method to flush data.. One of: fsync, O_DSYNC,
                      littlesync, nosync, O_DIRECT, O_DIRECT_NO_FSYNC
  --innodb-log-buffer-size=#
                      The size of the buffer which InnoDB uses to write log to
                      the log files on disk.
  --innodb-log-file-size=#
                      Ignored for mysqld option compatibility
  --innodb-log-files-in-group=#
                      Ignored for mysqld option compatibility
  --innodb-log-group-home-dir=name
                      Path to InnoDB log files.
  --innodb-max-dirty-pages-pct=#
                      Percentage of dirty pages allowed in bufferpool.
  --innodb-use-native-aio
                      Use native AIO if supported on this platform.
                      (Defaults to on; use --skip-innodb-use-native-aio to disable.)
  --innodb-page-size=#
                      The universal page size of the database.
  --innodb-buffer-pool-filename=name
                      Ignored for mysqld option compatibility
  --debug-sync=name   Debug sync point. This is only used by the xtrabackup
                      test suite
  --innodb-checksum-algorithm=name
                      The algorithm InnoDB uses for page checksumming. [CRC32,
                      STRICT_CRC32, INNODB, STRICT_INNODB, NONE, STRICT_NONE].
                      One of: crc32, strict_crc32, innodb, strict_innodb, none,
                      strict_none
  --innodb-undo-directory=name
                      Directory where undo tablespace files live, this path can
                      be absolute.
  --innodb-undo-tablespaces=#
                      Number of undo tablespaces to use.
  --innodb-compression-level=#
                      Compression level used for zlib compression.
  --defaults-group=name
                      defaults group in config file (default "mysqld").
  --plugin-dir=name   Server plugin directory. Used to load encryption plugin
                      during 'prepare' phase.Has no effect in the 'backup'
                      phase (plugin directory during backup is the same as
                      server's)
  --innodb-log-checksums
                      Whether to require checksums for InnoDB redo log blocks
                      (Defaults to on; use --skip-innodb-log-checksums to disable.)
  --open-files-limit=#
                      the maximum number of file descriptors to reserve with
                      setrlimit().
  --lock-ddl-per-table
                      Lock DDL for each table before xtrabackup starts to copy
                      it and until the backup is completed.
  --rocksdb-datadir=name
                      RocksDB data directory.This option is only  used with
                      --copy-back or --move-back option
  --rocksdb-backup    Backup rocksdb data, if rocksdb plugin is installed.Used
                      only with --backup option. Can be useful for partial
                      backups, to exclude all rocksdb data
                      (Defaults to on; use --skip-rocksdb-backup to disable.)
  --check-privileges  Check database user privileges fro the backup user
                      (Defaults to on; use --skip-check-privileges to disable.)

Variables (--variable-name=value)
and boolean options {FALSE|TRUE}  Value (after reading options)
--------------------------------- ----------------------------------------
datadir                           /var/lib/mysql
tmpdir                            /var/tmp
parallel                          1
extended-validation               FALSE
encrypted-backup                  TRUE
log                               (No default value)
log-bin                           (No default value)
innodb                            (No default value)
innodb-adaptive-hash-index        TRUE
innodb-autoextend-increment       8
innodb-data-file-path             (No default value)
innodb-data-home-dir              (No default value)
innodb-doublewrite                FALSE
innodb-io-capacity                200
innodb-file-io-threads            4
innodb-read-io-threads            4
innodb-write-io-threads           4
innodb-file-per-table             FALSE
innodb-flush-method               fsync
innodb-log-buffer-size            1048576
innodb-log-file-size              50331648
innodb-log-files-in-group         1
innodb-log-group-home-dir         (No default value)
innodb-max-dirty-pages-pct        90
innodb-use-native-aio             TRUE
innodb-page-size                  16384
innodb-buffer-pool-filename       (No default value)
debug-sync                        (No default value)
innodb-checksum-algorithm         crc32
innodb-undo-directory             (No default value)
innodb-undo-tablespaces           0
innodb-compression-level          6
defaults-group                    mysqld
plugin-dir                        (No default value)
innodb-log-checksums              TRUE
open-files-limit                  0
lock-ddl-per-table                FALSE
rocksdb-datadir                   (No default value)
rocksdb-backup                    TRUE
check-privileges                  TRUE

Variables (--variable-name=value)
and boolean options {FALSE|TRUE}  Value (after reading options)
--------------------------------- ----------------------------------------
verbose                           FALSE
version                           FALSE
target-dir                        /root/xtrabackup_backupfiles/
backup                            FALSE
prepare                           FALSE
export                            FALSE
print-param                       FALSE
use-memory                        104857600
throttle                          0
log                               (No default value)
log-copy-interval                 1000
extra-lsndir                      (No default value)
incremental-lsn                   (No default value)
incremental-basedir               (No default value)
incremental-dir                   (No default value)
tables                            (No default value)
tables-file                       (No default value)
databases                         (No default value)
databases-file                    (No default value)
tables-exclude                    (No default value)
databases-exclude                 (No default value)
stream                            (No default value)
compress                          (No default value)
compress-threads                  1
compress-chunk-size               65536
incremental-force-scan            FALSE
close-files                       FALSE
copy-back                         FALSE
move-back                         FALSE
galera-info                       FALSE
slave-info                        FALSE
no-lock                           FALSE
safe-slave-backup                 FALSE
rsync                             FALSE
force-non-empty-directories       FALSE
no-version-check                  FALSE
no-backup-locks                   FALSE
decompress                        FALSE
user                              (No default value)
host                              (No default value)
port                              0
socket                            (No default value)
incremental-history-name          (No default value)
incremental-history-uuid          (No default value)
remove-original                   FALSE
ftwrl-wait-query-type             ALL
kill-long-query-type              SELECT
kill-long-queries-timeout         0
ftwrl-wait-timeout                0
ftwrl-wait-threshold              60
debug-sleep-before-unlock         0
safe-slave-backup-timeout         300
binlog-info                       on
secure-auth                       TRUE
ssl                               FALSE
ssl-ca                            (No default value)
ssl-capath                        (No default value)
ssl-cert                          (No default value)
ssl-cipher                        (No default value)
ssl-key                           (No default value)
ssl-crl                           (No default value)
ssl-crlpath                       (No default value)
ssl-verify-server-cert            FALSE
[root@centos-s-2vcpu-2gb-sfo3-01 ~]#
6月 232020
 

CentOS 8 Mariadb Server系统资源限制配置

https://mariadb.com/kb/en/configuring-linux-for-mariadb/
https://mariadb.com/kb/en/systemd/#configuring-the-open-files-limit

方案一

在系统层面配置openfile值(默认值为1024)

[root@centos-s-1vcpu-2gb-sfo3-01 ~]# ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7182
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 7182
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
[root@centos-s-1vcpu-2gb-sfo3-01 ~]#

使用通配符或用户名进行配置

[root@centos-s-1vcpu-2gb-sfo3-01 ~]# vi /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535

mysql soft nofile 65535
mysql hard nofile 65535

确认修改成功

[root@centos-s-1vcpu-2gb-sfo3-01 ~]# ulimit -n
65535
[root@centos-s-1vcpu-2gb-sfo3-01 ~]# ulimit -Sn
65535
[root@centos-s-1vcpu-2gb-sfo3-01 ~]# ulimit -Hn
65535
[root@centos-s-1vcpu-2gb-sfo3-01 ~]#

方案二

基于systemd的服务启动参数配置openfile值

Mariadb Server服务启动配置文件(默认不建议修改,会随软件包升级而覆盖)

[root@centos-s-1vcpu-2gb-sfo3-01 ~]# cat /usr/lib/systemd/system/mariadb.service
# It's not recommended to modify this file in-place, because it will be
# overwritten during package upgrades. If you want to customize, the
# best way is to create a file "/etc/systemd/system/mariadb.service",
# containing
# .include /usr/lib/systemd/system/mariadb.service
# ...make your changes here...
# or create a file "/etc/systemd/system/mariadb.service.d/foo.conf",
# which doesn't need to include ".include" call and which will be parsed
# after the file mariadb.service itself is parsed.
#
# For more info about custom unit files, see systemd.unit(5) or
# http://fedoraproject.org/wiki/Systemd#How_do_I_customize_a_unit_file.2F_add_a_custom_unit_file.3F

# For example, if you want to increase mysql's open-files-limit to 10000,
# you need to increase systemd's LimitNOFILE setting, so create a file named
# "/etc/systemd/system/mariadb.service.d/limits.conf" containing:
# [Service]
# LimitNOFILE=10000

# Note: /usr/lib/... is recommended in the .include line though /lib/...
# still works.
# Don't forget to reload systemd daemon after you change unit configuration:
# root> systemctl --system daemon-reload

# Use [mysqld.INSTANCENAME] as sections in my.cnf to configure this instance.

[Unit]
Description=MariaDB 10.3 database server
Documentation=man:mysqld(8)
Documentation=https://mariadb.com/kb/en/library/systemd/
After=network.target

[Install]
WantedBy=multi-user.target
Alias=mysql.service
Alias=mysqld.service

[Service]
Type=notify
User=mysql
Group=mysql

ExecStartPre=/usr/libexec/mysql-check-socket
# '%n' expands to 'Full unit name'; man systemd.unit
ExecStartPre=/usr/libexec/mysql-prepare-db-dir %n
# MYSQLD_OPTS here is for users to set in /etc/systemd/system/mariadb@.service.d/MY_SPECIAL.conf
# Note: we set --basedir to prevent probes that might trigger SELinux alarms,
# per bug #547485
ExecStart=/usr/libexec/mysqld --basedir=/usr $MYSQLD_OPTS $_WSREP_NEW_CLUSTER
ExecStartPost=/usr/libexec/mysql-check-upgrade

# Setting this to true can break replication and the Type=notify settings
# See also bind-address mysqld option.
PrivateNetwork=false

KillMode=process
KillSignal=SIGTERM

# Don't want to see an automated SIGKILL ever
SendSIGKILL=no

# Restart crashed server only, on-failure would also restart, for example, when
# my.cnf contains unknown option
Restart=on-abort
RestartSec=5s

UMask=007

# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300

# Place temp files in a secure directory, not /tmp
PrivateTmp=true
[root@centos-s-1vcpu-2gb-sfo3-01 ~]#

通用配置值(LimitNOFILE=infinity)

sudo tee /etc/systemd/system/mariadb.service.d/limitnofile.conf <<EOF
[Service]

LimitNOFILE=infinity
EOF
sudo systemctl daemon-reload

通用配置值infinity实际值来源

[centos@mariadb ~]$ cat /proc/sys/fs/nr_open
1048576
[centos@mariadb ~]$

建议配置值(较大的正整数)

sudo tee /etc/systemd/system/mariadb.service.d/limitnofile.conf <<EOF
[Service]

LimitNOFILE=1048576
EOF
sudo systemctl daemon-reload

Mairadb最大连接数参数详情

https://mariadb.com/docs/reference/mdb/system-variables/max_connections/#mdb-system-variables-max-connections
https://mariadb.com/kb/en/server-system-variables/#max_connections
Minimum Value 10
Maximum Value 100000
Default Value 151

修改Mariadb最大连接数配置(上限值)

[root@centos-s-1vcpu-2gb-sfo3-01 ~]# vi /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
max_connections = 100000
[root@centos-s-1vcpu-2gb-sfo3-01 ~]# systemctl restart mariadb
[root@centos-s-1vcpu-2gb-sfo3-01 ~]#

确认Mariadb最大连接数配置生效

MariaDB [(none)]> show variables like "max_connections";
+-----------------+--------+
| Variable_name   | Value  |
+-----------------+--------+
| max_connections | 100000 |
+-----------------+--------+
1 row in set (0.002 sec)

MariaDB [(none)]>