暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

MySQL主从配置

栗子Jie 2020-06-03
610

1.主从配置概念 

数据库主从同步可以将数据一个数据库服务器复制到其他服务器上,在复制数据时,一个服务器充当主服务器(master),其余的服务器充当从服务器(slave)。通过配置文件,可以指定复制所有的数据库,某个数据库,甚至是某个数据库上的某个表。

2.主从配置原理

MySQL数据库主从同步主要是基于二进制日志机制,主服务器使用二进制日志来记录数据库的变动情况,从服务器通过读取和执行该日志文件来保持和主服务器的数据一致。主从服务器上都需要开启binlog才能进行主从。


3.主从配置步骤
  • 主服务器配置(master)
    主服务器my.cnf 文件修改
    开启binlog日志功能,配置读写权限(read-only),并配置需要复制的数据库名(或表名)和不需要进行备份(ignore)表。

    For advice on how to change settings please see
    # # http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html


    [client]
    socket=/lizijie/db/mysql.sock
    default-character-set=UTF8


    [mysqld]
    # 端口号
    port=3306
    # server-id 需要唯一,与从服务器不可重复
    server-id=1
    # 默认时区
    default-time_zone='+8:00'
    # slave-skip-errors在从库配置即可
    slave-skip-errors=1032,1049,1053,1062,1146
    # binlog日志开启
    log-bin=binlog
    binlog_format=row
    binlog_expire_logs_seconds=604800
    log_bin_trust_function_creators=1
    # 需要备份的表
    binlog-do-db=check
    # 不需要备份的表
    binlog-ignore-db=mysql
    binlog-ignore-db=information_schema
    binlog-ignore-db=performance_schema
    binlog-ignore-db=sys


    gtid_executed_compression_period=1000
    gtid_mode=on
    enforce_gtid_consistency=on


    character-set-server=UTF8
    collation-server=utf8_unicode_ci
    skip-host-cache
    skip-name-resolve


    innodb_buffer_pool_size=8G
    max_connections=2000
    wait_timeout=28800
    lower_case_table_names=1
    transaction-isolation=READ-COMMITTED


    slow_query_log=on
    long_query_time=0.1


    binlog_cache_size=2M
    innodb_open_files=3000
    max_allowed_packet=1G
    open_files_limit=65535
    performance_schema=OFF
    tmp_table_size=256M
    max_heap_table_size=256M
    max_write_lock_count=102400
    thread_cache_size=100
    sort_buffer_size=4M
    key_buffer_size=32M
    read_rnd_buffer_size = 4M
    read_buffer_size=4M
    join_buffer_size=128M
    innodb_flush_log_at_trx_commit=2
    innodb_log_file_size=1G
    innodb_buffer_pool_instances=8


    innodb_io_capacity=2000
    innodb_io_capacity_max=4000


    # Remove leading # and set to the amount of RAM for the most important data
    # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
    # innodb_buffer_pool_size = 128M


    # Remove leading # to turn on a very important data integrity option: logging
    # changes to the binary log between backups.
    # log_bin


    # These are commonly set, remove the # and set as required.
    # basedir = .....
    datadir = lizijie/db
    socket = lizijie/db/mysql.sock
    log-error = lizijie/log/mysqld.log
    pid-file = var/run/mysqld/mysqld.pid
    # port = .....
    # server_id = .....
    # socket = .....


    # Remove leading # to set options mainly useful for reporting servers.
    # The server defaults are faster for transactions and fast SELECTs.
    # Adjust sizes as needed, experiment to find the optimal values.
    # join_buffer_size = 128M
    # sort_buffer_size = 2M
    # read_rnd_buffer_size = 2M
    #
    # Remove leading # to revert to previous value for default_authentication_plugin,
    # # this will increase compatibility with older clients. For background, see:
    # # https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
    default-authentication-plugin=mysql_native_password


    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES


    Master配置用于复制的用户(IP为SlaveIP地址)

      -- mysql5.7之前版本
      grant replication slave on *.* to 'backup'@'192.168.30.132'
      identified by '123456';

        -- mysql5.7之后版本
        create user 'backup'@'192.168.30.130' identified with mysql_native_password by '123456';
        grant replication slave on *.* to 'backup'@'192.168.30.132';

         重新启动MySQL 

          /sbin/service mysql restart;

          获取Master同步状态

            # 锁定数据库 
            mysql> flush tables with read lock
            # 新打开一个终端,导出数据
            mysqldump --master-data -uroot -p test> test.sql
            # 查看Master状态,记录Position 和 File值 这个值需要在从服务器上进行配置
            show master status\G;
            # 解锁数据库
            mysql> unlock tables;


            • 从服务器配置

            从服务器my.cnf 文件修改

              # For advice on how to change settings please see
              # # http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html


              [client]
              socket=/lizijie/db/mysql.sock
              default-character-set=UTF8


              [mysqld]
              port=3306
              server-id=2
              default-time_zone='+8:00'
              #slave-skip-errors在从库配置即可
              slave-skip-errors=1032,1049,1053,1062,1146,22
              log-bin=binlog
              binlog_format=row
              read_only=1
              super_read_only=1


              binlog-do-db=check
              binlog-ignore-db=mysql
              binlog-ignore-db=information_schema
              binlog-ignore-db=performance_schema
              binlog-ignore-db=sys


              replicate-do-db=check
              replicate-ignore-db=mysql
              replicate-ignore-db=information_schema
              replicate-ignore-db=performance_schema
              replicate-ignore-db=sys


              skip_slave_start=1


              gtid_executed_compression_period=1000
              gtid_mode=on
              enforce_gtid_consistency=on
              #中继日志的名字。复制线程先把远程的变化拷贝到这个中继日志中再执行。
              relay_log=mysqld-relay-bin
              #意思是,中继日志执行之后,这些变化是否需要计入自己的binarylog。
              #当你的 B服务器需要作为另外一个服务器的主服务器的时候需要打开。
              #就是双主互相备 份,或者多主循环备份。如果需要,则打开。
              #log-slave-updates=ON
              character-set-server=UTF8
              skip-host-cache
              skip-name-resolve


              innodb_buffer_pool_size=8G
              max_connections=2000
              wait_timeout=28800
              lower_case_table_names=1
              transaction-isolation=READ-COMMITTED


              slow_query_log=on
              long_query_time=0.1


              binlog_cache_size=2M
              innodb_open_files=3000
              max_allowed_packet=1G
              open_files_limit=65535
              performance_schema=OFF
              tmp_table_size=256M
              max_heap_table_size=256M
              max_write_lock_count=102400
              thread_cache_size=100
              sort_buffer_size=4M
              key_buffer_size=32M
              read_rnd_buffer_size = 4M
              read_buffer_size=4M
              join_buffer_size=128M
              innodb_flush_log_at_trx_commit=2
              innodb_log_file_size=1G
              innodb_buffer_pool_instances=8


              innodb_io_capacity=2000
              innodb_io_capacity_max=4000


              # Remove leading # and set to the amount of RAM for the most important data
              # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
              # innodb_buffer_pool_size = 128M


              # Remove leading # to turn on a very important data integrity option: logging
              # changes to the binary log between backups.
              # log_bin


              # These are commonly set, remove the # and set as required.
              # basedir = .....
              datadir = lizijie/db
              socket = /lizijie/db/mysql.sock
              log-error = /lizijie/log/mysqld.log
              pid-file = /var/run/mysqld/mysqld.pid
              # port = .....
              # server_id = .....
              # socket = .....


              # Remove leading # to set options mainly useful for reporting servers.
              # The server defaults are faster for transactions and fast SELECTs.
              # Adjust sizes as needed, experiment to find the optimal values.
              # join_buffer_size = 128M
              # sort_buffer_size = 2M
              # read_rnd_buffer_size = 2M
              #
              # Remove leading # to revert to previous value for default_authentication_plugin,
              # # this will increase compatibility with older clients. For background, see:
              # # https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
              default-authentication-plugin=mysql_native_password


              sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES


              重启从服务器 
                /sbin/service mysql restart


                导入数据,开启同步 
                  mysql> create database test default charset utf8;
                  mysql -uroot -p test< test.sql
                  从服务器开启同步
                    change master to master_host='192.168.30.132',master_user='backup',master_password='egova',master_connect_retry=60, master_log_file='binlog.000010',master_log_pos=155;

                    重启MySQL查看状态是否已经开启


                    如果状态为Yes,则说明主从配置成功。数据已经开始同步。

                    文章转载自栗子Jie,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                    评论