MySQL系列详解七:MySQL双主架构演示-技术流ken
前言
在企业中,数据库高可用一直是企业的重中之重,中小企业很多都是使用mysql主从方案,一主多从,读写分离等,但是单主存在单点故障,从库切换成主库需要作改动。因此,如果是双主或者多主,就会增加mysql入口,增加高可用。不过多主需要考虑自增长ID问题,这个需要特别设置配置文件,比如双主,可以使用奇偶,总之,主之间设置自增长ID相互不冲突就能完美解决自增长ID冲突问题。
MySQL双主(主主)架构方案思路是
1.两台mysql都可读写,互为主备,默认只使用一台(masterA)负责数据的写入,另一台(masterB)备用;
2.masterA是masterB的主库,masterB又是masterA的主库,它们互为主从;
3.两台主库之间做高可用,可以采用keepalived等方案(使用VIP对外提供服务);
4.所有提供服务的从服务器与masterB进行主从同步(双主多从);
5.建议采用高可用策略的时候,masterA或masterB均不因宕机恢复后而抢占VIP(非抢占模式);
演示:MySQL双主架构实现
环境:
主1服务器端IP: 10.220.5.137
主2服务器端IP: 10.220.5.138
1.首先需要修改主服务器端配置文件
#my.cnf
[client]
port =
socket = /tmp/mysql3306.sock [mysql]
#prompt="\\u@\\h:\\p [\\d]>"
#pager="less -i -n -S"
#tee=/opt/mysql/query.log
no-auto-rehash [mysqld]
#misc
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql/mysql3306/data
port =
socket = /tmp/mysql3306.sock
event_scheduler =
#skip-grant tmpdir = /data/mysql/mysql3306/tmp
#timeout
interactive_timeout =
wait_timeout =
#auto_increment_offset =
#auto_increment_increment = #character set
character-set-server = utf8
open_files_limit =
max_connections =
max_connect_errors =
lower_case_table_names =
auto_increment_offset=1 <<从1开始增长
auto_increment_increment=2 <<每次增长为2,即为奇数 #symi replication #rpl_semi_sync_master_enabled=
#rpl_semi_sync_master_timeout= # second
#rpl_semi_sync_slave_enabled= #logs
log-output=file
slow_query_log =
slow_query_log_file = slow.log
log-error = error.log
log_warnings =
pid-file = mysql.pid
long_query_time =
#log-slow-admin-statements =
#log-queries-not-using-indexes =
log-slow-slave-statements = #binlog
#binlog_format = STATEMENT
binlog_format = row
server-id =
log-bin = /data/mysql/mysql3306/logs/mysql-bin
binlog_cache_size = 4M
max_binlog_size = 256M
max_binlog_cache_size = 1M
sync_binlog =
expire_logs_days =
#procedure
log_bin_trust_function_creators= #
gtid-mode = on
enforce-gtid-consistency= #relay log
skip_slave_start =
max_relay_log_size = 128M
relay_log_purge =
relay_log_recovery =
relay-log=relay-bin
relay-log-index=relay-bin.index
log_slave_updates
#slave-skip-errors=,,
#skip-grant-tables #buffers & cache
table_open_cache =
table_definition_cache =
tabl90 max_heap_table_size = 96M
sort_buffer_size = 128K
join_buffer_size = 128K
thread_cache_size =
query_cache_size =
query_cache_type =
query_cache_limit = 256K
query_cache_min_res_unit =
thread_stack = 192K
tmp_table_size = 96M
key_buffer_size = 8M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 32M #myisam
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = #innodb
innodb_buffer_pool_size = 100M
innodb_buffer_pool_instances =
innodb_data_file_path = ibdata1:100M:autoextend
innodb_flush_log_at_trx_commit =
innodb_log_buffer_size = 8M
innodb_log_file_size = 100M
innodb_log_files_in_group =
innodb_max_dirty_pages_pct =
innodb_file_per_table = 1e_open_cache =
innodb_rollback_on_timeout
innodb_status_file =
innodb_io_capacity =
transaction_isolation = READ-COMMITTED
innodb_flush_method = O_DIRECT
2.启动mysql并创建复制用的用户
[root@ken ~]# mysqld & <<启动mysql
[] 2896
[root@ken ~]# mysql -uroot -p <<登录mysql
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , 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> grant replication slave on *.* to ken@'%' identified by 'xx'; <<创建复制用的用户
3.检查主2的二进制日志文件位置,在主2服务器上面执行下面的命令获取到当前二进制日志记录位置
MySQL [ken]> show master status\G
*************************** . row ***************************
File: mysql-bin.000004
Position:
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 987ac782-d7b8-11e8-a462-000c292218ec:-,
c01b1811-d7b3-11e8--000c29492f7b:-
row in set (0.00 sec)
4.连接到主2服务器上面
change master to master_host='10.220.5.138',master_user='ken',master_password='xx',master_log_file='mysql-bin.000004',master_log_pos=1558;
5.启动slave
mysql> start slave;
6.检查连接状态
mysql> show slave status\G
ERROR (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:
Current database: *** NONE *** *************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.220.5.138
Master_User: ken
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
Slave_IO_Running: Yes <<IO线程启动成功
Slave_SQL_Running: Yes <<SQL线程启动成功
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: c01b1811-d7b3-11e8--000c29492f7b
Master_Info_File: /data/mysql/mysql3306/data/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: c01b1811-d7b3-11e8--000c29492f7b:
Executed_Gtid_Set: 987ac782-d7b8-11e8-a462-000c292218ec:-,
c01b1811-d7b3-11e8--000c29492f7b:
Auto_Position:
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
row in set (0.01 sec)
主1节点配置完毕,并且启动成功,下面来配置主2服务器。
1.修改主2配置文件
#my.cnf
[client]
port =
socket = /tmp/mysql3306.sock [mysql]
#prompt="\\u@\\h:\\p [\\d]>"
#pager="less -i -n -S"
#tee=/opt/mysql/query.log
no-auto-rehash [mysqld]
#misc
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql/mysql3306/data
port =
socket = /tmp/mysql3306.sock
event_scheduler =
#skip-grant tmpdir = /data/mysql/mysql3306/tmp
#timeout
interactive_timeout =
wait_timeout =
#auto_increment_offset =
#auto_increment_increment = #character set
character-set-server = utf8
open_files_limit =
max_connections =
max_connect_errors =
lower_case_table_names =
auto_increment_offset=2 <<从2开始增长
auto_increment_increment= <<每次增长为2,即为偶数 #symi replication #rpl_semi_sync_master_enabled=
#rpl_semi_sync_master_timeout= # second
#rpl_semi_sync_slave_enabled= #logs
log-output=file
slow_query_log =
slow_query_log_file = slow.log
log-error = error.log
log_warnings =
pid-file = mysql.pid
long_query_time =
#log-slow-admin-statements =
#log-queries-not-using-indexes =
log-slow-slave-statements = #binlog
#binlog_format = STATEMENT
binlog_format = row
server-id = 1003307 <<两个服务器的server_id必须不一致
log-bin = /data/mysql/mysql3306/logs/mysql-bin
binlog_cache_size = 4M
max_binlog_size = 256M
max_binlog_cache_size = 1M
sync_binlog =
expire_logs_days =
#procedure
log_bin_trust_function_creators= #
gtid-mode = on
enforce-gtid-consistency= #relay log
skip_slave_start =
max_relay_log_size = 128M
relay_log_purge =
relay_log_recovery =
relay-log=relay-bin
relay-log-index=relay-bin.index
log_slave_updates
#slave-skip-errors=,,
#skip-grant-tables #buffers & cache
table_open_cache =
table_definition_cache =
tabl90 max_heap_table_size = 96M
sort_buffer_size = 128K
join_buffer_size = 128K
thread_cache_size =
query_cache_size =
query_cache_type =
query_cache_limit = 256K
query_cache_min_res_unit =
thread_stack = 192K
tmp_table_size = 96M
key_buffer_size = 8M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 32M #myisam
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = #innodb
innodb_buffer_pool_size = 100M
innodb_buffer_pool_instances =
innodb_data_file_path = ibdata1:100M:autoextend
innodb_flush_log_at_trx_commit =
innodb_log_buffer_size = 8M
innodb_log_file_size = 100M
innodb_log_files_in_group =
innodb_max_dirty_pages_pct =
innodb_file_per_table = 1e_open_cache =
innodb_rollback_on_timeout
innodb_status_file =
innodb_io_capacity =
transaction_isolation = READ-COMMITTED
innodb_flush_method = O_DIRECT
2.启动mysql并创建复制用的用户
[root@ken ~]# mysqld & <<启动mysql
[4] 2896
[root@ken ~]# mysql -uroot -p <<登录mysql
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.23-log MySQL Community Server (GPL) Copyright (c) 2000, 2018, 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> grant replication slave on *.* to ken@'%' identified by 'xx'; <<创建复制用的用户
3.检查主1的二进制日志文件位置,在主1服务器上面执行下面的命令获取到当前二进制日志记录位置
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000009
Position: 1526
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 987ac782-d7b8-11e8-a462-000c292218ec:1-5,
c01b1811-d7b3-11e8-8698-000c29492f7b:3
1 row in set (0.01 sec)
4.连接到主2服务器上面
change master to master_host='10.220.5.137',master_user='ken',master_password='xx',master_log_file='mysql-bin.000009',master_log_pos=1526;
5.启动slave
mysql> start slave;
6.检查连接状态
mysql> show slave status\G
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 8
Current database: *** NONE *** *************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.220.5.137
Master_User: ken
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 1558
Relay_Log_File: relay-bin.000009
Relay_Log_Pos: 602
Relay_Master_Log_File: mysql-bin.000009
Slave_IO_Running: Yes <<IO线程启动成功
Slave_SQL_Running: Yes <<SQL线程启动成功
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: 1558
Relay_Log_Space: 803
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: 1003307
Master_UUID: c01b1811-d7b3-11e8-8698-000c29492f7b
Master_Info_File: /data/mysql/mysql3306/data/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: c01b1811-d7b3-11e8-8698-000c29492f7b:3
Executed_Gtid_Set: 987ac782-d7b8-11e8-a462-000c292218ec:1-5,
c01b1811-d7b3-11e8-8698-000c29492f7b:3
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.01 sec)
现在两个主服务器都已经搭建完,现在来进行测试
1.首先在主1服务器上面建表建库并插入数据
mysql> create database ken; <<创建数据库
mysql> use ken; <<切换数据库
mysql> create table tb2(id int primary key auto_increment,name char()); <<创建表
mysql> insert into tb2 (name) values('kk'),('kkl'),('dad'); <<插入数据
mysql> select * from tb2; <<检查数据,为奇数排列
+----+------+
| id | name |
+----+------+
| | kk |
| | kkl |
| | dad |
2.登录主2服务器并检查是否已经有主1上面建的表库
MySQL [ken]> select * from tb2; <<同步成功
+----+------+
| id | name |
+----+------+
| | kk |
| | kkl |
| | dad |
3.在主2服务器上面插入数据,检查数据是否会冲突以及同步到主1
MySQL [ken]> insert into tb2 (name) values('lll'),('lll'),('lll');
MySQL [ken]> select * from tb2;
+----+------+
| id | name |
+----+------+
| | kk |
| | kkl |
| | dad |
| | lll | <<偶数排序
| | lll |
| | lll |
+----+------+
rows in set (0.00 sec)
4.检查主1服务器是否已经同步过来
mysql> select * from tb2;
+----+------+
| id | name |
+----+------+
| | kk |
| | kkl |
| | dad |
| | lll |
| | lll |
| | lll |
+----+------+
rows in set (0.00 sec)
同步成功!这样就实现了mysql双主的架构.
MySQL系列详解七:MySQL双主架构演示-技术流ken的更多相关文章
- MySQL系列详解八:MySQL多线程复制演示-技术流ken
前言 Mysql 采用多线程进行复制是从 Mysql 5.6 开始支持的内容,但是 5.6 版本下有缺陷,虽然支持多线程,但是每个数据库只能一个线程,也就是说如果我们只有一个数据库,则主从复制时也只有 ...
- MySQL系列详解六:MySQL主从复制/半同步演示-技术流ken
前言 随着技术的发展,在实际的生产环境中,由单台MySQL数据库服务器不能满足实际的需求.此时数据库集群就很好的解决了这个问题了.采用MySQL分布式集群,能够搭建一个高并发.负载均衡的集群服务器.在 ...
- MySQL系列详解十:MySQL多源复制演示-技术流ken
前言 多源复制即多主一从结构,多个主服务器端的数据都会同步到后端一个从服务器上面.至于为什么要做多源复制下面的总结很到位. 1.灾备作用:将各个库汇总在一起,就算是其他库都挂了(整个机房都无法连接了) ...
- MySQL系列详解五: xtrabackup实现完全备份及增量备份详解-技术流ken
xtrabackup简介 xtrabackup是一个用来对mysql做备份的工具,它可以对innodb引擎的数据库做热备.xtrabackup备份和还原速度快,备份操作不会中断正在执行的事务,备份完成 ...
- MySQL系列详解三:MySQL中各类日志详解-技术流ken
前言 日志文件记录了MySQL数据库的各种类型的活动,MySQL数据库中常见的日志文件有 查询日志,慢查询日志,错误日志,二进制日志,中继日志 .下面分别对他们进行介绍. 查询日志 1.查看查询日志变 ...
- MySQL系列详解一:MySQL&&多实例安装-技术流ken
简介 MySQL是一个真正的多用户.多线程SQL数据库服务器.SQL(结构化查询语言)是世界上最流行的和标准化的数据库语言,它使得存储.更新和存取信息更加容易.MySQL是一个客户机/服务器结构的实现 ...
- MySQL系列详解九:MySQL级联复制演示-技术流ken
前言 级联复制就是master服务器,只给后端一台slave服务器同步数据,然后这个slave服务器在向后端的所有slave服务器同步数据,这样就可以降低master服务器的写压力,和复制数据的网络I ...
- MySQL系列详解二:MySQL语句操作-技术流ken
简介 本篇博客将详细讲解mysql的一些常用sql语句操作,例如创建数据库,删除数据库,创建表,修改表,删除表,以及简单查询案例. 关于mysql数据中的SQL的大小写问题 1.不区分大小写 1. s ...
- MySQL系列详解四:MySQL事务-技术流ken
MySQL 事务 MySQL 事务主要用于处理操作量大,复杂度高的数据.比如说,在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数 ...
随机推荐
- MIPS汇编指令集
MIPS有三种指令格式: R型 6 5 5 5 5 6 op rs rt rd shamt funct 功能:寄存器-寄存器ALU操作 (算术运算,逻辑运算) I型 6 5 5 16 op rs rt ...
- 谈一款MOBA类游戏《码神联盟》的服务端架构设计与实现(更新优化思路)
注:本文仅用于在博客园学习分享,还在随着项目不断更新和完善中,多有不足,暂谢绝各平台或个人的转载和推广,感谢支持. 一.前言 <码神联盟>是一款为技术人做的开源情怀游戏,每一种编程语言都是 ...
- App设计:消息推送和界面路由跳转
概要 app消息推送.显示通知栏,点击跳转页面是很一般的功能了,下面以个推为例演示push集成,消息处理模块及app内部路由模块的简单设计. 推送 推送sdk集成 集成sdk步骤根据文档一步步做就行了 ...
- 【渗透攻防Web篇】SQL注入攻击高级
前言 前面我们学习了如何寻找,确认,利用SQL注入漏洞的技术,本篇文章我将介绍一些更高级的技术,避开过滤,绕开防御.有攻必有防,当然还要来探讨一下SQL注入防御技巧. 目录 第五节 避开过滤方法总结 ...
- IDEA环境下GIT操作浅析之一Idea下仓库初始化与文件提交涉及到的基本命令
目标总括 idea 下通过命令操作文件提交,删除,与更新并推送到github 开源库基本操作idea 下通过命令实现分支的创建与合并操作 idea 下通过图形化方式实现idea 项目版本控制基本操作 ...
- laravel migrate时报错:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
今天在学习laravel的路由模型绑定时,在按照文档执行php artisan migrate时报错. In Connection.php line 664: SQLSTATE[42000]: Syn ...
- Windows 系统中的 CMD 黑窗口简单介绍
简介 DOS是磁盘操作系统的缩写,是个人计算机上的一类操作系统DOS命令,是DOS操作系统的命令,是一种面向磁盘的操作命令,主要包括目录操作类命令.磁盘操作类命令.文件操作类命令和其它命令.DOS系统 ...
- [转载]PHP中PSR-[0-4]规范
PHP是世界上最伟大的语言,这一点是毋庸置疑的吧.哈哈哈哈哈哈 .这个霸气的开头不错!(^__^) 但是正是因为伟大,所以用的人也就多了,人一多,再牛逼再伟大的东西,都会产生问题,逐渐就造成了很多 ...
- 11.Django2.0文档
第四章 模板 1.标签 (1)if/else {% if %} 标签检查(evaluate)一个变量,如果这个变量为真(即,变量存在,非空,不是布尔值假),系统会显示在 {% if %} 和 {% e ...
- maven发布jar包到nexus
在编码过程中,有些通用的代码模块,有时候我们不想通过复制拷贝来粗暴地复用,因为这样不仅体现不了变化,也不利于统一管理.这里我们使用maven deploy的方式,将通用的模块打成jar包,发布到nex ...