MySQL主从复制(异步模式)

MySQL主从复制有异步模式、半同步模式、GTID模式以及多源复制模式,MySQL默认模式是异步模式。所谓异步模式,只MySQL 主服务器上I/O thread 线程将二进制日志写入binlog文件之后就返回客户端结果,不会考虑二进制日志是否完整传输到从服务器以及是否完整存放到从服务器上的relay日志中,这种模式一旦主服务(器)宕机,数据就会发生丢失。

环境:

1 [[email protected] ~]# cat /etc/redhat-release
2 CentOS Linux release 7.2.1511 (Core)
3 [[email protected] ~]# uname -a
4 Linux localhost.localdomain 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
5 [[email protected] ~]# 

MySQL版本:

mysql-5.7.22-linux-glibc2.12-x86_64.tar

主从复制IP规划:

主:192.168.112.2220

从:192.168.112.221

部署步骤:

tar xf  mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
mkdir -p /application
mv mysql-5.7.22-linux-glibc2.12-x86_64 /application/
ln -s /application/mysql-5.7.22-linux-glibc2.12-x86_64 /application/mysql
groupadd mysql
useradd mysql -g mysql -s /sbin/nologin
mkdir -p /application/data
chown -R mysql.mysql /application/mysql
chown -R mysql.mysql /application/mysql/*
chown -R mysql.mysql /application/data

安装常用依赖库:

[[email protected] ~]# yum install cmake gcc gcc-c++ libaio-devel automake autoconf bison libtool ncurses-devel libgcrypt-devel libev-devel perl-Digest-MD5 perl-DBI  perl-DBD-MySQL

编写配置文件my.cnf:

  1 [client]
  2 port = 3306
  3 socket = /tmp/mysql.sock
  4
  5 [mysql]
  6 prompt="\[email protected] \R:\m:\s [\d]> "
  7 no-auto-rehash
  8
  9 [mysqld]
 10 user = mysql
 11 port = 3306
 12 basedir = /application/mysql
 13 datadir = /application/data/mysql/
 14 socket = /tmp/mysql.sock
 15 character-set-server = utf8mb4
 16 skip_name_resolve = 1
 17 open_files_limit = 65535
 18 back_log = 1024
 19 max_connections = 512
 20 max_connect_errors = 100000
 21 table_open_cache = 1024
 22 table_definition_cache = 1024
 23 thread_stack = 512K
 24 external-locking = FALSE
 25 max_allowed_packet = 32M
 26 sort_buffer_size = 4M
 27 join_buffer_size = 4M
 28 thread_cache_size = 0
 29 query_cache_size = 0
 30 query_cache_type = 0
 31 interactive_timeout = 600
 32 wait_timeout = 600
 33 tmp_table_size = 32M
 34 slow_query_log =1
 35 slow_query_log_file = /application/data/mysql/slow.log
 36 log-error = /application/data/mysql/error.log
 37 long_query_time = 0.5
 38 server-id = 33060220
 39 log-bin = /application/data/mysql/mysql-binlog
 40 sync_binlog = 1
 41 binlog_cache_size = 4M
 42 max_binlog_cache_size = 1G
 43 max_binlog_size = 1G
 44 expire_logs_days = 7
 45 master_info_repository = TABLE
 46 relay_log_info_repository = TABLE
 47 #gtid_mode = on
 48 enforce_gtid_consistency = 1
 49 log_slave_updates
 50 binlog_format = row
 51 relay_log_recovery = 1
 52 relay-log-purge = 1
 53 key_buffer_size = 32M
 54 read_buffer_size = 8M
 55 read_rnd_buffer_size = 4M
 56 bulk_insert_buffer_size = 64M
 57
 58 lock_wait_timeout = 3600
 59 explicit_defaults_for_timestamp = 1
 60 innodb_thread_concurrency = 0
 61 innodb_sync_spin_loops = 100
 62 innodb_spin_wait_delay = 30
 63 transaction_isolation = REPEATABLE-READ
 64 innodb_buffer_pool_size = 1024M
 65 innodb_buffer_pool_instances = 8
 66 innodb_buffer_pool_load_at_startup = 1
 67 innodb_buffer_pool_dump_at_shutdown = 1
 68 innodb_data_file_path = ibdata1:1G:autoextend
 69 innodb_flush_log_at_trx_commit = 1
 70 innodb_log_buffer_size = 32M
 71 innodb_log_file_size = 2G
 72 innodb_log_files_in_group = 2
 73 innodb_io_capacity_max = 2000
 74 innodb_io_capacity_max = 4000
 75 innodb_flush_neighbors = 0
 76 innodb_write_io_threads = 8
 77 innodb_read_io_threads = 8
 78 innodb_purge_threads = 4
 79 innodb_page_cleaners = 4
 80 innodb_open_files = 65535
 81 innodb_max_dirty_pages_pct = 50
 82 innodb_flush_method = O_DIRECT
 83 innodb_lru_scan_depth = 4000
 84 innodb_checksum_algorithm = crc32
 85 innodb_lock_wait_timeout = 10
 86 innodb_rollback_on_timeout = 1
 87 innodb_print_all_deadlocks = 1
 88 innodb_file_per_table = 1
 89 innodb_online_alter_log_max_size = 4G
 90 internal_tmp_disk_storage_engine = InnoDB
 91 innodb_stats_on_metadata = 0
 92 innodb_status_file = 1
 93 innodb_status_output = 0
 94 innodb_status_output_locks = 0
 95
 96 #performance_schema
 97
 98 performance_schema = 1
 99 performance_schema_instrument = ‘%=on‘
100
101 #innodb monitor
102 innodb_monitor_enable="module_innodb"
103 innodb_monitor_enable="module_server"
104 innodb_monitor_enable="module_dml"
105 innodb_monitor_enable="module_ddl"
106 innodb_monitor_enable="module_trx"
107 innodb_monitor_enable="module_os"
108 innodb_monitor_enable="module_purge"
109 innodb_monitor_enable="module_log"
110 innodb_monitor_enable="module_lock"
111 innodb_monitor_enable="module_buffer"
112 innodb_monitor_enable="module_index"
113 innodb_monitor_enable="module_ibuf_system"
114 innodb_monitor_enable="module_buffer_page"
115 innodb_monitor_enable="module_adaptive_hash"
116
117 [mysqldump]
118 quick
119 max_allowed_packet = 32M

初始化数据操作:

[[email protected] ~]# mysqld --defaults-file=/etc/my.cnf --basedir=/application/mysql --datadir=/application/data/ --user=mysql --initialize

启动数据库:

[[email protected] ~]# /application/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql &

创建用于主从复制的用户及账户:

[email protected] 14:47:  [(none)]> create user ‘rep‘@‘192.168.112.%‘ identified by ‘rep123‘;
Query OK, 0 rows affected (0.08 sec)

[email protected] 14:51:  [(none)]> grant replication slave on *.* to ‘rep‘@‘192.168.112.%‘;
Query OK, 0 rows affected (0.01 sec)
[email protected] 14:52:  [(none)]> flush privileges;Query OK, 0 rows affected (0.03 sec)
[email protected] 14:52:  [(none)]> 

导出数据库:

mysqldump --single-transaction -uroot -hlocalhost [email protected]123 --master-data=2 -A >all.sql[[email protected] ~]# netstat -lnupt|grep 3306tcp6       0      0 :::3306                 :::*                    LISTEN      5819/mysqld         [[email protected] ~]# 

说明:--single-transaction  如果备份的数据库存储引擎是InnoDB,该参数可以保证数据的一致性,配合RR隔离级别一起使用,当发起事务时,读取一个数据的快照直到备份结束时,都不会读取到本事务开始之后提交的任何数据,--master-data=2,让备份出来的文件中记录备份这一时刻的binlog文件与position号,即记录备份时的binlog文件名和偏移位置

至此,主从复制在主服务器上操作完成,后面在从服务器上操作:

同样部署MySQL服务,然后登陆MySQL,导入all.sql文件,再运行:

CHANGE MASTER TO
MASTER_HOST=‘192.168.112.220‘,
MASTER_USER=‘rep‘,
MASTER_PASSWORD=‘rep123‘,
MASTER_PORT=3306,
MASTER_LOG_FILE=‘mysql-binlog.000003‘,
MASTER_LOG_POS=863;

开启主从复制状态:

[email protected] 14:55:  [(none)]> start slave;

查看主从复制状态:

 1 [email protected] 14:56:  [(none)]> show slave status\G;
 2 *************************** 1. row ***************************
 3                Slave_IO_State: Waiting for master to send event
 4                   Master_Host: 192.168.112.220
 5                   Master_User: bak
 6                   Master_Port: 3306
 7                 Connect_Retry: 60
 8               Master_Log_File: mysql-binlog.000003
 9           Read_Master_Log_Pos: 2274
10                Relay_Log_File: localhost-relay-bin.000002
11                 Relay_Log_Pos: 1734
12         Relay_Master_Log_File: mysql-binlog.000003
13              Slave_IO_Running: Yes
14             Slave_SQL_Running: Yes
15               Replicate_Do_DB:
16           Replicate_Ignore_DB:
17            Replicate_Do_Table:
18        Replicate_Ignore_Table:
19       Replicate_Wild_Do_Table:
20   Replicate_Wild_Ignore_Table:
21                    Last_Errno: 0
22                    Last_Error:
23                  Skip_Counter: 0
24           Exec_Master_Log_Pos: 2274
25               Relay_Log_Space: 1945
26               Until_Condition: None
27                Until_Log_File:
28                 Until_Log_Pos: 0
29            Master_SSL_Allowed: No
30            Master_SSL_CA_File:
31            Master_SSL_CA_Path:
32               Master_SSL_Cert:
33             Master_SSL_Cipher:
34                Master_SSL_Key:
35         Seconds_Behind_Master: 0
36 Master_SSL_Verify_Server_Cert: No
37                 Last_IO_Errno: 0
38                 Last_IO_Error:
39                Last_SQL_Errno: 0
40                Last_SQL_Error:
41   Replicate_Ignore_Server_Ids:
42              Master_Server_Id: 33060220
43                   Master_UUID: cda19536-5749-11e8-8c7a-000c29268dcd
44              Master_Info_File: mysql.slave_master_info
45                     SQL_Delay: 0
46           SQL_Remaining_Delay: NULL
47       Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
48            Master_Retry_Count: 86400
49                   Master_Bind:
50       Last_IO_Error_Timestamp:
51      Last_SQL_Error_Timestamp:
52                Master_SSL_Crl:
53            Master_SSL_Crlpath:
54            Retrieved_Gtid_Set:
55             Executed_Gtid_Set:
56                 Auto_Position: 0
57          Replicate_Rewrite_DB:
58                  Channel_Name:
59            Master_TLS_Version:
60 1 row in set (0.00 sec)
61
62 ERROR:
63 No query specified
64
65 [email protected] 14:56:  [(none)]> 

至此主从复制搭建完成

原文地址:https://www.cnblogs.com/kindnull/p/9040968.html

时间: 2024-10-11 10:50:52

MySQL主从复制(异步模式)的相关文章

mysql主从复制异步半同步实例

建立mysql的复制 node1: mysql> show master status;+------------------+----------+--------------+------------------+-------------------+| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+-----

mysql主从复制--mysql-5.5异步、半同步配置

背景介绍 mysql5.5之前版本,mysql主从复制比较简单 mysql5.6:gtid,multi-thread replication master 1 启用二进制日志 log-bin = master-bin log-bin-index = master-bin.index 2 选择一个唯一的server id server-id = [0~2^32] 3 创建具有复制权限的用户 replication slave,复制的从节点 replication client,联系master,获

MySQL主从复制:半同步、异步

MySQL主从复制:半同步.异步 大纲 前言 如何对MySQL进行扩展? MySQL Replication WorkFlow MySQL主从复制模式 实战演练 MySQL异步复制实现 MySQL半同步复制实现 实验中的思考 总结 前言 本篇我们介绍MySQL Replication的相关内容, 我们首先介绍MySQL CLuster的实现原理和如何一步步构建一个MySQL Replication Cluster 看懂本文需要了解: MySQL基本操作,MySQL日志类型及其作用 如何对MySQ

MySQL 5.5 主从复制异步、半同步以及注意事项详解

大纲 一.前言 二.Mysql 基础知识 三.Mysql 复制(Replication) 四.Mysql 复制(Replication)类型 五.Mysql 主从复制基本步骤 六.Mysql 主从复制(异步) 七.Mysql 主从复制(半同步) 八.Mysql 复制工具 九.Mysql 复制注意事项 十.Mysql 复制过滤 一.前言 从这一篇博客开始我们就来学习mysql的高级课程,在前面的几篇博客我们讲解了mysql基础知识.mysql日志类型.mysql配置文件.mysql备份策略,这一篇

MySQL主从复制(二)

主从架构中:从node是不接受w操作的,否则可能会导致数据不一致. 一.复制架构中应该注意的问题: 1.限制slave为只读模式 可以设置在启动参数中. > show global variables like 'read_only'; 此限制对拥有SUPER权限的用户都无效. 阻止所有用户: mysql> flush tables with read lock; //将阻塞所有w操作. //但是中继日志的重放是可以的,不会被阻塞 2.如何保证主从复制的事务安全? master在执行事务后,应

Mysql之主从模式、读写分离概念

MySQL主从架构复制: 在主server上每次进行可能引起数据变化的操作都要先记录到二进制文件中,并将数据同步到主server的磁盘上,与此同时还要通过端口(3306)将二进制日志发送到从server上,在从server上,从server会先将接受的二进制日志保存为中继日志(relay log),在从中继日志中进行读取,重新执行一遍操作,进行数据的复制 但一般来说,如果主server有多个cpu,当数据量变化很频繁时,可能会在每个cpu上都运行一个事务,而注server只能一个个的记录入二进制

MySQL主从复制的常见拓扑、原理分析以及如何提高主从复制的效率总结

一.主从复制搭建方法参考 1.MySQL5.6 数据库主从(Master/Slave)同步安装与配置详解请参考: http://blog.csdn.net/xlgen157387/article/details/51331244#comments 2.使用mysqlreplicate命令快速搭建 Mysql 主从复制: http://blog.csdn.net/xlgen157387/article/details/52452394 二.Mysql 主从复制的常用拓扑结构 2.1.一主一从 是最

MySQL主从复制原理和实践

mysql主从复制 mysql支持单向 双向 链式级联 实时 异步复制,在复制过程中,一台服务器充当主服务器(Master),而一个或多个其他服务器充当从服务器(Slave) mysql主从复制的应用场景 1.主从服务器互为备份 2.主从服务器读写分离分担网站压力 读写分离 中大型公司:通过程序(php,java) 测试环境:代理软件(mysql-proxy,amoeba) 门户网站:分布式dbproxy(读写分离,hash负载均衡,健康检查) 主从同步实践操作(多实例环境) 1.主库上面设置s

MySql主从复制简单案例实现

mysql的主从复制原理 在mysql主从复制架构中,有一台服务器作为MASTER服务器,该服务器负责所有的读请求和写请求.另外一台或多台作为slave服务器.当master上的某个应用程序发起写请求时,该请求会被内核响应并在内核中执行,然后在将其数据写入到磁盘中去.并且将此次的操作以事件的形式记录到二进制文件中去.此时master上的Binlog  dump thread等待slave上的I/O thread连接请求.一旦slave I/O thread连接上了master的Binlog du