[mysql5.7双主搭建及常见问题]

搭建mysql5.7双主结构

  1. 安装mysql5.7

     wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-common-5.7.25-1.el7.x86_64.rpm
     wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-libs-5.7.25-1.el7.x86_64.rpm
     wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-client-5.7.25-1.el7.x86_64.rpm
     wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-libs-compat-5.7.25-1.el7.x86_64.rpm
     wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-devel-5.7.25-1.el7.x86_64.rpm
     wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-server-5.7.25-1.el7.x86_64.rpm
     yum install *.rpm --nogpgcheck
    
  2. 修改配置文件

     cd /etc/my.cnf
    

    节点1:

     [mysqld]
     #
     # * Basic Settings
     #
     user            = mysql
     pid-file        = /var/run/mysqld/mysqld.pid
     socket          = /var/lib/mysql/mysql.sock
     log-error       = /var/log/mysql/error.log
     port            = 3306
     basedir         = /usr
     datadir     = /mnt/data/mysql
     character_set_server = utf8
     tmpdir          = /tmp
     lc_messages_dir = /usr/share/mysql
     lc_messages     = en_US
     skip-external-locking
     
     # * Fine Tuning
     #
     max_connections         = 500
     max_user_connections    = 500
     max_connect_errors      = 100000
     connect_timeout         = 1800
     wait_timeout            = 1800
     max_allowed_packet      = 16M
     thread_cache_size       = 128
     thread_stack            = 256k
     sort_buffer_size        = 2M
     bulk_insert_buffer_size = 8M
     tmp_table_size          = 128M
     max_heap_table_size     = 128M
     #
     # * MyISAM
     #
     # This replaces the startup script and checks MyISAM tables if needed
     # the first time they are touched. On error, make copy and try a repair.
     myisam_recover_options = BACKUP
     key_buffer_size         = 64M
     open-files-limit        = 5000
     back_log                = 512
     table_open_cache        = 2048
     table_definition_cache  = 6144
     myisam_sort_buffer_size = 256M
     concurrent_insert       = 2
     read_buffer_size        = 2M
     read_rnd_buffer_size    = 16M
     
     #
     # * Query Cache Configuration
     #
     # Cache only tiny result sets, so we can fit more in the query cache.
     query_cache_limit               = 0K
     query_cache_size                = 0M
     query_cache_type        = OFF
     # for more write intensive setups, set to DEMAND or OFF
     
     # * Logging and Replication
     
     general_log_file        = /var/lib/mysql/mysql.log
     general_log             = 1
     
     
     slow_query_log=1
     slow_query_log_file     = /var/lib/mysql/mysql-slow.log
     long_query_time = 10
     #log_slow_verbosity     = query_plan
     lower_case_table_names  = 1
     
     #主节点设置为1
     server-id               = 1 
     log-bin=mysql-bin
     gtid-mode                = ON
     enforce-gtid-consistency = ON
     #自增gtid起始id
     auto_increment_offset   = 1
     #自增gtid步长
     auto_increment_increment = 2
     
     #binlog
     binlog_format = row
     
     log_bin                 = /var/log/mysql/mysql-bin.log
     log_bin_index   = /var/log/mysql/mysql-bin.index
     
     expire_logs_days        = 7
     max_binlog_size         = 100M
     
     
     sql_mode                = STRICT_TRANS_TABLES
     
     # InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
     # Read the manual for more InnoDB related options. There are many!
     default_storage_engine  = InnoDB
     innodb_open_files       = 400
     innodb_buffer_pool_size = 1526M
     innodb_log_file_size = 128M
     innodb_log_buffer_size = 64M
     innodb_log_files_in_group = 3
     innodb_flush_log_at_trx_commit = 1
     innodb_lock_wait_timeout = 30
     innodb_max_dirty_pages_pct = 80
     innodb_thread_concurrency = 0
     innodb_flush_method = O_DIRECT
     innodb_read_io_threads = 12
     innodb_write_io_threads = 12
     innodb_io_capacity = 2000
     innodb_purge_threads = 2
     innodb_purge_batch_size = 64
     innodb_old_blocks_pct = 50
     transaction_isolation = READ-COMMITTED
     
     
     bind-address=0.0.0.0
     
     
     [isamchk]
     key_buffer              = 16M
    

    节点2:

     [mysqld]
     #
     # * Basic Settings
     #
     user            = mysql
     pid-file        = /var/run/mysqld/mysqld.pid
     socket          = /var/lib/mysql/mysql.sock
     log-error       = /var/log/mysql/error.log
     port            = 3306
     basedir         = /usr
     datadir     = /mnt/data/mysql
     character_set_server = utf8
     tmpdir          = /tmp
     lc_messages_dir = /usr/share/mysql
     lc_messages     = en_US
     skip-external-locking
     
     # * Fine Tuning
     #
     max_connections         = 500
     max_user_connections    = 500
     max_connect_errors      = 100000
     connect_timeout         = 1800
     wait_timeout            = 1800
     max_allowed_packet      = 16M
     thread_cache_size       = 128
     thread_stack            = 256k
     sort_buffer_size        = 2M
     bulk_insert_buffer_size = 8M
     tmp_table_size          = 128M
     max_heap_table_size     = 128M
     #
     # * MyISAM
     #
     # This replaces the startup script and checks MyISAM tables if needed
     # the first time they are touched. On error, make copy and try a repair.
     myisam_recover_options = BACKUP
     key_buffer_size         = 64M
     open-files-limit        = 5000
     back_log                = 512
     table_open_cache        = 2048
     table_definition_cache  = 6144
     myisam_sort_buffer_size = 256M
     concurrent_insert       = 2
     read_buffer_size        = 2M
     read_rnd_buffer_size    = 16M
     
     #
     # * Query Cache Configuration
     #
     # Cache only tiny result sets, so we can fit more in the query cache.
     query_cache_limit               = 0K
     query_cache_size                = 0M
     query_cache_type        = OFF
     # for more write intensive setups, set to DEMAND or OFF
     
     # * Logging and Replication
     
     general_log_file        = /var/lib/mysql/mysql.log
     general_log             = 1
     
     
     slow_query_log=1
     slow_query_log_file     = /var/lib/mysql/mysql-slow.log
     long_query_time = 10
     #log_slow_verbosity     = query_plan
     lower_case_table_names  = 1
     
     #备节点id设置为2
     server-id               = 2
     log-bin			= mysql-bin
     gtid-mode                = ON
     enforce-gtid-consistency = ON
     #自增gtid起始设置为2,步长为2,这样就不会和master重复
     auto_increment_offset   = 2
     auto_increment_increment = 2
     
     #binlog
     binlog_format = row
     
     log_bin                 = /var/log/mysql/mysql-bin.log
     log_bin_index   = /var/log/mysql/mysql-bin.index
     
     expire_logs_days        = 7
     max_binlog_size         = 100M
     
     
     sql_mode                = STRICT_TRANS_TABLES
     
     # InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
     # Read the manual for more InnoDB related options. There are many!
     default_storage_engine  = InnoDB
     innodb_open_files       = 400
     innodb_buffer_pool_size = 1526M
     innodb_log_file_size = 128M
     innodb_log_buffer_size = 64M
     innodb_log_files_in_group = 3
     innodb_flush_log_at_trx_commit = 1
     innodb_lock_wait_timeout = 30
     innodb_max_dirty_pages_pct = 80
     innodb_thread_concurrency = 0
     innodb_flush_method = O_DIRECT
     innodb_read_io_threads = 12
     innodb_write_io_threads = 12
     innodb_io_capacity = 2000
     innodb_purge_threads = 2
     innodb_purge_batch_size = 64
     innodb_old_blocks_pct = 50
     transaction_isolation = READ-COMMITTED
     
     bind-address=0.0.0.0
     
     
     [isamchk]
     key_buffer              = 16M
    
  3. 初始化mysql

    在配置文件中加上skip-grant-tables,跳过权限认证

     [mysqld]
     skip-grant-tables
    

    启动mysql

     systemctl start mysqld
    

    初始化root用户

     mysql -u root
     mysql> set global validate_password_policy=0;
     mysql> set global validate_password_length=0;
     mysql> alter user root@'localhost' identified by 'password';
    

    把配置文件改回来后重启mysqld

     [mysqld]
     #skip-grant-tables
    
     systemctl restart mysqld
    
  4. 创建同步账号

    节点1:

     CREATE USER 'repl1'@'10.1.16.11' IDENTIFIED BY 'RepAdmin,.123';
     GRANT REPLICATION SLAVE ON *.* TO 'repl1'@'server2_ip';
    
     CHANGE MASTER TO MASTER_HOST = 'server2_ip', 
     MASTER_PORT = 3306, 
     MASTER_USER = 'repl2', 
     MASTER_PASSWORD = 'RepAdmin,.123', 	
     # 自动根据gtid进行复制
     MASTER_AUTO_POSITION = 1;
    

    节点2:

     CREATE USER 'repl2'@'10.1.16.12' IDENTIFIED BY 'RepAdmin,.123';
     GRANT REPLICATION SLAVE ON *.* TO 'repl2'@'server1_ip';
    
     CHANGE MASTER TO MASTER_HOST = 'server2_ip', 
     MASTER_PORT = 3306, 
     MASTER_USER = 'repl2', 
     MASTER_PASSWORD = 'RepAdmin,.123', 	
     # 自动根据gtid进行复制
     MASTER_AUTO_POSITION = 1;
    

    两个节点账号创建完之后启动复制

     start slave
    

    查看复制状态,等待master发事件就是复制状态正常了

     show slave status
    

    在这里插入图片描述

常见问题

  1. master的binlog,gtid为空
    在这里插入图片描述
    查看master的gtid集合

     SHOW GLOBAL VARIABLES LIKE 'gtid_executed';
    

    如果确实为空,随便执行一条sql即可,如:

     create database test;
     drop database test;
    
  2. slave的gitd比master多
    查看两个节点的gtid集合

     SHOW GLOBAL VARIABLES LIKE 'gtid_executed';
    

    如果master为空的,slave有gtid集合,把salve上面的集合清理掉,在slave上

     reset master
    
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dmonstererer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值