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)]>
3 月 042020
 

安装EPEL仓库

[root@ip-172-31-43-75 ~]# yum -y install epel-release
[root@ip-172-31-43-75 ~]# yum makecache

查看当前Redis服务版本信息

[root@ip-172-31-43-75 ~]# yum info redis
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: d36uatko69830t.cloudfront.net
 * epel: mirrors.aliyun.com
 * extras: d36uatko69830t.cloudfront.net
 * updates: d36uatko69830t.cloudfront.net
Available Packages
Name        : redis
Arch        : x86_64
Version     : 3.2.12
Release     : 2.el7
Size        : 544 k
Repo        : epel/x86_64
Summary     : A persistent key-value database
URL         : http://redis.io
License     : BSD
Description : Redis is an advanced key-value store. It is often referred to as a data
            : structure server since keys can contain strings, hashes, lists, sets and
            : sorted sets.
            : 
            : You can run atomic operations on these types, like appending to a string;
            : incrementing the value in a hash; pushing to a list; computing set
            : intersection, union and difference; or getting the member with highest
            : ranking in a sorted set.
            : 
            : In order to achieve its outstanding performance, Redis works with an
            : in-memory dataset. Depending on your use case, you can persist it either
            : by dumping the dataset to disk every once in a while, or by appending
            : each command to a log.
            : 
            : Redis also supports trivial-to-setup master-slave replication, with very
            : fast non-blocking first synchronization, auto-reconnection on net split
            : and so forth.
            : 
            : Other features include Transactions, Pub/Sub, Lua scripting, Keys with a
            : limited time-to-live, and configuration settings to make Redis behave like
            : a cache.
            : 
            : You can use Redis from most programming languages also.

[root@ip-172-31-43-75 ~]#

在主服务器上安装Redis服务

[root@ip-172-31-43-75 ~]# yum -y install redis

在主服务器上注册并启动服务

[root@ip-172-31-43-75 ~]# systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /usr/lib/systemd/system/redis.service.
[root@ip-172-31-43-75 ~]# systemctl start redis
[root@ip-172-31-43-75 ~]# redis-cli ping
PONG
[root@ip-172-31-43-75 ~]#

在主服务器上执行性能测试

[root@ip-172-31-43-75 ~]# redis-benchmark -q -n 1000 -c 10 -P 5
PING_INLINE: 333333.34 requests per second
PING_BULK: 499999.97 requests per second
SET: 499999.97 requests per second
GET: 333333.34 requests per second
INCR: 333333.34 requests per second
LPUSH: 333333.34 requests per second
RPUSH: 499999.97 requests per second
LPOP: 333333.34 requests per second
RPOP: 333333.34 requests per second
SADD: 333333.34 requests per second
HSET: 249999.98 requests per second
SPOP: 499999.97 requests per second
LPUSH (needed to benchmark LRANGE): 200000.00 requests per second
LRANGE_100 (first 100 elements): 45454.55 requests per second
LRANGE_300 (first 300 elements): 14084.51 requests per second
LRANGE_500 (first 450 elements): 6849.31 requests per second
LRANGE_600 (first 600 elements): 5102.04 requests per second
MSET (10 keys): 166666.67 requests per second

[root@ip-172-31-43-75 ~]#

在主服务器上查看端口监听

[root@ip-172-31-43-75 ~]# netstat -lnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp6       0      0 :::111                  :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
[root@ip-172-31-43-75 ~]#

在从服务器上安装并启动Redis服务

[root@ip-172-31-43-112 ~]# yum -y install epel-release
[root@ip-172-31-43-112 ~]# yum -y install redis

[root@ip-172-31-43-112 ~]# systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /usr/lib/systemd/system/redis.service.
[root@ip-172-31-43-112 ~]# systemctl start redis
[root@ip-172-31-43-112 ~]# redis-cli ping
PONG
[root@ip-172-31-43-112 ~]#

配置主服务器

[root@ip-172-31-43-75 ~]# vi /etc/redis.conf


# TCP keepalive.
#
# If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence
# of communication. This is useful for two reasons:
#
# 1) Detect dead peers.
# 2) Take the connection alive from the point of view of network
#    equipment in the middle.
#
# On Linux, the specified value (in seconds) is the period used to send ACKs.
# Note that to close the connection the double of the time is needed.
# On other kernels the period depends on the kernel configuration.
#
# A reasonable value for this option is 300 seconds, which is the new
# Redis default starting with Redis 3.2.1.
#tcp-keepalive 300
tcp-keepalive 60


################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 lookback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#bind 127.0.0.1
bind 172.31.43.75


# Require clients to issue AUTH <PASSWORD> before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass foobared
requirepass 43l4890fji24897usdkj4eyud834ksdhwl9


# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached. You can select among five behaviors:
#
# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key according to the LRU algorithm
# volatile-random -> remove a random key with an expire set
# allkeys-random -> remove a random key, any key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
# noeviction -> don't expire at all, just return an error on write operations
#
# Note: with any of the above policies, Redis will return an error on write
#       operations, when there are no suitable keys for eviction.
#
#       At the date of writing these commands are: set setnx setex append
#       incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd
#       sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby
#       zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby
#       getset mset msetnx exec sort
#
# The default is:
#
# maxmemory-policy noeviction
maxmemory-policy noeviction


# By default Redis asynchronously dumps the dataset on disk. This mode is
# good enough in many applications, but an issue with the Redis process or
# a power outage may result into a few minutes of writes lost (depending on
# the configured save points).
#
# The Append Only File is an alternative persistence mode that provides
# much better durability. For instance using the default data fsync policy
# (see later in the config file) Redis can lose just one second of writes in a
# dramatic event like a server power outage, or a single write if something
# wrong with the Redis process itself happens, but the operating system is
# still running correctly.
#
# AOF and RDB persistence can be enabled at the same time without problems.
# If the AOF is enabled on startup Redis will load the AOF, that is the file
# with the better durability guarantees.
#
# Please check http://redis.io/topics/persistence for more information.

#appendonly no
appendonly yes

# The name of the append only file (default: "appendonly.aof")

appendfilename "appendonly.aof"

在主服务器上重新启动Redis服务并查看端口监听

[root@ip-172-31-43-75 ~]# systemctl restart redis
[root@ip-172-31-43-75 ~]# netstat -lnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 172.31.43.75:6379       0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp6       0      0 :::111                  :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
[root@ip-172-31-43-75 ~]#

配置从服务器

[root@ip-172-31-43-112 ~]# vi /etc/redis.conf

#bind 127.0.0.1
bind 172.31.43.112

#tcp-keepalive 300
tcp-keepalive 60

requirepass 934jskls034ks73shdjd6

# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# 1) Redis replication is asynchronous, but you can configure a master to
#    stop accepting writes if it appears to be not connected with at least
#    a given number of slaves.
# 2) Redis slaves are able to perform a partial resynchronization with the
#    master if the replication link is lost for a relatively small amount of
#    time. You may want to configure the replication backlog size (see the next
#    sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
#    network partition slaves automatically try to reconnect to masters
#    and resynchronize with them.
#
# slaveof <masterip> <masterport>
slaveof 172.31.43.75 6379

# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the slave to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the slave request.
#
# masterauth <master-password>
masterauth 43l4890fji24897usdkj4eyud834ksdhwl9

重新启动从服务器Redis服务

[root@ip-172-31-43-112 ~]# systemctl restart redis

登录测试

[root@ip-172-31-43-112 ~]# redis-cli -h 172.31.43.112 -p 6379
172.31.43.112:6379> AUTH 934jskls034ks73shdjd6
OK
172.31.43.112:6379> exit
[root@ip-172-31-43-112 ~]#

在主服务器上检查主从复制状态

[root@ip-172-31-43-75 ~]# redis-cli -h 172.31.43.75 -p 6379
172.31.43.75:6379> AUTH 43l4890fji24897usdkj4eyud834ksdhwl9
OK
172.31.43.75:6379> INFO
# Server
redis_version:3.2.12
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:7897e7d0e13773f
redis_mode:standalone
os:Linux 3.10.0-957.1.3.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.8.5
process_id:19963
run_id:d9d36c6ad3afb25b4ea2ab1c9acfdbb1818c9e9a
tcp_port:6379
uptime_in_seconds:772
uptime_in_days:0
hz:10
lru_clock:6234889
executable:/usr/bin/redis-server
config_file:/etc/redis.conf

# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:1881968
used_memory_human:1.79M
used_memory_rss:8114176
used_memory_rss_human:7.74M
used_memory_peak:1917896
used_memory_peak_human:1.83M
total_system_memory:8094560256
total_system_memory_human:7.54G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:4.31
mem_allocator:jemalloc-3.6.0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1583292981
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
aof_enabled:1
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_current_size:0
aof_base_size:0
aof_pending_rewrite:0
aof_buffer_length:0
aof_rewrite_buffer_length:0
aof_pending_bio_fsync:0
aof_delayed_fsync:0

# Stats
total_connections_received:2
total_commands_processed:217
instantaneous_ops_per_sec:1
total_net_input_bytes:7756
total_net_output_bytes:478
instantaneous_input_kbps:0.04
instantaneous_output_kbps:0.01
rejected_connections:0
sync_full:1
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:772
migrate_cached_sockets:0

# Replication
role:master
connected_slaves:1
slave0:ip=172.31.43.112,port=6379,state=online,offset=309,lag=0
master_repl_offset:309
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:308

# CPU
used_cpu_sys:0.40
used_cpu_user:0.24
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

# Keyspace
172.31.43.75:6379>

主服务器故障或维护时将从服务器临时切换为主服务器角色

[root@ip-172-31-43-112 ~]# redis-cli -h 172.31.43.112 -p 6379 
172.31.43.112:6379> AUTH 934jskls034ks73shdjd6
OK
172.31.43.112:6379> SLAVEOF NO ONE
OK
172.31.43.112:6379> INFO
# Server
redis_version:3.2.12
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:7897e7d0e13773f
redis_mode:standalone
os:Linux 3.10.0-957.1.3.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.8.5
process_id:19834
run_id:094e98c730ce1571ba38ca222d4762d332e25e86
tcp_port:6379
uptime_in_seconds:561
uptime_in_days:0
hz:10
lru_clock:6235238
executable:/usr/bin/redis-server
config_file:/etc/redis.conf

# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:812536
used_memory_human:793.49K
used_memory_rss:6008832
used_memory_rss_human:5.73M
used_memory_peak:833488
used_memory_peak_human:813.95K
total_system_memory:8094560256
total_system_memory_human:7.54G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:7.40
mem_allocator:jemalloc-3.6.0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1583292981
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok

# Stats
total_connections_received:2
total_commands_processed:59
instantaneous_ops_per_sec:0
total_net_input_bytes:1027
total_net_output_bytes:19991
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0

# Replication
role:master
connected_slaves:0
master_repl_offset:785
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:0.27
used_cpu_user:0.22
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

# Keyspace
172.31.43.112:6379>

对于有多个从服务器集群的处理(在所有其他从节点执行)

SLAVEOF hostname port

主服务器恢复后再次切换回从服务器角色

172.31.43.112:6379> SLAVEOF 172.31.43.75 6379
OK
172.31.43.112:6379> INFO
# Server
redis_version:3.2.12
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:7897e7d0e13773f
redis_mode:standalone
os:Linux 3.10.0-957.1.3.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.8.5
process_id:19834
run_id:094e98c730ce1571ba38ca222d4762d332e25e86
tcp_port:6379
uptime_in_seconds:795
uptime_in_days:0
hz:10
lru_clock:6235472
executable:/usr/bin/redis-server
config_file:/etc/redis.conf

# Clients
connected_clients:2
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:833416
used_memory_human:813.88K
used_memory_rss:6012928
used_memory_rss_human:5.73M
used_memory_peak:833488
used_memory_peak_human:813.95K
total_system_memory:8094560256
total_system_memory_human:7.54G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:7.21
mem_allocator:jemalloc-3.6.0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1583292981
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok

# Stats
total_connections_received:2
total_commands_processed:62
instantaneous_ops_per_sec:0
total_net_input_bytes:1178
total_net_output_bytes:22251
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.02
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0

# Replication
role:slave
master_host:172.31.43.75
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:1121
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:0.37
used_cpu_user:0.31
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

# Keyspace
172.31.43.112:6379>

操作示例(复制)

172.31.43.75:6379> SET ABC 123465
OK
172.31.43.75:6379>

172.31.43.112:6379> GET ABC
"123465"
172.31.43.112:6379>
3 月 032020
 

使用Percona官方YUM仓库安装

$ yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm

使用MariaDB Tools仓库安装

[root@server3 ~]# yum list |grep percona
percona-xtrabackup.x86_64                   2.3.10-1.el7               mariadb-tools
percona-xtrabackup-24.x86_64                2.4.15-1.el7               mariadb-tools
[root@server3 ~]# 

[root@server3 ~]# yum info percona-xtrabackup-24
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: d36uatko69830t.cloudfront.net
 * extras: d36uatko69830t.cloudfront.net
 * updates: d36uatko69830t.cloudfront.net
Available Packages
Name        : percona-xtrabackup-24
Arch        : x86_64
Version     : 2.4.15
Release     : 1.el7
Size        : 7.5 M
Repo        : mariadb-tools/7/x86_64
Summary     : XtraBackup online backup for MySQL / InnoDB
URL         : http://www.percona.com/software/percona-xtrabackup
License     : GPLv2
Description : Percona XtraBackup is OpenSource online (non-blockable) backup solution for InnoDB and XtraDB
            : engines

[root@server3 ~]# 

[root@server3 ~]# yum -y install percona-xtrabackup-24

查看安装路径

[root@server3 ~]# rpm -lq percona-xtrabackup-24 
/usr/bin/innobackupex
/usr/bin/xbcloud
/usr/bin/xbcloud_osenv
/usr/bin/xbcrypt
/usr/bin/xbstream
/usr/bin/xtrabackup
/usr/lib64/xtrabackup/plugin/keyring_file.so
/usr/lib64/xtrabackup/plugin/keyring_vault.so
/usr/share/doc/percona-xtrabackup-24-2.4.15
/usr/share/doc/percona-xtrabackup-24-2.4.15/COPYING
/usr/share/man/man1/innobackupex.1.gz
/usr/share/man/man1/xbcrypt.1.gz
/usr/share/man/man1/xbstream.1.gz
/usr/share/man/man1/xtrabackup.1.gz
[root@server3 ~]#
3 月 072017
 

 

[mysqld]
ssl-ca=/etc/mysql-ssl/ca-cert.pem
ssl-cert=/etc/mysql-ssl/server-cert.pem
ssl-key=/etc/mysql-ssl/server-key.pem
ssl-cipher=AES128+EECDH:AES128+EDH
# replication:
GRANT REPLICATION SLAVE ON *.* to ‘repl’@’%’ REQUIRE SSL;
STOP SLAVE;
CHANGE MASTER MASTER_SSL=1,
MASTER_SSL_CA=’/etc/mysql-ssl/ca-cert.pem’,
MASTER_SSL_CERT=’/etc/mysql-ssl/client-cert.pem’,
MASTER_SSL_KEY=’/etc/mysql-ssl/client-key.pem';
SHOW SLAVE STATUS\G;
START SLAVE;
SHOW SLAVE STATUS\G;

内容引用:
(1)cipherlist

5 月 052016
 

在CentOS 6.7下安装MySQL 5.6版本

下载并安装官方Yum源

[root@localhost ~]# yum -y install http://dev.mysql.com/get/mysql57-community-release-el6-8.noarch.rpm

查看RPM安装包路径

[root@localhost ~]# rpm -lq mysql57-community-release-el6-8
/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
/etc/yum.repos.d/mysql-community-source.repo
/etc/yum.repos.d/mysql-community.repo
[root@localhost ~]#

查看并修改mysql-community.repo文件
启用(enable=1)需要安装的版本,禁用(enable=0)不需要的版本

[root@localhost ~]# vi /etc/yum.repos.d/mysql-community.repo
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-tools-community]
name=MySQL Tools Community
baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

# Enable to use MySQL 5.5
[mysql55-community]
name=MySQL 5.5 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/6/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-tools-preview]
name=MySQL Tools Preview
baseurl=http://repo.mysql.com/yum/mysql-tools-preview/el/6/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

查看仓库列表的启用及禁用状态

mysql-yum-repo-quick-guide-01

更新本地cache后查看mysql-community-server版本信息mysql-yum-repo-quick-guide-02

安装

相关引用:

MySQL Yum repository supports the following Linux Distros:

Red Hat Enterprise Linux 7 / Oracle Linux 7
Red Hat Enterprise Linux 6 / Oracle Linux 6
Red Hat Enterprise Linux 5 / Oracle Linux 5
Fedora 21, 22 and 23

MySQL Yum repository includes the latest packages:

 MySQL 5.7 (GA)
 MySQL 5.6 (GA)
 MySQL 5.5 (GA - Red Hat Enterprise Linux and Oracle Linux Only)
 MySQL Workbench
 MySQL Fabric
 MySQL Router
 MySQL Utilities
 MySQL Connector / ODBC
 MySQL Connector / Python
 MySQL Shell (preview)

下载地址
http://dev.mysql.com/downloads/repo/yum/

官方指南
http://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/

3 月 272015
 

使用脚本快速安装MariaDB YUM源仓库配置文件

https://mariadb.com/kb/en/mariadb-package-repository-setup-and-usage/

[root@ip-172-31-33-11 ~]# curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash
[info] Repository file successfully written to /etc/yum.repos.d/mariadb.repo
[info] Adding trusted package signing keys...
[info] Successfully added trusted package signing keys
[root@ip-172-31-33-11 ~]# ls /etc/yum.repos.d/
CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo
CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo mariadb.repo
[root@ip-172-31-33-11 ~]# cat /etc/yum.repos.d/mariadb.repo 
[mariadb-main]
name = MariaDB Server
baseurl = https://downloads.mariadb.com/MariaDB/mariadb-10.4/yum/rhel/$releasever/$basearch
gpgkey = file:///etc/pki/rpm-gpg/MariaDB-Server-GPG-KEY
gpgcheck = 1
enabled = 1


[mariadb-maxscale]
# To use the latest stable release of MaxScale, use "latest" as the version
# To use the latest beta (or stable if no current beta) release of MaxScale, use "beta" as the version
name = MariaDB MaxScale
baseurl = https://downloads.mariadb.com/MaxScale/2.4/centos/$releasever/$basearch
gpgkey = file:///etc/pki/rpm-gpg/MariaDB-MaxScale-GPG-KEY
gpgcheck = 1
enabled = 1

[mariadb-tools]
name = MariaDB Tools
baseurl = https://downloads.mariadb.com/Tools/rhel/$releasever/$basearch
gpgkey = file:///etc/pki/rpm-gpg/MariaDB-Enterprise-GPG-KEY
gpgcheck = 1
enabled = 1
[root@ip-172-31-33-11 ~]#

查看版本信息

[root@ip-172-31-33-11 ~]# yum info MariaDB-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: d36uatko69830t.cloudfront.net
 * extras: d36uatko69830t.cloudfront.net
 * updates: d36uatko69830t.cloudfront.net
Available Packages
Name        : MariaDB-server
Arch        : x86_64
Version     : 10.4.12
Release     : 1.el7.centos
Size        : 26 M
Repo        : mariadb-main/7/x86_64
Summary     : MariaDB: a very fast and robust SQL database server
URL         : http://mariadb.org
License     : GPLv2
Description : MariaDB: a very fast and robust SQL database server
            : 
            : It is GPL v2 licensed, which means you can use the it free of charge under the
            : conditions of the GNU General Public License Version 2 (http://www.gnu.org/licenses/).
            : 
            : MariaDB documentation can be found at https://mariadb.com/kb
            : MariaDB bug reports should be submitted through https://jira.mariadb.org

Name        : mariadb-server
Arch        : x86_64
Epoch       : 1
Version     : 5.5.64
Release     : 1.el7
Size        : 11 M
Repo        : base/7/x86_64
Summary     : The MariaDB server and related files
URL         : http://mariadb.org
License     : GPLv2 with exceptions and LGPLv2 and BSD
Description : MariaDB is a multi-user, multi-threaded SQL database server. It is a
            : client/server implementation consisting of a server daemon (mysqld)
            : and many different client programs and libraries. This package contains
            : the MariaDB server and some accompanying files and directories.
            : MariaDB is a community developed branch of MySQL.

[root@ip-172-31-33-11 ~]#

查看galera复制引擎的版本信息

[root@ip-172-31-33-11 ~]# yum info galera-4
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: d36uatko69830t.cloudfront.net
 * extras: d36uatko69830t.cloudfront.net
 * updates: d36uatko69830t.cloudfront.net
Available Packages
Name        : galera-4
Arch        : x86_64
Version     : 26.4.3
Release     : 1.rhel7.el7.centos
Size        : 9.3 M
Repo        : mariadb-main/7/x86_64
Summary     : Galera: a synchronous multi-master wsrep provider (replication engine)
URL         : http://www.codership.com/
License     : GPL-2.0
Description : Galera is a fast synchronous multimaster wsrep provider (replication engine)
            : for transactional databases and similar applications. For more information
            : about wsrep API see http://launchpad.net/wsrep. For a description of Galera
            : replication engine see http://www.codership.com.
            : 
            : Copyright 2007-2014 Codership Oy. All rights reserved. Use is subject to license terms under GPLv2
            : license.
            : 
            : This software comes with ABSOLUTELY NO WARRANTY. This is free software,
            : and you are welcome to modify and redistribute it under the GPLv2 license.

[root@ip-172-31-33-11 ~]#

安装MariaDB数据库服务及依赖软件包

[root@server1 ~]# yum -y install MariaDB-server MariaDB-client
[root@server2 ~]# yum -y install MariaDB-server MariaDB-client
[root@server3 ~]# yum -y install MariaDB-server MariaDB-client

查看依赖

启动服务

[root@server1 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/mysql.service to /usr/lib/systemd/system/mariadb.service.
Created symlink from /etc/systemd/system/mysqld.service to /usr/lib/systemd/system/mariadb.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@server1 ~]# systemctl start mariadb
[root@server1 ~]#

[root@server2 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/mysql.service to /usr/lib/systemd/system/mariadb.service.
Created symlink from /etc/systemd/system/mysqld.service to /usr/lib/systemd/system/mariadb.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@server2 ~]# systemctl start mariadb
[root@server2 ~]#

[root@server3 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/mysql.service to /usr/lib/systemd/system/mariadb.service.
Created symlink from /etc/systemd/system/mysqld.service to /usr/lib/systemd/system/mariadb.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@server3 ~]# systemctl start mariadb
[root@server3 ~]#

安装同步软件rsync

[root@server1 ~]# yum -y install rsync

查看rsync版本信息

[root@server1 ~]# yum info rsync
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: d36uatko69830t.cloudfront.net
 * extras: d36uatko69830t.cloudfront.net
 * updates: d36uatko69830t.cloudfront.net
Installed Packages
Name        : rsync
Arch        : x86_64
Version     : 3.1.2
Release     : 6.el7_6.1
Size        : 815 k
Repo        : installed
From repo   : base
Summary     : A program for synchronizing files over a network
URL         : http://rsync.samba.org/
License     : GPLv3+
Description : Rsync uses a reliable algorithm to bring remote and host files into
            : sync very quickly. Rsync is fast because it just sends the differences
            : in the files over the network instead of sending the complete
            : files. Rsync is often used as a very powerful mirroring process or
            : just as a more capable replacement for the rcp command. A technical
            : report which describes the rsync algorithm is included in this
            : package.

[root@server1 ~]#

编辑集群配置文件

server1

[root@server1 ~]# vi /etc/my.cnf.d/galera.cnf
[mysqld]
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
bind-address=0.0.0.0

# Galera Provider Configuration
wsrep_on=ON
wsrep_provider=/usr/lib64/galera-4/libgalera_smm.so

# Galera Cluster Configuration
wsrep_cluster_name="test_cluster"
wsrep_cluster_address="gcomm://172.31.46.167,172.31.33.251,172.31.46.43"

# Galera Synchronization Configuration
wsrep_sst_method=rsync

# Galera Node Configuration
wsrep_node_address="172.31.46.167"
wsrep_node_name="server01"

server2

[root@server2 ~]# vi /etc/my.cnf.d/galera.cnf
[mysqld]
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
bind-address=0.0.0.0

# Galera Provider Configuration
wsrep_on=ON
wsrep_provider=/usr/lib64/galera-4/libgalera_smm.so

# Galera Cluster Configuration
wsrep_cluster_name="test_cluster"
wsrep_cluster_address="gcomm://172.31.46.167,172.31.33.251,172.31.46.43"

# Galera Synchronization Configuration
wsrep_sst_method=rsync

# Galera Node Configuration
wsrep_node_address="172.31.33.251"
wsrep_node_name="server02"

server3

[root@server3 ~]# vi /etc/my.cnf.d/galera.cnf
[mysqld]
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
bind-address=0.0.0.0

# Galera Provider Configuration
wsrep_on=ON
wsrep_provider=/usr/lib64/galera-4/libgalera_smm.so

# Galera Cluster Configuration
wsrep_cluster_name="test_cluster"
wsrep_cluster_address="gcomm://172.31.46.167,172.31.33.251,172.31.46.43"

# Galera Synchronization Configuration
wsrep_sst_method=rsync

# Galera Node Configuration
wsrep_node_address="172.31.46.43"
wsrep_node_name="server03"
停止所有节点的MariaDB服务
[root@server3 ~]# systemctl stop mariadb 
[root@server2 ~]# systemctl stop mariadb 
[root@server1 ~]# systemctl stop mariadb

启动的一个节点

[root@server1 ~]# galera_new_cluster 
[root@server1 ~]# mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size'"
Enter password: 
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 1     |
+--------------------+-------+
[root@server1 ~]#

启动第二个节点

[root@server2 ~]# systemctl start mariadb
[root@server2 ~]# mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size'"
Enter password: 
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 2     |
+--------------------+-------+
[root@server2 ~]#

启动第三个节点

[root@server3 ~]# systemctl start mariadb
[root@server3 ~]# mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size'"
Enter password: 
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 3     |
+--------------------+-------+
[root@server3 ~]# 
10 月 252014
 

复制状态检测插件

[root@monitor libexec]# ./check_mysql_slavestatus.sh -H 192.168.153.113 -P 3306 -u cacti -p qweszxc -w 2 -c 5
 OK: Slave SQL running: Yes Slave IO running: Yes / master: 192.168.153.112 / slave is 0 seconds behind master |
 delay=0s
[root@monitor libexec]#

取值来源

Slave_SQL_Running
Slave_IO_Running
Master_Host
Seconds_Behind_Master

新增插件
http://www.claudiokuenzler.com/nagios-plugins/check_mysql_slavestatus.sh

定义命令

[root@monitor libexec]# vi ../etc/objects/commands.cfg
define command{
 command_name check_mysql_slavestatus
 command_line $USER1$/check_mysql_slavestatus.sh -H $HOSTADDRESS$ -P $ARG1$ -u $ARG2$ -p $ARG3$
 }

在配置文件中引用命令

define service{
 use generic-service
 host_name dbmaster01
 service_description MySQL Slave Status
 check_command check_nrpe!check_mysql_slavestatus!3306!cacti!qweszxc!1!5
 }

优化,重定义(适用多台相同账户配置主机)

# 'check_mysql_slavestatus'
define command{
 command_name check_mysql_slavestatus
 command_line $USER1$/check_mysql_slavestatus.sh -H $HOSTADDRESS$ -P 3306 -u cacti -p qweszxc -w
 $ARG1$ -c $ARG2$
 }
define service{
 use generic-service
 host_name dbmaster01
 service_description MySQL Slave Status
 check_command check_mysql_slavestatus!1!5
 }

nagios-check-mysql-slavestatus

Seconds_Behind_Master 解析

http://blog.itpub.net/23721637/viewspace-1050178/

MySQL slave状态之Seconds_Behind_Master

http://blog.csdn.net/zbszhangbosen/article/details/8494921

8 月 222014
 

服务器主机IP和虚拟浮动IP配置

RealServer A 192.168.75.133
RealServer B 192.168.75.134
VIP A 192.168.75.110
VIP B 192.168.75.111

安装KeepAlived软件包

[root@mysql-b ~]# yum install keepalived

================================================================================
 Package Arch Version Repository Size
================================================================================
 Installing:
 keepalived i686 1.2.7-3.el6 base 170 k
 Installing for dependencies:
 libnl i686 1.1.4-2.el6 base 124 k
 lm_sensors-libs i686 3.1.1-17.el6 base 37 k
 net-snmp-libs i686 1:5.5-49.el6_5.1 updates 1.5 M
 perl i686 4:5.10.1-136.el6 base 9.7 M
 perl-Module-Pluggable i686 1:3.90-136.el6 base 40 k
 perl-Pod-Escapes i686 1:1.04-136.el6 base 32 k
 perl-Pod-Simple i686 1:3.13-136.el6 base 212 k
 perl-libs i686 4:5.10.1-136.el6 base 593 k
 perl-version i686 3:0.77-136.el6 base 51 k
Transaction Summary
================================================================================
Install 10 Package(s)

查看RPM包的文件路径

[root@mysql-a ~]# rpm -lq keepalived
 /etc/keepalived
 /etc/keepalived/keepalived.conf
 /etc/rc.d/init.d/keepalived
 /etc/sysconfig/keepalived
 /usr/bin/genhash
 /usr/sbin/keepalived
 /usr/share/doc/keepalived-1.2.7
 /usr/share/doc/keepalived-1.2.7/AUTHOR
 /usr/share/doc/keepalived-1.2.7/CONTRIBUTORS
 /usr/share/doc/keepalived-1.2.7/COPYING
 /usr/share/doc/keepalived-1.2.7/ChangeLog
 /usr/share/doc/keepalived-1.2.7/README
 /usr/share/doc/keepalived-1.2.7/TODO
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.HTTP_GET.port
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.IPv6
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.SMTP_CHECK
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.SSL_GET
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.SYNOPSIS
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.fwmark
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.inhibit
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.misc_check
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.misc_check_arg
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.quorum
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.sample
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.status_code
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.track_interface
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.virtual_server_group
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.virtualhost
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.vrrp
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.vrrp.localcheck
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.vrrp.lvs_syncd
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.vrrp.routes
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.vrrp.scripts
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.vrrp.static_ipaddress
 /usr/share/doc/keepalived-1.2.7/keepalived.conf.vrrp.sync
 /usr/share/man/man1/genhash.1.gz
 /usr/share/man/man5/keepalived.conf.5.gz
 /usr/share/man/man8/keepalived.8.gz
 /usr/share/snmp/mibs/KEEPALIVED-MIB.txt
 [root@mysql-a ~]#

KeepAlived双主RealServer A配置
! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}

vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
#192.168.200.16
#192.168.200.17
#192.168.200.18
192.168.75.110
}
}

vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
#192.168.200.16
#192.168.200.17
#192.168.200.18
192.168.75.111
}
}

KeepAlived双主RealServer B配置
! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}

vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
#192.168.200.16
#192.168.200.17
#192.168.200.18
192.168.75.110
}
}

vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
#192.168.200.16
#192.168.200.17
#192.168.200.18
192.168.75.111
}
}
在RealServer B上启动KeepAlived服务
[root@mysql-b keepalived]# chkconfig –list keepalived
keepalived 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@mysql-b keepalived]# chkconfig –level 35 keepalived on
[root@mysql-b keepalived]# service keepalived start
Starting keepalived: [ OK ]
[root@mysql-b keepalived]#

查看两个Virtual IP信息
[root@mysql-b keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
link/ether 00:0c:29:7c:b3:f2 brd ff:ff:ff:ff:ff:ff
inet 192.168.75.134/24 brd 192.168.75.255 scope global eth0
inet 192.168.75.110/32 scope global eth0
inet 192.168.75.111/32 scope global eth0
inet6 fe80::20c:29ff:fe7c:b3f2/64 scope link
valid_lft forever preferred_lft forever
[root@mysql-b keepalived]#
在RealServer A上启动KeepAlived服务后查看Virtual IP信息
[root@mysql-a keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
link/ether 00:0c:29:13:94:f0 brd ff:ff:ff:ff:ff:ff
inet 192.168.75.133/24 brd 192.168.75.255 scope global eth0
inet 192.168.75.110/32 scope global eth0
inet6 fe80::20c:29ff:fe13:94f0/64 scope link
valid_lft forever preferred_lft forever
[root@mysql-a keepalived]#

在RealServer B上再次查看Virtual IP信息
[root@mysql-b keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:7c:b3:f2 brd ff:ff:ff:ff:ff:ff
inet 192.168.75.134/24 brd 192.168.75.255 scope global eth0
inet 192.168.75.111/32 scope global eth0
inet6 fe80::20c:29ff:fe7c:b3f2/64 scope link
valid_lft forever preferred_lft forever
[root@mysql-b keepalived]#
————————–MySQL

[root@mysql-a ~]# yum install mysql-server mysql

================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
mysql i686 5.1.73-3.el6_5 updates 903 k
mysql-server i686 5.1.73-3.el6_5 updates 8.8 M
Installing for dependencies:
perl-DBD-MySQL i686 4.013-3.el6 base 134 k
perl-DBI i686 1.609-4.el6 base 705 k

Transaction Summary
================================================================================
Install 4 Package(s)
[root@mysql-a ~]# /usr/bin/mysqladmin -u root password ‘mysqlpass’

 

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

server-id=1
log-bin=mysql-bin
binlog-ignore-db=mysql
binlog_format=mixed
expire_logs_days=7
sync_binlog=1

auto_increment_offset=1
auto_increment_increment=2
log_slave_updates

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

—————————
server-id=2
log-bin=mysql-bin
binlog-ignore-db=mysql
binlog_format=mixed
expire_logs_days=7
sync_binlog=1

auto_increment_offset=2
auto_increment_increment=2
log_slave_updates

———————————–
添加复制用户并授权
A

mysql> grant replication slave on *.* to repl@’192.168.75.134′ identified by ‘replpasswd’;
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

B

mysql> grant replication slave on *.* to repl@’192.168.75.133′ identified by ‘replpasswd’;
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

—————-
锁定表为只读状态,并查看二进制日志的当前位置信息
A

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
+——————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000001 | 348 | | mysql |
+——————+———-+————–+——————+
1 row in set (0.00 sec)
B

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
+——————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000003 | 491 | | mysql |
+——————+———-+————–+——————+
1 row in set (0.00 sec)
——————-
各自指定对方为master主机的连接配置信息和位置信息
A

mysql> change master to master_host=’192.168.75.134′,master_user=’repl’,master_password=’replpasswd’,master_log_file=’mysql-bin.000003′,master_log_pos=491;
Query OK, 0 rows affected (0.03 sec)

mysql>
B

mysql> change master to master_host=’192.168.75.133′,master_user=’repl’,master_password=’replpasswd’,master_log_file=’mysql-bin.000001′,master_log_pos=348;
Query OK, 0 rows affected (0.03 sec)

mysql>
————–
解除表锁定,分别启动Slave并查看复制运行状态
A

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.75.134
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 491
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 491
Relay_Log_Space: 407
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)

ERROR:
No query specified

mysql>

 

B
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.75.133
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 348
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 348
Relay_Log_Space: 407
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)

ERROR:
No query specified

mysql>

 

[root@mysql-a ~]# netstat -nt |grep 3306
tcp 0 0 192.168.75.133:3306 192.168.75.134:36244 ESTABLISHED
tcp 0 0 192.168.75.133:60121 192.168.75.134:3306 ESTABLISHED
[root@mysql-a ~]#
[root@mysql-b keepalived]# netstat -nt |grep 3306
tcp 0 0 192.168.75.134:3306 192.168.75.133:60121 ESTABLISHED
tcp 0 0 192.168.75.134:36244 192.168.75.133:3306 ESTABLISHED
[root@mysql-b keepalived]#
———————–
测试:
A
mysql> create database aaa;
Query OK, 1 row affected (0.00 sec)

mysql>
B

mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| aaa |
| mysql |
| test |
+——————–+
4 rows in set (0.00 sec)

mysql> create database bbb;
Query OK, 1 row affected (0.00 sec)

mysql>

A

mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| aaa |
| bbb |
| mysql |
| test |
+——————–+
5 rows in set (0.01 sec)

mysql>

 

——————–

VIP测试

A

mysql> create database ccc;
Query OK, 1 row affected (0.00 sec)

mysql> grant all on ccc.* to cccuser@’%’;
Query OK, 0 rows affected (0.00 sec)

mysql> set password for cccuser@’%’=password(‘cccpwd’);
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql>
VIP 192.168.75.110

[root@localhost ~]# mysql -u cccuser -pcccpwd -h 192.168.75.110
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.1.73-log Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> use ccc;
Database changed

mysql> create table list (id int(4) not null primary key auto_increment,
-> name char(20) not null,
-> address char(40) not null);
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+—————+
| Tables_in_ccc |
+—————+
| list |
+—————+
1 row in set (0.00 sec)

mysql> quit
Bye
[root@localhost ~]#

VIP 192.168.75.111

[root@localhost ~]# mysql -u cccuser -pcccpwd -h 192.168.75.111
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.1.73-log Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> use ccc;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+—————+
| Tables_in_ccc |
+—————+
| list |
+—————+
1 row in set (0.00 sec)

mysql> quit
Bye
[root@localhost ~]#
————————-
关闭RealServer B 测试KeepAlived VIP可用性和数据库连接
[root@localhost ~]# mysql -u cccuser -pcccpwd -h 192.168.75.110
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.1.73-log Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> quit
Bye
[root@localhost ~]# mysql -u cccuser -pcccpwd -h 192.168.75.111
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.1.73-log Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> quit
Bye
[root@localhost ~]#
在RealServer B关闭情况下的ping测试
[root@localhost ~]# ping -c 4 192.168.75.110
PING 192.168.75.110 (192.168.75.110) 56(84) bytes of data.
64 bytes from 192.168.75.110: icmp_seq=1 ttl=64 time=0.535 ms
64 bytes from 192.168.75.110: icmp_seq=2 ttl=64 time=0.312 ms
64 bytes from 192.168.75.110: icmp_seq=3 ttl=64 time=0.351 ms
64 bytes from 192.168.75.110: icmp_seq=4 ttl=64 time=0.356 ms

— 192.168.75.110 ping statistics —
4 packets transmitted, 4 received, 0% packet loss, time 3001ms
rtt min/avg/max/mdev = 0.312/0.388/0.535/0.088 ms
[root@localhost ~]# ping -c 4 192.168.75.111
PING 192.168.75.111 (192.168.75.111) 56(84) bytes of data.
64 bytes from 192.168.75.111: icmp_seq=1 ttl=64 time=0.572 ms
64 bytes from 192.168.75.111: icmp_seq=2 ttl=64 time=0.301 ms
64 bytes from 192.168.75.111: icmp_seq=3 ttl=64 time=0.452 ms
64 bytes from 192.168.75.111: icmp_seq=4 ttl=64 time=0.293 ms

— 192.168.75.111 ping statistics —
4 packets transmitted, 4 received, 0% packet loss, time 3006ms
rtt min/avg/max/mdev = 0.293/0.404/0.572/0.117 ms
[root@localhost ~]#