zoukankan      html  css  js  c++  java
  • percona-mysql5.7.24使用xtrabackup工具配置主从同步

    主从配置详细过程:
    
    环境准备:
    配置好服务器,主从服务器都安装并启动mysql数据库
    
    
    # 添加读写账号和只读账号,应用配置中,写主库用读写账号,统计从库数据yoga只读账号
    grant select,insert,update,delete on f_crack_check.* to f_crack_check_user@"%" identified by "pass";
    grant select on f_crack_check.* to f_crack_check_read@"%" identified by "pass";
    flush privileges;
    
    注意:当配置好主从后,添加账号,或者任何写库的操作都需要在主库中操作,否则可能引起主从不一致
    
    # 为了方便后面切换服务器方便,内网使用域名的方式进行数据库链接
    
    主库连接地址:172.30.0.51 f_crack_check.master.prod.mysql
    从库连接地址:172.30.0.52 f_crack_check.slave01.prod.mysql
    
    主从数据库服务器均安装xtrabackup工具
    wget http://repo.percona.com/tools/yum/release/7/RPMS/x86_64/percona-xtrabackup-24-2.4.12-1.el7.x86_64.rpm
    
    yum localinstall percona-xtrabackup-24-2.4.12-1.el7.x86_64.rpm -y
    
    
    1.主库中备份,将mysql数据库整个备份到/opt/目录下
    # innobackupex --defaults-file="/etc/my.cnf" --user=root -p'pass' --socket=/tmp/mysql.sock /opt
    xtrabackup: recognized server arguments: --datadir=/data/mysql_data --tmpdir=/tmp --open_files_limit=10000 --log_bin=mysql-bin --server-id=51 --innodb_data_home_dir=/data/mysql_data --innodb_data_file_path=ibdata1:1G;ibdata2:1G:autoextend --innodb_log_group_home_dir=/data/mysql_data --innodb_undo_directory=/data/mysql_data/undolog/ --innodb_undo_tablespaces=3 --innodb_buffer_pool_size=3G --innodb_log_file_size=256M --innodb_log_buffer_size=64M --innodb_log_files_in_group=3 --innodb_flush_log_at_trx_commit=1 --innodb_file_per_table=1 --innodb_read_io_threads=8 --innodb_write_io_threads=8 --innodb_max_dirty_pages_pct=75 --innodb_flush_method=O_DIRECT --innodb_autoextend_increment=32 
    xtrabackup: recognized client arguments: --datadir=/data/mysql_data --tmpdir=/tmp --open_files_limit=10000 --log_bin=mysql-bin --server-id=51 --innodb_data_home_dir=/data/mysql_data --innodb_data_file_path=ibdata1:1G;ibdata2:1G:autoextend --innodb_log_group_home_dir=/data/mysql_data --innodb_undo_directory=/data/mysql_data/undolog/ --innodb_undo_tablespaces=3 --innodb_buffer_pool_size=3G --innodb_log_file_size=256M --innodb_log_buffer_size=64M --innodb_log_files_in_group=3 --innodb_flush_log_at_trx_commit=1 --innodb_file_per_table=1 --innodb_read_io_threads=8 --innodb_write_io_threads=8 --innodb_max_dirty_pages_pct=75 --innodb_flush_method=O_DIRECT --innodb_autoextend_increment=32 
    190809 19:38:37 innobackupex: Starting the backup operation
    
    IMPORTANT: Please check that the backup run completes successfully.
               At the end of a successful backup run innobackupex
               prints "completed OK!".
    
    190809 19:38:37  version_check Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup;port=3306;mysql_socket=/tmp/mysql.sock' as 'root'  (using password: YES).
    Failed to connect to MySQL server as DBD::mysql module is not installed at - line 1327.
    190809 19:38:37 Connecting to MySQL server host: localhost, user: root, password: set, port: 3306, socket: /tmp/mysql.sock
    Using server version 5.7.24-27-log
    innobackupex version 2.4.12 based on MySQL server 5.7.19 Linux (x86_64) (revision id: 170eb8c)
    xtrabackup: uses posix_fadvise().
    xtrabackup: cd to /data/mysql_data
    xtrabackup: open files limit requested 10000, set to 655350
    xtrabackup: using the following InnoDB configuration:
    xtrabackup:   innodb_data_home_dir = /data/mysql_data
    xtrabackup:   innodb_data_file_path = ibdata1:1G;ibdata2:1G:autoextend
    xtrabackup:   innodb_log_group_home_dir = /data/mysql_data
    xtrabackup:   innodb_log_files_in_group = 3
    xtrabackup:   innodb_log_file_size = 268435456
    xtrabackup: using O_DIRECT
    InnoDB: Number of pools: 1
    190809 19:38:37 >> log scanned up to (3033736244)
    InnoDB: Opened 3 undo tablespaces
    InnoDB: 0 undo tablespaces made active
    xtrabackup: Generating a list of tablespaces
    InnoDB: Allocated tablespace ID 22 for mysql/server_cost, old maximum was 3
    190809 19:38:37 [01] Copying /data/mysql_data/ibdata1 to /opt/2019-08-09_19-38-37/ibdata1
    190809 19:38:38 >> log scanned up to (3033736244)
    
    ...
    190809 19:39:21 Finished backing up non-InnoDB tables and files
    190809 19:39:21 Executing LOCK BINLOG FOR BACKUP...
    190809 19:39:21 [00] Writing /opt/2019-08-09_19-38-37/xtrabackup_binlog_info
    190809 19:39:21 [00]        ...done
    190809 19:39:21 Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS...
    xtrabackup: The latest check point (for incremental): '3033736235'
    xtrabackup: Stopping log copying thread.
    .190809 19:39:21 >> log scanned up to (3033736276)
    
    190809 19:39:21 Executing UNLOCK BINLOG
    190809 19:39:21 Executing UNLOCK TABLES
    190809 19:39:21 All tables unlocked
    190809 19:39:21 [00] Copying ib_buffer_pool to /opt/2019-08-09_19-38-37/ib_buffer_pool
    190809 19:39:21 [00]        ...done
    190809 19:39:21 Backup created in directory '/opt/2019-08-09_19-38-37/'
    MySQL binlog position: filename 'mysql-bin.000004', position '660'
    190809 19:39:21 [00] Writing /opt/2019-08-09_19-38-37/backup-my.cnf
    190809 19:39:21 [00]        ...done
    190809 19:39:21 [00] Writing /opt/2019-08-09_19-38-37/xtrabackup_info
    190809 19:39:21 [00]        ...done
    xtrabackup: Transaction log of lsn (3033736235) to (3033736276) was copied.
    190809 19:39:21 completed OK!
    
    2.预处理,进行事物检查(最好直接在主库中备份完成后检查)
    # innobackupex --defaults-file="/etc/my.cnf" --user=root -p'pass' --socket=/tmp/mysql.sock --apply-log --use-memory=2G /opt/2019-08-09_19-38-37/
    xtrabackup: recognized server arguments: --innodb_checksum_algorithm=crc32 --innodb_log_checksum_algorithm=strict_crc32 --innodb_data_file_path=ibdata1:1G;ibdata2:1G:autoextend --innodb_log_files_in_group=3 --innodb_log_file_size=268435456 --innodb_fast_checksum=0 --innodb_page_size=16384 --innodb_log_block_size=512 --innodb_undo_directory=/data/mysql_data/undolog/ --innodb_undo_tablespaces=3 --server-id=51 --redo-log-version=1 
    xtrabackup: recognized client arguments: --innodb_checksum_algorithm=crc32 --innodb_log_checksum_algorithm=strict_crc32 --innodb_data_file_path=ibdata1:1G;ibdata2:1G:autoextend --innodb_log_files_in_group=3 --innodb_log_file_size=268435456 --innodb_fast_checksum=0 --innodb_page_size=16384 --innodb_log_block_size=512 --innodb_undo_directory=/data/mysql_data/undolog/ --innodb_undo_tablespaces=3 --server-id=51 --redo-log-version=1 
    190809 19:41:38 innobackupex: Starting the apply-log operation
    
    IMPORTANT: Please check that the apply-log run completes successfully.
               At the end of a successful apply-log run innobackupex
               prints "completed OK!".
    
    innobackupex version 2.4.12 based on MySQL server 5.7.19 Linux (x86_64) (revision id: 170eb8c)
    xtrabackup: cd to /opt/2019-08-09_19-38-37/
    xtrabackup: This target seems to be not prepared yet.
    InnoDB: Number of pools: 1
    xtrabackup: xtrabackup_logfile detected: size=8388608, start_lsn=(3033736235)
    xtrabackup: using the following InnoDB configuration for recovery:
    xtrabackup:   innodb_data_home_dir = .
    xtrabackup:   innodb_data_file_path = ibdata1:1G;ibdata2:1G:autoextend
    xtrabackup:   innodb_log_group_home_dir = .
    xtrabackup:   innodb_log_files_in_group = 1
    xtrabackup:   innodb_log_file_size = 8388608
    xtrabackup: using the following InnoDB configuration for recovery:
    xtrabackup:   innodb_data_home_dir = .
    xtrabackup:   innodb_data_file_path = ibdata1:1G;ibdata2:1G:autoextend
    xtrabackup:   innodb_log_group_home_dir = .
    xtrabackup:   innodb_log_files_in_group = 1
    xtrabackup:   innodb_log_file_size = 8388608
    xtrabackup: Starting InnoDB instance for recovery.
    xtrabackup: Using 2147483648 bytes for buffer pool (set by --use-memory parameter)
    InnoDB: PUNCH HOLE support available
    
    xtrabackup: starting shutdown with innodb_fast_shutdown = 1
    InnoDB: FTS optimize thread exiting.
    InnoDB: Starting shutdown...
    InnoDB: Shutdown completed; log sequence number 3033736295
    InnoDB: Number of pools: 1
    xtrabackup: using the following InnoDB configuration for recovery:
    xtrabackup:   innodb_data_home_dir = .
    xtrabackup:   innodb_data_file_path = ibdata1:1G;ibdata2:1G:autoextend
    xtrabackup:   innodb_log_group_home_dir = .
    xtrabackup:   innodb_log_files_in_group = 3
    xtrabackup:   innodb_log_file_size = 268435456
    InnoDB: PUNCH HOLE support available
    InnoDB: Mutexes and rw_locks use GCC atomic builtins
    InnoDB: Uses event mutexes
    InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
    ...
    InnoDB: FTS optimize thread exiting.
    InnoDB: Starting shutdown...
    InnoDB: Shutdown completed; log sequence number 3033736744
    190809 19:41:50 completed OK!
    
    
    3.将备份出来的数据库scp拷贝到从库
    # scp -P 2019 -r 2019-08-09_19-38-37 172.30.0.52:/opt
    
    4.关闭从库,清理从库数据,恢复数据到从库
    
    /etc/init.d/mysqld stop
    
    删除从库的数据和日志信息
    rm -rf /data/mysql_data/*
    
    在从库上执行(将数据恢复到数据库中)
    # innobackupex --defaults-file="/etc/my.cnf" --user=root --socket=/tmp/mysql.sock --copy-back /opt/2019-08-09_19-38-37/
    xtrabackup: recognized server arguments: --datadir=/data/mysql_data --tmpdir=/tmp --open_files_limit=10000 --log_bin=mysql-bin --server-id=52 --innodb_data_home_dir=/data/mysql_data --innodb_data_file_path=ibdata1:1G;ibdata2:1G:autoextend --innodb_log_group_home_dir=/data/mysql_data --innodb_undo_directory=/data/mysql_data/undolog/ --innodb_undo_tablespaces=3 --innodb_buffer_pool_size=3G --innodb_log_file_size=256M --innodb_log_buffer_size=64M --innodb_log_files_in_group=3 --innodb_flush_log_at_trx_commit=1 --innodb_file_per_table=1 --innodb_read_io_threads=8 --innodb_write_io_threads=8 --innodb_max_dirty_pages_pct=75 --innodb_flush_method=O_DIRECT --innodb_autoextend_increment=32 
    xtrabackup: recognized client arguments: --datadir=/data/mysql_data --tmpdir=/tmp --open_files_limit=10000 --log_bin=mysql-bin --server-id=52 --innodb_data_home_dir=/data/mysql_data --innodb_data_file_path=ibdata1:1G;ibdata2:1G:autoextend --innodb_log_group_home_dir=/data/mysql_data --innodb_undo_directory=/data/mysql_data/undolog/ --innodb_undo_tablespaces=3 --innodb_buffer_pool_size=3G --innodb_log_file_size=256M --innodb_log_buffer_size=64M --innodb_log_files_in_group=3 --innodb_flush_log_at_trx_commit=1 --innodb_file_per_table=1 --innodb_read_io_threads=8 --innodb_write_io_threads=8 --innodb_max_dirty_pages_pct=75 --innodb_flush_method=O_DIRECT --innodb_autoextend_increment=32 
    190809 19:52:10 innobackupex: Starting the copy-back operation
    
    IMPORTANT: Please check that the copy-back run completes successfully.
               At the end of a successful copy-back run innobackupex
               prints "completed OK!".
    
    innobackupex version 2.4.12 based on MySQL server 5.7.19 Linux (x86_64) (revision id: 170eb8c)
    190809 19:52:10 [01] Copying undo001 to /data/mysql_data/undolog/undo001
    190809 19:52:10 [01]        ...done
    190809 19:52:10 [01] Copying undo002 to /data/mysql_data/undolog/undo002
    190809 19:52:10 [01]        ...done
    190809 19:52:10 [01] Copying undo003 to /data/mysql_data/undolog/undo003
    190809 19:52:10 [01]        ...done
    190809 19:52:10 [01] Copying ib_logfile0 to /data/mysql_data/ib_logfile0
    190809 19:52:10 [01]        ...done
    190809 19:52:11 [01] Copying ib_logfile1 to /data/mysql_data/ib_logfile1
    190809 19:52:11 [01]        ...done
    190809 19:52:12 [01] Copying ib_logfile2 to /data/mysql_data/ib_logfile2
    ...
    190809 19:52:27 [01] Copying ./xtrabackup_binlog_pos_innodb to /data/mysql_data/xtrabackup_binlog_pos_innodb
    190809 19:52:27 [01]        ...done
    190809 19:52:27 completed OK!
    
    5.修改权限,启动从库
    # chown -R mysql.mysql /data/mysql_data/
    
    查看主库中master位置,方便后面配置主从同步点
    
    # cat /opt/2019-08-09_19-38-37/xtrabackup_binlog_info 
    mysql-bin.000004        660
    
    6.主库中创建建salve同步用户
    
    grant replication slave,reload,super on *.* to rep@172.30.0.52 identified by 'wsdb123';
    FLUSH PRIVILEGES;
    
    7.从库执行同步
    
    mysql> change master to master_host='f_crack_check.prod.mysql.eus',master_user='rep',master_password='wsdb123',master_log_file='mysql-bin.000004',master_log_pos=660;
    mysql> start slave;
    
    mysql> show slave statusG
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: f_crack_check.prod.mysql.eus
                      Master_User: rep
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000004
              Read_Master_Log_Pos: 1119
                   Relay_Log_File: mysql-relay-bin.000002
                    Relay_Log_Pos: 779
            Relay_Master_Log_File: mysql-bin.000004
                 Slave_IO_Running: Yes # 关键项,都为yes表示配置主从同步成功
                Slave_SQL_Running: Yes
                  Replicate_Do_DB: 
              Replicate_Ignore_DB: information_schema,performance_schema,undolog,for_nagios,undolog,information_schema,performance_schema,undolog,for_nagios,undolog
               Replicate_Do_Table: 
           Replicate_Ignore_Table: 
          Replicate_Wild_Do_Table: 
      Replicate_Wild_Ignore_Table: information_schema.%,performance_schema.%,undolog.%,for_nagios.%,undolog.%,information_schema.%,performance_schema.%,undolog.%,for_nagios.%,undolog.%
                       Last_Errno: 0
                       Last_Error: 
                     Skip_Counter: 0
              Exec_Master_Log_Pos: 1119
                  Relay_Log_Space: 986
                  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: 
      Replicate_Ignore_Server_Ids: 
                 Master_Server_Id: 51
                      Master_UUID: b7215ccd-b9b8-11e9-885d-00163e0073ee
                 Master_Info_File: mysql.slave_master_info
                        SQL_Delay: 0
              SQL_Remaining_Delay: NULL
          Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
               Master_Retry_Count: 86400
                      Master_Bind: 
          Last_IO_Error_Timestamp: 
         Last_SQL_Error_Timestamp: 
                   Master_SSL_Crl: 
               Master_SSL_Crlpath: 
               Retrieved_Gtid_Set: 
                Executed_Gtid_Set: b7215ccd-b9b8-11e9-885d-00163e0073ee:1-774
                    Auto_Position: 0
             Replicate_Rewrite_DB: 
                     Channel_Name: 
               Master_TLS_Version: 
    1 row in set (0.00 sec)
    
    
    pass
    
    ### rep from master
    172.30.0.51 f_crack_check.prod.mysql.eus
    
    # 主从服务器配置:
    阿里云服务器,计算型网络增强型
    8核16G内存
    40G系统盘 SSD 400G数据盘
    
    master主节点的my.cnf配置:
    
    [master]# cat /etc/my.cnf
    # Example MySQL config file for medium systems.
    # 8核 16G
    # This is for a system with little memory (16G) where MySQL plays
    [client]
    #password       = your_password
    port                                                        = 3306
    socket                                                      = /tmp/mysql.sock
    
    # The MySQL server
    [mysqld]
    user                                                        = mysql
    port                                                        = 3306
    bind-address                                                = 0.0.0.0
    socket                                                      = /tmp/mysql.sock
    datadir                                                     = /data/mysql_data
    pid-file                                                    = /data/mysql_data/mysql.pid
    basedir                                                     = /usr/local/mysql
    tmpdir                                                      = /tmp
    
    #此开关默认为NULL,即不允许导入导出。
    #secure-file-priv                                           = /opt/upload
    
    #-------------------------------gobal variables------------------------#
    #默认关闭,涉及到timestamp类型的列自动更新的问题
    explicit_defaults_for_timestamp                 = 1
    ###transaction_write_set_extraction              = XXHASH64  #以便在server收集写集合的同时将其记录到二进制日志。并且是行更改后的唯一标识此标识将用于检测冲突。
    ###loose-group_replication_group_name            = 'ce9be252-2b71-11e6-b8f4-00212889f856' #组的名字可以随便起,但不能用主机的GTID
    ###loose-group_replication_start_on_boot         = off  #为了避免每次启动自动引导具有相同名称的第二个组,所以设置为OFF。
    ###loose-group_replication_bootstrap_group       = off #同上
    ###loose-group_replication_local_address         = '192.168.1.88:33071' #写自己主机所在IP
    ###loose-group_replication_group_seeds           ='192.168.1.88:33071,192.168.1.89:33071,192.168.1.90:33071'
    ###loose-group_replication_single_primary_mode   = off  #关闭单主模式的参数
    ###loose-group_replication_enforce_update_everywhere_checks = on #开启多主模式的参数
    
    skip-external-locking
    skip-name-resolve
    skip-ssl
    
    #memory is 16G
    key_buffer_size                                             = 32M
    table_open_cache                                            = 2048
    table_definition_cache                                      = 1024
    sort_buffer_size                                            = 4M
    net_buffer_length                                           = 32K
    read_buffer_size                                            = 4M
    read_rnd_buffer_size                                        = 16M
    
    open_files_limit                                            = 10000
    thread_cache_size                                           = 400
    query_cache_type                                            = 0
    query_cache_size                                            = 32M
    max_write_lock_count                                        = 300
    wait_timeout                                                = 120
    interactive_timeout                                         = 120
    net_read_timeout                                            = 120
    net_write_timeout                                           = 120
    
    max_connections                                             = 800
    max_user_connections                                        = 750
    max_connect_errors                                          = 10000
    max_allowed_packet                                          = 512M
    back_log                                                    = 2048
    log_timestamps                                              = system
    performance_schema                                          = OFF
    character_set_server                                        = utf8mb4
    
    ##当链接数耗尽后,通过设置别用端口,让root可以登录
    extra_max_connections                                       = 2
    extra_port                                                  = 13306
    
    ###让mysql不区分大小写敏感
    lower_case_table_names                                      = 1
    
    #explicit_defaults_for_timestamp                            = 1
    
    #----------------Myisam--------------------------------#
    myisam_recover_options                                      = DEFAULT
    bulk_insert_buffer_size                                     = 64M
    myisam_sort_buffer_size                                     = 128M
    myisam_max_sort_file_size                                   = 512M
    myisam_repair_threads                                       = 1
    
    #if the query is exec time great than 2 seconds, the query will log to slow log if slowlog is enabled.
    long_query_time                                             = 1
    slow_query_log                                              = On
    slow-query-log-file                                         = /data/mysql_data/slow.log
    show_compatibility_56                                       = on
    
    # Don't listen on a TCP/IP port at all. This can be a security enhancement,
    # if all processes that need to connect to mysqld run on the same host.
    # All interaction with mysqld must be made via Unix sockets or named pipes.
    # Note that using this option without enabling named pipes on Windows
    # (via the "enable-named-pipe" option) will render mysqld useless!
    # 
    #skip-networking
    
    #----------------------------MySQL Log----------------#
    # Replication Master Server (default)
    # binary logging is required for replication
    log-bin                                                     = mysql-bin
    expire_logs_days                                            = 20
    log_error                                                   = error.log
    log_error_verbosity                                         = 1
    log_warnings                                                = 1
    
    # binary logging format - mixed recommended
    binlog_format                                               = row
    relay-log                                                   = mysql-relay-bin
    relay-log-index                                             = relay.index
    # required unique id between 1 and 2^32 - 1
    server-id                                                   = 51  # 关键配置,主从配置server-id不能重复
    #sql-mode                                                    = "NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
    #sql-mode                                                    = "NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
    sync_binlog                                                 = 1
    log_slave_updates                                           = 1
    #binlog_checksum                                            = NONE
    
    #------------------------------replicate--------------#
    #排除不需要同步的库表
    #replicate-ignore-db                                        = mysql
    #replicate-ignore-db                                        = sys
    replicate-ignore-db                                         = information_schema
    replicate-ignore-db                                         = performance_schema
    replicate-ignore-db                                         = undolog
    replicate-ignore-db                                         = for_nagios
    replicate-ignore-db                                         = undolog
    
    #replicate_wild_ignore_table                                = mysql.%
    #replicate_wild_ignore_table                                = sys.%
    replicate_wild_ignore_table                                 = information_schema.%
    replicate_wild_ignore_table                                 = performance_schema.%
    replicate_wild_ignore_table                                 = undolog.%
    replicate_wild_ignore_table                                 = for_nagios.%
    replicate_wild_ignore_table                                 = undolog.%
    
    #主主复制需要开启
    #auto_increment_offset= 2
    #auto_increment_increment= 2
    
    #GTID模式复制,需要开启如下
    #gtid_mode                                                  = ON
    #enforce_gtid_consistency                                   = ON
    
    #并发复制
    slave-parallel-type                                         = LOGICAL_CLOCK
    slave-parallel-workers                                      = 2
    master_info_repository                                      = TABLE
    relay_log_info_repository                                   = TABLE
    relay_log_recovery                                          = ON
    
    #跳过slave进程启动参数
    skip-slave-start
    
    #如果实例为从库,则需要设置为on
    #read_only                                                   = on
    
    #skip-grant-tables
    
    #--------------------------------------------------------innoDB------------#
    innodb_rollback_on_timeout
    # Uncomment the following if you are using InnoDB tables
    innodb_data_home_dir                                        = /data/mysql_data
    innodb_data_file_path                                       = ibdata1:1G;ibdata2:1G:autoextend
    innodb_log_group_home_dir                                   = /data/mysql_data
    innodb_undo_directory                                       = /data/mysql_data/undolog/
    innodb_undo_logs                                            = 128
    innodb_undo_tablespaces                                     = 3
    
    # You can set .._buffer_pool_size up to 50 - 80 %
    #innodb_use_sys_malloc = 0 
    #innodb_page_size = 8192
    innodb_buffer_pool_size                                     = 3G
    innodb_buffer_pool_instances                                = 1
    #innodb_additional_mem_pool_size = 8M
    
    # Set .._log_file_size to 25 % of buffer pool size
    innodb_log_file_size                                        = 256M
    innodb_log_buffer_size                                      = 64M
    innodb_log_files_in_group                                   = 3
    #每次事务提交时MySQL都会把log buffer的数据写入log file,并且flush(刷到磁盘)中去,该模式为系统默认
    innodb_flush_log_at_trx_commit                              = 1
    innodb_lock_wait_timeout                                    = 120
    #启用独立表空间
    innodb_file_per_table                                       = 1
    
    #CPU是1颗8核的,那么可以设置
    innodb_read_io_threads                                      = 8
    innodb_write_io_threads                                     = 8
    
    #默认是0,则表示没有并发线程数限制,所有请求都会直接请求线程执行,当并发用户线程数量小于64,建议设置innodb_thread_concurrency=0,在大多数情况下,最佳的值是小于并接近虚拟CPU的个数
    innodb_thread_concurrency                                   = 12
    innodb_max_dirty_pages_pct                                  = 75
    innodb_flush_method                                         = O_DIRECT
    
    innodb_purge_threads                                        = 10
    innodb_large_prefix                                         = 1
    
    #参数待测试
    #innodb_io_capacity                                         = 20000
    #innodb_io_capacity_max                                     = 40000
    
    #根据CPU核心数来设定
    thread_pool_size                                            = 8
    #thread_handling = pool-of-threads
    thread_pool_oversubscribe                                   = 24
    
    #thread_handling                             = pool-of-threads
    thread_pool_stall_limit                                     = 100
    thread_pool_max_threads                                     = 30
    
    #解释: 在启动时把热数据加载到内存。
    innodb_buffer_pool_load_at_startup                          = 1
    #解释: 在关闭时把热数据dump到本地磁盘
    innodb_buffer_pool_dump_at_shutdown                         = 1
    
    ##默认是8M, 如果一次insert数据量比较多的话, 可以适当增加
    innodb_autoextend_increment                                 = 32
    
    [mysqldump]
    quick
    max_allowed_packet                                          = 512M
    
    [mysql]
    no-auto-rehash
    # Remove the next comment character if you are not familiar with SQL
    #safe-updates
    
    [myisamchk]
    #key_buffer_size = 20M
    #sort_buffer_size = 20M
    key_buffer_size                                             = 200M
    sort_buffer_size                                            = 200M
    read_buffer                                                 = 2M
    write_buffer                                                = 2M
    
    [mysqld_safe]
    #控制文件打开数的show global status like 'open%file%';比较合适的设置:Open_files / open_files_limit * 100% <= 75%
    open-files-limit                                            = 65535
    log-error                                                   = /data/mysql_data/error.log
    
    [mysqlhotcopy]
    interactive-timeout
    
    
    slave从库my.cnf配置
    
    
    [slave]# cat /etc/my.cnf
    # Example MySQL config file for medium systems.
    # 8核 16G
    # This is for a system with little memory (16G) where MySQL plays
    [client]
    #password       = your_password
    port                                                        = 3306
    socket                                                      = /tmp/mysql.sock
    
    # The MySQL server
    [mysqld]
    user                                                        = mysql
    port                                                        = 3306
    bind-address                                                = 0.0.0.0
    socket                                                      = /tmp/mysql.sock
    datadir                                                     = /data/mysql_data
    pid-file                                                    = /data/mysql_data/mysql.pid
    basedir                                                     = /usr/local/mysql
    tmpdir                                                      = /tmp
    
    #此开关默认为NULL,即不允许导入导出。
    #secure-file-priv                                           = /opt/upload
    
    #-------------------------------gobal variables------------------------#
    #默认关闭,涉及到timestamp类型的列自动更新的问题
    explicit_defaults_for_timestamp                 = 1
    ###transaction_write_set_extraction              = XXHASH64  #以便在server收集写集合的同时将其记录到二进制日志。并且是行更改后的唯一标识此标识将用于检测冲突。
    ###loose-group_replication_group_name            = 'ce9be252-2b71-11e6-b8f4-00212889f856' #组的名字可以随便起,但不能用主机的GTID
    ###loose-group_replication_start_on_boot         = off  #为了避免每次启动自动引导具有相同名称的第二个组,所以设置为OFF。
    ###loose-group_replication_bootstrap_group       = off #同上
    ###loose-group_replication_local_address         = '192.168.1.88:33071' #写自己主机所在IP
    ###loose-group_replication_group_seeds           ='192.168.1.88:33071,192.168.1.89:33071,192.168.1.90:33071'
    ###loose-group_replication_single_primary_mode   = off  #关闭单主模式的参数
    ###loose-group_replication_enforce_update_everywhere_checks = on #开启多主模式的参数
    
    skip-external-locking
    skip-name-resolve
    skip-ssl
    
    #memory is 16G
    key_buffer_size                                             = 32M
    table_open_cache                                            = 2048
    table_definition_cache                                      = 1024
    sort_buffer_size                                            = 4M
    net_buffer_length                                           = 32K
    read_buffer_size                                            = 4M
    read_rnd_buffer_size                                        = 16M
    
    open_files_limit                                            = 10000
    thread_cache_size                                           = 400
    query_cache_type                                            = 0
    query_cache_size                                            = 32M
    max_write_lock_count                                        = 300
    wait_timeout                                                = 120
    interactive_timeout                                         = 120
    net_read_timeout                                            = 120
    net_write_timeout                                           = 120
    
    max_connections                                             = 800
    max_user_connections                                        = 750
    max_connect_errors                                          = 10000
    max_allowed_packet                                          = 512M
    back_log                                                    = 2048
    log_timestamps                                              = system
    performance_schema                                          = OFF
    character_set_server                                        = utf8mb4
    
    ##当链接数耗尽后,通过设置别用端口,让root可以登录
    extra_max_connections                                       = 2
    extra_port                                                  = 13306
    
    ###让mysql不区分大小写敏感
    lower_case_table_names                                      = 1
    
    #explicit_defaults_for_timestamp                            = 1
    
    #----------------Myisam--------------------------------#
    myisam_recover_options                                      = DEFAULT
    bulk_insert_buffer_size                                     = 64M
    myisam_sort_buffer_size                                     = 128M
    myisam_max_sort_file_size                                   = 512M
    myisam_repair_threads                                       = 1
    
    #if the query is exec time great than 2 seconds, the query will log to slow log if slowlog is enabled.
    long_query_time                                             = 1
    slow_query_log                                              = On
    slow-query-log-file                                         = /data/mysql_data/slow.log
    show_compatibility_56                                       = on
    
    # Don't listen on a TCP/IP port at all. This can be a security enhancement,
    # if all processes that need to connect to mysqld run on the same host.
    # All interaction with mysqld must be made via Unix sockets or named pipes.
    # Note that using this option without enabling named pipes on Windows
    # (via the "enable-named-pipe" option) will render mysqld useless!
    # 
    #skip-networking
    
    #----------------------------MySQL Log----------------#
    # Replication Master Server (default)
    # binary logging is required for replication
    log-bin                                                     = mysql-bin
    expire_logs_days                                            = 20
    log_error                                                   = error.log
    log_error_verbosity                                         = 1
    log_warnings                                                = 1
    
    # binary logging format - mixed recommended
    binlog_format                                               = row
    relay-log                                                   = mysql-relay-bin
    relay-log-index                                             = relay.index
    # required unique id between 1 and 2^32 - 1
    server-id                                                   = 52
    #sql-mode                                                    = "NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
    #sql-mode                                                    = "NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
    sync_binlog                                                 = 1
    log_slave_updates                                           = 1
    #binlog_checksum                                            = NONE
    
    #------------------------------replicate--------------#
    #排除不需要同步的库表
    #replicate-ignore-db                                        = mysql
    #replicate-ignore-db                                        = sys
    replicate-ignore-db                                         = information_schema
    replicate-ignore-db                                         = performance_schema
    replicate-ignore-db                                         = undolog
    replicate-ignore-db                                         = for_nagios
    replicate-ignore-db                                         = undolog
    
    #replicate_wild_ignore_table                                = mysql.%
    #replicate_wild_ignore_table                                = sys.%
    replicate_wild_ignore_table                                 = information_schema.%
    replicate_wild_ignore_table                                 = performance_schema.%
    replicate_wild_ignore_table                                 = undolog.%
    replicate_wild_ignore_table                                 = for_nagios.%
    replicate_wild_ignore_table                                 = undolog.%
    
    #主主复制需要开启
    #auto_increment_offset= 2
    #auto_increment_increment= 2
    
    #GTID模式复制,需要开启如下
    #gtid_mode                                                  = ON
    #enforce_gtid_consistency                                   = ON
    
    #并发复制
    slave-parallel-type                                         = LOGICAL_CLOCK
    slave-parallel-workers                                      = 2
    master_info_repository                                      = TABLE
    relay_log_info_repository                                   = TABLE
    relay_log_recovery                                          = ON
    
    #跳过slave进程启动参数
    skip-slave-start
    
    #如果实例为从库,则需要设置为on
    read_only                                                   = on
    
    #skip-grant-tables
    
    #--------------------------------------------------------innoDB------------#
    innodb_rollback_on_timeout
    # Uncomment the following if you are using InnoDB tables
    innodb_data_home_dir                                        = /data/mysql_data
    innodb_data_file_path                                       = ibdata1:1G;ibdata2:1G:autoextend
    innodb_log_group_home_dir                                   = /data/mysql_data
    innodb_undo_directory                                       = /data/mysql_data/undolog/
    innodb_undo_logs                                            = 128
    innodb_undo_tablespaces                                     = 3
    
    # You can set .._buffer_pool_size up to 50 - 80 %
    #innodb_use_sys_malloc = 0 
    #innodb_page_size = 8192
    innodb_buffer_pool_size                                     = 3G
    innodb_buffer_pool_instances                                = 1
    #innodb_additional_mem_pool_size = 8M
    
    # Set .._log_file_size to 25 % of buffer pool size
    innodb_log_file_size                                        = 256M
    innodb_log_buffer_size                                      = 64M
    innodb_log_files_in_group                                   = 3
    #每次事务提交时MySQL都会把log buffer的数据写入log file,并且flush(刷到磁盘)中去,该模式为系统默认
    innodb_flush_log_at_trx_commit                              = 1
    innodb_lock_wait_timeout                                    = 120
    #启用独立表空间
    innodb_file_per_table                                       = 1
    
    #CPU是1颗8核的,那么可以设置
    innodb_read_io_threads                                      = 8
    innodb_write_io_threads                                     = 8
    
    #默认是0,则表示没有并发线程数限制,所有请求都会直接请求线程执行,当并发用户线程数量小于64,建议设置innodb_thread_concurrency=0,在大多数情况下,最佳的值是小于并接近虚拟CPU的个数
    innodb_thread_concurrency                                   = 12
    innodb_max_dirty_pages_pct                                  = 75
    innodb_flush_method                                         = O_DIRECT
    
    innodb_purge_threads                                        = 10
    innodb_large_prefix                                         = 1
    
    #参数待测试
    #innodb_io_capacity                                         = 20000
    #innodb_io_capacity_max                                     = 40000
    
    #根据CPU核心数来设定
    thread_pool_size                                            = 8
    #thread_handling = pool-of-threads
    thread_pool_oversubscribe                                   = 24
    
    #thread_handling                             = pool-of-threads
    thread_pool_stall_limit                                     = 100
    thread_pool_max_threads                                     = 30
    
    #解释: 在启动时把热数据加载到内存。
    innodb_buffer_pool_load_at_startup                          = 1
    #解释: 在关闭时把热数据dump到本地磁盘
    innodb_buffer_pool_dump_at_shutdown                         = 1
    
    ##默认是8M, 如果一次insert数据量比较多的话, 可以适当增加
    innodb_autoextend_increment                                 = 32
    
    [mysqldump]
    quick
    max_allowed_packet                                          = 512M
    
    [mysql]
    no-auto-rehash
    # Remove the next comment character if you are not familiar with SQL
    #safe-updates
    
    [myisamchk]
    #key_buffer_size = 20M
    #sort_buffer_size = 20M
    key_buffer_size                                             = 200M
    sort_buffer_size                                            = 200M
    read_buffer                                                 = 2M
    write_buffer                                                = 2M
    
    [mysqld_safe]
    #控制文件打开数的show global status like 'open%file%';比较合适的设置:Open_files / open_files_limit * 100% <= 75%
    open-files-limit                                            = 65535
    log-error                                                   = /data/mysql_data/error.log
    
    [mysqlhotcopy]
    interactive-timeout
    
    
    备份计划:
    1.备份脚本的目录结构
    
    [/usr/local/worksh]# tree xtrabackup_cron/
    xtrabackup_cron/
    ├── bin
    │   └── mysql_increment_hot_backup.sh
    ├── conf
    │   └── mysql_increment_hot_backup.conf
    ├── log
    └── var
    
    备份脚本
    
    [/usr/local/worksh]# cat /usr/local/worksh/xtrabackup_cron/bin/mysql_increment_hot_backup.sh 
    #!/usr/bin/env bash
    
    # Program: MySQL 增量备份脚本 使用 percona xtrabackup
    ##进入程序目录
    cd /usr/local/worksh/xtrabackup_cron/bin
    # 读取配置文件中的所有变量值, 设置为全局变量
    # 配置文件
    conf_file="../conf/mysql_increment_hot_backup.conf"
    # mysql 用户
    user=`sed '/^user=/!d;s/.*=//' $conf_file`
    # mysql 密码
    #password=`sed '/^password=/!d;s/.*=//' $conf_file`
    password="xtrabackup**123"
    
    
    # mysql 备份目录
    backup_dir=`sed '/^backup_dir=/!d;s/.*=//' $conf_file`
    # percona-xtrabackup 备份软件路径
    xtrabackup_dir=`sed '/^xtrabackup_dir=/!d;s/.*=//' $conf_file`
    # 全备是在一周的第几天
    full_backup_week_day=`sed '/^full_backup_week_day=/!d;s/.*=//' $conf_file`
    # mysql 全备前缀标识
    full_backup_prefix=`sed '/^full_backup_prefix=/!d;s/.*=//' $conf_file`
    # mysql 增量备前缀标识
    increment_prefix=`sed '/^increment_prefix=/!d;s/.*=//' $conf_file`
    # mysql 配置文件
    mysql_conf_file=`sed '/^mysql_conf_file=/!d;s/.*=//' $conf_file`
    # 备份错误日志文件
    error_log=`sed '/^error_log=/!d;s/.*=//' $conf_file`
    # 备份索引文件
    index_file=`sed '/^index_file=/!d;s/.*=//' $conf_file`
    
    # 备份日期
    backup_date=`date +%F`
    # 备份日期
    backup_time=`date +%H-%M-%S`
    # 备份日期
    backup_week_day=`date +%u`
    
    # 设置备份线程数
    backup_thread=4
    
    # 创建相关目录
    log_dir=../log
    var_dir=../var
    mkdir -p $backup_dir
    mkdir -p $log_dir
    mkdir -p $var_dir
    
    
    
    # 全量备份
    function full_backup() {
      backup_folder=${full_backup_prefix}_${backup_date}_${backup_time}_${backup_week_day}
    
      mkdir -p $backup_dir/$backup_folder
      $xtrabackup_dir/bin/innobackupex   --defaults-file=$mysql_conf_file --user=$user --password=$password --parallel=$backup_thread --no-timestamp  $backup_dir/$backup_folder > $log_dir/${backup_folder}.log 2>&1
      return $?
    }
    
    # 增量备份
    function increment_backup() {
      backup_folder=${increment_prefix}_${backup_date}_${backup_time}_${backup_week_day}
      incr_base_folder=`sed -n '$p' $index_file | 
                       awk -F '[, {}]*' '{print $3}' | 
                       awk -F ':' '{print $2}'`
    
      mkdir -p $backup_dir/$backup_folder
      $xtrabackup_dir/bin/innobackupex 
        --defaults-file=$mysql_conf_file 
        --user=$user 
        --password=$password 
        --no-timestamp 
        --incremental 
        $backup_dir/$backup_folder 
        --incremental-basedir=$backup_dir/$incr_base_folder > $log_dir/${backup_folder}.log 2>&1
      return $?
    }
    
    # 删除之前的备份(一般在全备完成后使用)
    function delete_before_backup() {
      cat $index_file | awk -F '[, {}]*' '{print $3}' | 
        awk -v backup_dir=$backup_dir -F ':' '{if($2!=""){printf("rm -rf %s/%s
    ", backup_dir, $2)}}' | 
        /bin/bash
      
      cat $index_file | awk -F '[, {}]*' '{print $3}' | 
        awk -v log_dir=$log_dir -F ':' '{if($2!=""){printf("rm -rf %s/%s.log
    ", log_dir, $2)}}' | 
        /bin/bash
    }
    
    # 备份索引文件
    function backup_index_file() {
      cp $index_file ${index_file}_$(date -d "1 day ago" +%F)
    }
    
    # 备份索引文件
    function send_index_file_to_remote() {
      echo 'send index file ok'
    }
    
    # 添加索引, 索引记录了当前最新的备份
    function append_index_to_file() {
      echo "{week_day:$backup_week_day, 
             dir:${1}_${backup_date}_${backup_time}_${backup_week_day}, 
             type:${1}, 
             date:${backup_date}}" >> $index_file
    }
    
    # 记录 错误消息到文件
    function logging_backup_err() {
      echo "{week_day:$backup_week_day, 
             dir:${1}_${backup_date}_${backup_time}_${backup_week_day}, 
             type:${1}, 
             date:${backup_date}}" >> $error_log
    }
    
    # 清空索引
    function purge_index_from_file() {
      > $index_file
    }
    
    # 清空错误日志信息
    function purge_err_log() {
      > $error_log
    }
    
    # 打包备份
    function tar_backup_file() {
      echo "tar $1 ok"
    }
    
    # 发送备份到远程
    function send_backup_to_remote() {
      echo "send $1 remote ok"
    }
     
    # 判断是应该全备还是增量备份
    # 0:full, 1:incr
    function get_backup_type() {
      full_backup_week_day=`sed '/^full_backup_week_day=/!d;s/.*=//' $conf_file`
      backup_type=0
      if [ "$full_backup_week_day" -eq `date +%u` ]; then
        backup_type=0
      else
        backup_type=1
      fi
      if [ ! -n "`cat $index_file`" ]; then
        backup_type=0
      fi
      return $backup_type
    }
    
    # 测试配置文件正确性
    function test_conf_file() {
      # 判断每个变量是否在配置文件中有配置,没有则退出程序
      if [ ! -n "$user" ]; then echo 'fail: configure file user not set'; exit 2; fi
      if [ ! -n "$password" ]; then echo 'fail: configure file password not set'; exit 2; fi
      if [ ! -n "$backup_dir" ]; then echo 'fail: configure file backup_dir not set'; exit 2; fi
      if [ ! -n "$full_backup_week_day" ]; then echo 'fail: configure file full_backup_week_day not set'; exit 2; fi
      if [ ! -n "$full_backup_prefix" ]; then echo 'fail: configure file full_backup_prefix not set'; exit 2; fi
      if [ ! -n "$increment_prefix" ]; then echo 'fail: configure file increment_prefix not set'; exit 2; fi
      if [ ! -n "$mysql_conf_file" ]; then echo 'fail: configure file mysql_conf_file not set'; exit 2; fi
      if [ ! -n "$error_log" ]; then echo 'fail: configure file error_log not set'; exit 2; fi
      if [ ! -n "$index_file" ]; then echo 'fail: configure file index_file not set'; exit 2; fi
    }
    
    # 执行
    function run() {
      # 检测配置文件值
      test_conf_file
    
      # 判断是执行全备还是曾量备份
      get_backup_type
      backup_type=$?
      case $backup_type in
        0 )
          # 全量备份
          full_backup 
          backup_ok=$?
          if [ 0 -eq "$backup_ok" ]; then
          # 全备成功
            # # 打包最新备份
            # tar_backup_file $full_backup_prefix
            # # 将tar备份发送到远程
            # send_backup_to_remote $full_backup_prefix
            # 备份索引文件
            backup_index_file
            # # 发送索引文件到远程
            # send_index_file_to_remote
            # 清除之前的备份
            delete_before_backup
            # 清除索引文件
            purge_index_from_file
            # 添加索引, 索引记录了当前最新的备份
            append_index_to_file $full_backup_prefix
          else
          # 全备失败
            # 删除备份目录
            rm -rf ${backup_dir}/${full_backup_prefix}_${backup_date}_${backup_time}_${backup_week_day}
            # 记录错误日志
            logging_backup_err $full_backup_prefix
          fi
          ;;
        1 )
          # 增量备份
          increment_backup
          backup_ok=$?
          if [ 0 -eq "$backup_ok" ]; then
          # 增量备份成功
            # # 打包最新备份
            # tar_backup_file $increment_prefix
            # # 将tar备份发送到远程
            # send_backup_to_remote $increment_prefix
            # 添加索引, 索引记录了当前最新的备份
            append_index_to_file $increment_prefix
          else
          # 增量备份失败
            # 删除备份目录
            rm -rf ${backup_dir}/${full_backup_prefix}_${backup_date}_${backup_time}_${backup_week_day}
            # 记录错误日志
            logging_backup_err $increment_prefix
          fi
          ;;
      esac
    }
    
    run
    chown -R apache.users /data/MySQL_Data_Backup
    
    配置文件
    # cat /usr/local/worksh/xtrabackup_cron/conf/mysql_increment_hot_backup.conf
    # 备份专用用户
    # GRANT RELOAD, PROCESS, SUPER, LOCK TABLES, REPLICATION CLIENT, CREATE TABLESPACE ON *.* TO 'xtrabackup'@'localhost' identified by 'xtrabackuppass';
    # flush privileges;
    
    # mysql 用户名
    user=xtrabackup
    
    # mysql 密码
    password="xtrabackuppass"
    
    # 备份存放路径
    backup_dir=/data/MySQL_Data_Backup
    
    # percona-xtrabackup 备份软件路径
    xtrabackup_dir=/usr
    
    # 全备是在一周的第几天
    full_backup_week_day=4
    
    # 全量备信息名称 前缀
    full_backup_prefix=full
    
    # 增量备信息名称 前缀
    increment_prefix=incr
    
    # mysql配置文件
    mysql_conf_file=/etc/my.cnf
    
    # 错误日志文件(更具此文件知道备份是否成功)
    # format:
    # {week_day:1,dir:full/incr_2015-12-29_00-00-00_7,type:full/incr,date:2015-12-30}
    error_log=../var/mysql_increment_hot_backup.err
    
    # 索引文件
    # format:
    # {week_day:1,dir:full/incr_2015-12-29_00-00-00_7,type:full/incr}
    index_file=../var/mysql_increment_hot_backup.index
    
    
    2.添加计划任务crontab -e
    
    ########mysql backup 每周4进行全备份,每天定时增量备份,保留一周
    03       01       *       *       *  /usr/local/worksh/xtrabackup_cron/bin/mysql_increment_hot_backup.sh
  • 相关阅读:
    Jenkins参数化构建
    python笔记
    jenkins定时任务
    技巧:Vimdiff 使用
    clover如何使用UEFI引导和EFI驱动选择
    Broadcast BCM94322 用ubuntu修改ID
    MAC实现睡眠和休眠唤醒
    MAC的睡眠模式介绍
    linux 用dd命令读写引导区文件
    MAC下打开FTP服务
  • 原文地址:https://www.cnblogs.com/reblue520/p/11331010.html
Copyright © 2011-2022 走看看