MySQL Scale out
简介
MySQL复制中较常见的复制架构有“一主一从”、“一主多从”、“双主”、“多级复制”和“多主环形机构”等,见下图;
最常用,也最灵活的就要数“一主多从”复制架构了,其能满足多种需求,如:
为不同的角色使用不同的备库(例如添加不同的索引或使用不同的存储引擎);
把一台备库当做待用的主库,除了复制没有其它数据传输;
将一台备库放在远程数据中心,用作灾难恢复;
延迟一个或多个备库,以备灾难恢复;
使用其中一个备库,作为备份、培训、开发或者测试使用服务器;
而“双主”复制架构则用于特殊的场景下,如两个处于不同地理位置的办公室,且都需要一份可写的数据拷贝;
这种架构最大的问题是如何解决数据冲突和不一致,尤其当两台服务器同时修改同一行记录,或同时在两台服务器上向一个包含auto_increment列的表里插入数据时;
而通过将一台服务器设置为只读的被动服务器,则可以很好的避免数据写入冲突的问题,这种主动-被动模式下的主-主复制架构使得反复切换主动和被动服务器非常方便,可以实现在不关闭服务器的情况下执行维护、优化表、升级操作系统或其他任务;
配置主动-被动模式的主-主复制架构的一般流程:
确保两台服务器上有相同的数据;
启用二进制日志,选择唯一的服务器ID,并创建复制账号;
启用备库更新的日志记录,这是故障转移和故障恢复的关键;
把被动服务器配置成只读,防止可能与主动服务器上的更新产生冲突;
启动每个服务器的MySQL实例;
将每个主库设置为对方的备库,使用新创建的二进制日志开始工作;
同时为了消除不同地理位置的站点单点故障问题,可以为每个主库增加冗余,即为每一个主库增加一个从库;
而MMM(=Master-Master Replication Manager for MySQL)则是一套脚本集合,用以监控、管理双主复制架构,通过设置一个可写的VIP和多个只读的VIP,完成故障自动转移、读负载分摊等功能;
架构设计
服务器规划
虚IP规划
配置部署
双主复制架构部署
MySQL或MariaDB的安装初始化可详见博客“MySQL架构”
利用mysqld_multi在一台主机上启动多个mysqld实例
数据库初始化
# 在主机Host1和Host2上cd /usr/local/mysqlscripts/mysql_install_db --user=mysql --datadir=/data/mariadb_data_3406/scripts/mysql_install_db --user=mysql --datadir=/data/mariadb_data_3506/
数据库配置
# 在主机Host1上vi /etc/my.cnf[mysqld_multi]mysqld = /usr/local/mysql/bin/mysqld_safemysqladmin = /usr/local/mysql/bin/mysqladmin[mysqld1]port = 3406socket = /tmp/mysql3406.sockskip-external-lockingkey_buffer_size = 256Mmax_allowed_packet = 1Mtable_open_cache = 256sort_buffer_size = 1Mread_buffer_size = 1Mread_rnd_buffer_size = 4Mmyisam_sort_buffer_size = 64Mthread_cache_size = 8query_cache_size= 16Mthread_concurrency = 2datadir = /data/mariadb_data_3406innodb_file_per_table = 1default_storage_engine = InnoDBlog-bin=mysql-binrelay-log=/data/relaylogs_3406/relay-bin # 指定中继日志路径log_slave_updates=1 # 开启从库更新操作写入二进制日志功能auto_increment_increment=2 # 双主复制中自增长字段的步长auto_increment_offset=1 # 双主复制中自增长字段的起始值,此为1sync_binlog = 1 # 可保证事务日志及时写入磁盘文件binlog_format=rowserver-id = 11# 注意server-id的唯一性[mysqld2]port = 3506socket = /tmp/mysql3506.sockskip-external-lockingkey_buffer_size = 256Mmax_allowed_packet = 1Mtable_open_cache = 256sort_buffer_size = 1Mread_buffer_size = 1Mread_rnd_buffer_size = 4Mmyisam_sort_buffer_size = 64Mthread_cache_size = 8query_cache_size= 16Mthread_concurrency = 2datadir = /data/mariadb_data_3506innodb_file_per_table = 1default_storage_engine = InnoDBlog-bin=mysql-binrelay-log=/data/relaylogs_3506/relay-binlog_slave_updates=1sync_binlog = 1binlog_format=rowserver-id = 12# 在主机Host2上vi /etc/my.cnf[mysqld_multi]mysqld = /usr/local/mysql/bin/mysqld_safemysqladmin = /usr/local/mysql/bin/mysqladmin[mysqld1]port = 3406socket = /tmp/mysql3406.sockskip-external-lockingkey_buffer_size = 256Mmax_allowed_packet = 1Mtable_open_cache = 256sort_buffer_size = 1Mread_buffer_size = 1Mread_rnd_buffer_size = 4Mmyisam_sort_buffer_size = 64Mthread_cache_size = 8query_cache_size= 16Mthread_concurrency = 2datadir = /data/mariadb_data_3406innodb_file_per_table = 1default_storage_engine = InnoDBlog-bin=mysql-binrelay-log=/data/relaylogs_3406/relay-binlog_slave_updates=1auto_increment_increment=2 # # 双主复制中自增长字段的步长auto_increment_offset=2 # 双主复制中自增长字段的起始值,此为2sync_binlog = 1binlog_format=rowserver-id = 21[mysqld2]port = 3506socket = /tmp/mysql3506.sockskip-external-lockingkey_buffer_size = 256Mmax_allowed_packet = 1Mtable_open_cache = 256sort_buffer_size = 1Mread_buffer_size = 1Mread_rnd_buffer_size = 4Mmyisam_sort_buffer_size = 64Mthread_cache_size = 8query_cache_size= 16Mthread_concurrency = 2datadir = /data/mariadb_data_3506innodb_file_per_table = 1default_storage_engine = InnoDBlog-bin=mysql-binrelay-log=/data/relaylogs_3506/relay-binlog_slave_updates=1sync_binlog = 1binlog_format=rowserver-id = 22
启动数据库实例
# 在主机Host1和Host2上/etc/init.d/mysqld_multi start 1 # 停止服务操作是/etc/init.d/mysqld_multi stop 1/etc/init.d/mysqld_multi start 2 # 停止服务操作是/etc/init.d/mysqld_multi stop 2
登录数据库
# 在主机Host1和Host2上mysql -S /tmp/mysql3406.sock # 登录master1或master2mysql -S /tmp/mysql3506.sock # 登录slave1或slave2
创建所需账户(在Master1实例上)
grant replication client on *.* to '3m_moni'@'192.168.0.%' identified by '3m_12345'; # 创建MMM的监控账户grant super,replication client,process on *.* to '3m_agen'@'192.168.0.%' identified by '3m_12345'; # 创建MMM的代理账户grant replication slave on *.* to '3m_repl'@'192.168.0.%' identified by '3m_12345'; # 创建复制账户
配置数据同步
# 每次从库连接主库前,需先查询对应主库的二进制日志文件及其事件位置,即在主库上执行show master status即可,据此决定从库连接时的master_log_file和master_log_pos参数;# slave1实例上change master to master_host='192.168.0.45',master_port=3406,master_user='3m_repl',master_password='3m_12345',master_log_file='mysql-bin.000001',master_log_pos=2448;# master2实例上change master to master_host='192.168.0.45',master_port=3406,master_user='3m_repl',master_password='3m_12345',master_log_file='mysql-bin.000002',master_log_pos=365;# slave2实例上change master to master_host='192.168.0.46',master_port=3406,master_user='3m_repl',master_password='3m_12345',master_log_file='mysql-bin.000004',master_log_pos=342;# master1实例上change master to master_host='192.168.0.46',master_port=3406,master_user='3m_repl',master_password='3m_12345',master_log_file='mysql-bin.000004',master_log_pos=342;
查看同步状态
# 重点检查Slave_IO_Running、Slave_SQL_Running和Master_Server_Id等参数MariaDB [(none)]> show slave status/G*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.45 Master_User: 3m_repl Master_Port: 3406 Connect_Retry: 60 Master_Log_File: mysql-bin.000005 Read_Master_Log_Pos: 326 Relay_Log_File: relay-bin.000010 Relay_Log_Pos: 613 Relay_Master_Log_File: mysql-bin.000005 Slave_IO_Running: Yes Slave_SQL_Running: Yes Master_Server_Id: 11
MMM安装部署
Host1主机上:部署agent和monitor
yum -y install mysql-mmm-*# 配置公共设置vi /etc/mysql-mmm/mmm_common.confactive_master_role writer<host default> cluster_interface eth0 pid_path /var/run/mysql-mmm/mmm_agentd.pid bin_path /usr/libexec/mysql-mmm/ replication_user 3m_repl# 复制账户 replication_password 3m_12345# 复制账户密码 agent_user 3m_agen# agent账户 agent_password 3m_12345# agent账户密码</host><host db1> ip 192.168.0.45 mysql_port 3406# 可指定需连接的mysqld的端口 mode master peer db2# peer表示db1、db2是同等级别的</host><host db2> ip 192.168.0.46 mysql_port 3406 mode master peer db1</host><host db3> ip 192.168.0.45 mysql_port 3506 mode slave</host><host db4> ip 192.168.0.46 mysql_port 3506 mode slave</host><role writer> hosts db1, db2 ips 192.168.0.11# 可写VIP只配置一个 mode exclusive# 表示排它</role><role reader> hosts db1, db2,db3,db4 ips 192.168.0.12,192.168.0.13,192.168.0.14,192.168.0.15 # 只读VIP可配置多个 mode balanced# 表示可以共用</role>==========scp mmm_common.conf 192.168.0.46:/etc/mysql-mmm/ # 将公共配置文件拷贝至其它主机==========# 配置监控设置vi /etc/mysql-mmm/mmm_mon.confinclude mmm_common.conf<monitor> ip 127.0.0.1 pid_path /var/run/mysql-mmm/mmm_mond.pid bin_path /usr/libexec/mysql-mmm status_path /var/lib/mysql-mmm/mmm_mond.status ping_ips 192.168.0.45,192.168.0.46# 健康监测时需ping的主机IP,不是VIP哦 auto_set_online 60</monitor><host default> monitor_user 3m_moni# 监控账户 monitor_password 3m_12345 # 监控账户密码</host>debug 0# 配置agent设置vi /etc/mysql-mmm/mmm_agent.confinclude mmm_common.confthis db1# 因为在一台主机上启用了2个mysqld实例,故可配置2个this参数哦this db3
Host2主机上:只需部署agent
yum -y install mysql-mmm-agent# 配置agent设置vi /etc/mysql-mmm/mmm_agent.confinclude mmm_common.confthis db2this db4
服务启动
# 在主机Host1上[root@mysql mysql-mmm]# mmm_control showdb1(192.168.0.45) master/ONLINE. Roles: reader(192.168.0.14), writer(192.168.0.11)db2(192.168.0.46) master/ONLINE. Roles: reader(192.168.0.13)db3(192.168.0.45) slave/ONLINE. Roles: reader(192.168.0.15)db4(192.168.0.46) slave/ONLINE. Roles: reader(192.168.0.12)
测试验证
查看双主复制架构中基于MMM实现的状态信息:
# 在主机Host1上[root@mysql mysql-mmm]# mmm_control showdb1(192.168.0.45) master/ONLINE. Roles: reader(192.168.0.14), writer(192.168.0.11)db2(192.168.0.46) master/ONLINE. Roles: reader(192.168.0.13)db3(192.168.0.45) slave/ONLINE. Roles: reader(192.168.0.15)db4(192.168.0.46) slave/ONLINE. Roles: reader(192.168.0.12)
手动进行各节点的健康监测
# 在主机Host1上[root@mysql mysql-mmm]# mmm_control checksdb4ping [last change: 2014/05/06 22:38:27]OKdb4mysql[last change: 2014/05/06 22:38:27]OKdb4rep_threads[last change: 2014/05/06 22:38:27]OKdb4rep_backlog[last change: 2014/05/06 22:38:27]OK: Backlog is nulldb2ping [last change: 2014/05/06 22:38:27]OKdb2mysql[last change: 2014/05/06 22:38:27]OKdb2rep_threads[last change: 2014/05/06 22:38:27]OKdb2rep_backlog[last change: 2014/05/06 22:38:27]OK: Backlog is nulldb3ping [last change: 2014/05/06 22:38:27]OKdb3mysql[last change: 2014/05/06 22:38:27]OKdb3rep_threads[last change: 2014/05/06 22:38:27]OKdb3rep_backlog[last change: 2014/05/06 22:38:27]OK: Backlog is nulldb1ping [last change: 2014/05/06 22:38:27]OKdb1mysql[last change: 2014/05/06 22:38:27]OKdb1rep_threads[last change: 2014/05/06 22:38:27]OKdb1rep_backlog[last change: 2014/05/06 22:38:27]OK: Backlog is null
补充说明
在本篇的演示案例中,前端程序若要与MySQL通信,则写库需连接192.168.0.11:3406,读库可连接192.168.0.12-15中的一个或多个,端口可能是3406或3506;
在只读VIP漂移时,会导致前端程序连接的mysqld端口发生变化,所以生产环境下还是统一使用3306端口为宜;
利用MMM实现了双主复制架构中的故障自动转移后,mysql并非直接与前端程序通信,还需配合使用读写分离器(如Ameoba),以统一对外的连接地址,由读写分离器负责读写的向下分配;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Blue Wings Chaos Effect features a diverse cast of characters, each with a unique identity and backstory. For this reason, the editor has specially compiled an introduction to the characters of BlazBlue Chaos Effect for all players. How many characters are there in BlazBlue Chaos Effect? Answer: There are 7 characters. 1. [God of Death] Ragnar Chad Bradedge (nicknamed RG, Nissan), his brother and sister were raised by church nuns. One day, one of the six heroes of the villain broke in, killed the nuns, and burned them down. Church, took his sister away, leaving behind his younger brother "The Weapon of Things" Ice Sword Snow Girl. 2. Noel Vermillion The adopted daughter of the Vermillion family looks almost the same as Ragnar's sister. After graduation, he joined the governing body as secretary to Ragnar's younger brother. 3. λ-11 is collectively known as Lambda and Eleventh Sister. After the original developer gave up, Kokonoe rescued and

In this article we will introduce you to the best alternatives to CrushOn.AI with free and unlimited messaging capabilities. There are many artificial intelligence platforms on the market now that allow users to talk to characters from various media such as animation, which provides users with a more interesting and interactive experience. What is CrushOn.AI? CrushonAI is an AI chatbot platform that allows users to experience the fun of interaction by having conversations with virtual characters. Users have the opportunity to communicate with, build connections with, and create storylines related to their favorite characters across a variety of media including anime. The best alternative to CrushOn.AI that offers unlimited free messages If you are looking for the best Crush

Anchor Arrival is a 3D turn-based card game with a high-definition beautiful girl two-dimensional theme. It provides a rich and exciting combination of characters for players to explore and experience. It has many powerful combinations of high-quality lineups. New players are also curious novices. What powerful characters are recommended in the pool? Let’s take a look at the selection reference for novices to win ten consecutive golds! Anchor Point Advent is a powerful character recommendation for novice pools. The first ten-consecutive pick is Alice. She is mainly a single-target lightning-type burst character. The output is very explosive, and the experience will be very friendly to newcomers, so it is highly recommended to choose it. It is recommended to choose the combination of "Alice" + "Antelope" for 10 points. Alice is the most worthy character to output the goldpire attribute, and is not even a bit stronger than the other two characters in the novice card pool. Alice can pass special

PHP is a widely used programming language that is widely used to create and develop various web applications. In many web applications, the role permission management system is an important feature to ensure that different users have appropriate access rights. This article will introduce how to use PHP to implement a simple and practical role permission management system. The basic concept of the role permission management system is to divide users into different roles and assign corresponding permissions to each role. In this way, users can only perform operations they have permission to perform, thus ensuring the system's

There are many characters for players to choose from in the game "Blank Wing: Chaos Effect". Many players want to know which characters are recommended in "Blank Wing: Chaos Effect". The editor recommends that you choose NO-11, White Face, and Noelle. Next Next, the editor will give you a detailed introduction to the character recommendation strategy of "Blank Wing: Chaos Effect". Interested players can come and take a look with the editor! "Blank Wing: Chaos Effect" character recommendation: 1. NO -11 Character Analysis 1. NO-11 has high damage and good mobility. 2. The difficulty of getting started is average and suitable for novice players. 3. The exclusive module recommends strengthening jump a, skills and long-press skill secrets. 2. White-faced character analysis 1. It is difficult to get started and is suitable for players with certain operations. 2. The character has strong survivability and can be bounced

After the second beta test of "Zhu Xian World" ended, players' evaluations were polarized. After the second test, the official also listed a tuning list to make adjustments in five aspects: economy, development, gameplay, functions, and art. It means that corresponding optimization measures will be made in response to problems reported by players. Today, "Zhu Xian World" released a demonstration of the progress of character medical beauty in the explosive reform plan. Through screen comparison, we will show you the phased results of the project team's radical character changes in the past few months. Which version do you think looks better? The following is the exposure plan previously announced by "Zhu Xian World":

Who are the powerful characters in Eternal Night’s Coming and Resurrection? A strong character in the game will bring players a comfortable experience, so what are they in Eternal Night's Resurrection? Next, the editor will share the summary of the powerful characters in Eternal Night's Coming and Resurrection below. If you need it, you can take a look. List of powerful characters and character strength list in "Eternal Night: Resurrection" T0: Aqua, Pochi T1: Little Eve, Elaine, Fini T2: Wheat 2, the strongest character recommendation (1) Aqua Character Analysis 1. Good at using various weapons for output, with low difficulty to obtain and high damage. 2. In addition to high damage, it also has high blood volume and can add a shield to yourself. 3. Every time you attack an enemy target, add a mark to the enemy to increase your own damage. (2) Little Eve character

CakePHP is a popular PHP development framework that provides a comprehensive permission control mechanism, namely AccessControlList (ACL). Using ACLs can help you control the access rights of each user in your application. In this article, we will cover how to use ACL roles in CakePHP. Configuring the ACL component First, we need to configure the ACL component in CakePHP. Add the following code in app_controller.php:
