本文主要给大家简单讲讲设计MySQL双主架构方案的思路是什么,相关专业术语大家可以上网查查或者找一些相关书籍补充一下,这里就不涉猎了,我们就直奔主题吧,希望设计MySQL双主架构方案的思路是什么这篇文章可以给大家带来一些实际帮助。
创新互联建站是专业的德惠网站建设公司,德惠接单;提供成都网站制作、成都网站建设、外贸营销网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行德惠网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
在开始之前,我们先来了解主从同步复制原理。
复制分成三步:
下图描述了这一过程:
该过程的第一部分就是master记录二进制日志。在每个事务更新数据完成之前,master在二日志记录这些改变。MySQL将事务串行的写入二进制日志,即使事务中的语句都是交叉执行的。在事件写入二进制日志完成后,master通知存储引擎提交事务。
下一步就是slave将master的binary log拷贝到它自己的中继日志。首先,slave开始一个工作线程——I/O线程。I/O线程在master上打开一个普通的连接,然后开始binlog dump process。Binlog dump process从master的二进制日志中读取事件,如果已经跟上master,它会睡眠并等待master产生新的事件。I/O线程将这些事件写入中继日志。
SQL slave thread处理该过程的最后一步。SQL线程从中继日志读取事件,更新slave的数据,使其与master中的数据一致。只要该线程与I/O线程保持一致,中继日志通常会位于OS的缓存中,所以中继日志的开销很小。
此外,在master中也有一个工作线程:和其它MySQL的连接一样,slave在master中打开一个连接也会使得master开始一个线程。
MySQL5.6以前的版本复制过程有一个很重要的限制——复制在slave上是串行化的,也就是说master上的并行更新操作不能在slave上并行操作。 MySQL5.6版本参数slave-parallel-workers=1 表示启用多线程功能。
MySQL5.6开始,增加了一个新特性,是加入了全局事务 ID (GTID) 来强化数据库的主备一致性,故障恢复,以及容错能力。
官方文档:http://dev.mysql.com/doc/refman/5.6/en/replication-gtids.html
这样做可以在一定程度上保证主库的高可用,在一台主库down掉之后,可以在极短的时间内切换到另一台主库上(尽可能减少主库宕机对业务造成的影响),减少了主从同步给线上主库带来的压力;
但是也有几个不足的地方:
架构的简易图如下:
1.CentOS 6.8 64位 2台:masterA(192.168.10.11),masterB(192.168.10.12)
2.官方Mysql5.6版本
yum -y install make gcc gcc-c++ ncurses-devel bison openssl-devel
groupadd -g 27 mysql adduser -u 27 -g mysql -s /sbin/nologin mysql
mkdir -p /data/packages/src cd /data/packages/wget http://distfiles.macports.org/cmake/cmake-3.2.3.tar.gzwget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.34.tar.gz
mkdir -p /usr/local/mysql/data
cd /data/packages/srctar -zxvf ../cmake-3.2.3.tar.gz cd cmake-3.2.3/./bootstrap gmakemake install
cd ../tar xf mysql-5.6.34.tar.gz cd mysql-5.6.34cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc \-DWITH_SSL=bundled -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci \-DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 \-DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \-DMYSQL_DATADIR=/usr/local/mysql/datamake && make install
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[client] port = 3306socket = /tmp/mysql.sock [mysqld] basedir = /usr/local/mysql port = 3306socket = /tmp/mysql.sock datadir = /usr/local/mysql/data pid-file = /usr/local/mysql/data/mysql.pid log-error = /usr/local/mysql/data/mysql.err server-id = 1auto_increment_offset = 1auto_increment_increment = 2 #奇数ID log-bin = mysql-bin #打开二进制功能,MASTER主服务器必须打开此项 binlog-format=ROW binlog-row-p_w_picpath=minimal log-slave-updates=truegtid-mode=on enforce-gtid-consistency=truemaster-info-repository=TABLE relay-log-info-repository=TABLEsync-master-info=1slave-parallel-workers=0sync_binlog=0binlog-checksum=CRC32 master-verify-checksum=1slave-sql-verify-checksum=1binlog-rows-query-log_events=1#expire_logs_days=5max_binlog_size=1024M #binlog单文件最大值 replicate-ignore-db = mysql #忽略不同步主从的数据库 replicate-ignore-db = information_schema replicate-ignore-db = performance_schema replicate-ignore-db = test replicate-ignore-db = zabbix max_connections = 3000max_connect_errors = 30skip-character-set-client-handshake #忽略应用程序想要设置的其他字符集 init-connect='SET NAMES utf8' #连接时执行的SQL character-set-server=utf8 #服务端默认字符集 wait_timeout=1800 #请求的最大连接时间 interactive_timeout=1800 #和上一参数同时修改才会生效 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES #sql模式 max_allowed_packet = 10M bulk_insert_buffer_size = 8M query_cache_type = 1query_cache_size = 128M query_cache_limit = 4M key_buffer_size = 256M read_buffer_size = 16K skip-name-resolve slow_query_log=1long_query_time = 6slow_query_log_file=slow-query.log innodb_flush_log_at_trx_commit = 2innodb_log_buffer_size = 16M [mysql] no-auto-rehash [myisamchk] key_buffer_size = 20M sort_buffer_size = 20M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout [mysqldump] quick max_allowed_packet = 16M [mysqld_safe]
log-slave-updates = true #将复制事件写入binlog,一台云服务器既做主库又做从库此选项必须要开启
#masterA自增长ID auto_increment_offset = 1auto_increment_increment = 2 #奇数ID
#masterB自增加ID auto_increment_offset = 2auto_increment_increment = 2 #偶数ID
[client] port = 3306socket = /tmp/mysql.sock [mysqld] basedir = /usr/local/mysql port = 3306socket = /tmp/mysql.sock datadir = /usr/local/mysql/data pid-file = /usr/local/mysql/data/mysql.pid log-error = /usr/local/mysql/data/mysql.err server-id = 2auto_increment_offset = 2auto_increment_increment = 2 #偶数ID log-bin = mysql-bin #打开二进制功能,MASTER主服务器必须打开此项 binlog-format=ROW binlog-row-p_w_picpath=minimal log-slave-updates=truegtid-mode=on enforce-gtid-consistency=truemaster-info-repository=TABLE relay-log-info-repository=TABLEsync-master-info=1slave-parallel-workers=0sync_binlog=0binlog-checksum=CRC32 master-verify-checksum=1slave-sql-verify-checksum=1binlog-rows-query-log_events=1#expire_logs_days=5max_binlog_size=1024M #binlog单文件最大值 replicate-ignore-db = mysql #忽略不同步主从的数据库 replicate-ignore-db = information_schema replicate-ignore-db = performance_schema replicate-ignore-db = test replicate-ignore-db = zabbix max_connections = 3000max_connect_errors = 30skip-character-set-client-handshake #忽略应用程序想要设置的其他字符集 init-connect='SET NAMES utf8' #连接时执行的SQL character-set-server=utf8 #服务端默认字符集 wait_timeout=1800 #请求的最大连接时间 interactive_timeout=1800 #和上一参数同时修改才会生效 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES #sql模式 max_allowed_packet = 10M bulk_insert_buffer_size = 8M query_cache_type = 1query_cache_size = 128M query_cache_limit = 4M key_buffer_size = 256M read_buffer_size = 16K skip-name-resolve slow_query_log=1long_query_time = 6slow_query_log_file=slow-query.log innodb_flush_log_at_trx_commit = 2innodb_log_buffer_size = 16M [mysql] no-auto-rehash [myisamchk] key_buffer_size = 20M sort_buffer_size = 20M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout [mysqldump] quick max_allowed_packet = 16M [mysqld_safe]
cd /usr/local/mysql scripts/mysql_install_db --user=mysql
chmod +x /etc/rc.d/init.d/mysqld/etc/init.d/mysqld start
masterA上:
mysql> grant replication slave on *.* to 'repl'@'192.168.10.12' identified by '123456'; mysql> flush privileges;
masterB上:
mysql> grant replication slave on *.* to 'repl'@'192.168.10.11' identified by '123456'; mysql> flush privileges;
masterA上:
mysql> show master status;+------------------+----------+--------------+------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+--------------+------------------+-------------------+| mysql-bin.000003 | 120 | | | |+------------------+----------+--------------+------------------+-------------------+1 row in set (0.00 sec)
masterB上
mysql> show master status;+------------------+----------+--------------+------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+--------------+------------------+-------------------+| mysql-bin.000003 | 437 | | | |+------------------+----------+--------------+------------------+-------------------+1 row in set (0.00 sec)
masterA上:
mysql> change master to master_host='192.168.10.12',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=437; mysql> start slave; mysql> show slave status\G;
显示有如下状态则正常:
Slave_IO_Running: Yes Slave_SQL_Running: Yes
masterB上:
#本人是测试环境,可以保证没数据写入,否则需要的步骤是:先masterA锁表-->masterA备份数据-->masterA解锁表 -->masterB导入数据-->masterB设置主从-->查看主从
mysql> change master to master_host='192.168.10.11',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=120; start slave; mysql> show slave status\G;
显示有如下状态则正常:
Slave_IO_Running: Yes Slave_SQL_Running: Yes
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || test |+--------------------+4 rows in set (0.00 sec) mysql> create database test01; Query OK, 1 row affected (0.00 sec) mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || test || test01 |+--------------------+5 rows in set (0.00 sec) mysql> quit Bye[root@masterA data]#
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || test || test01 |+--------------------+5 rows in set (0.00 sec) mysql> quit Bye[root@masterB data]#
masterA和masterB分别执行如下命令:
mysql> stop slave; Query OK, 0 rows affected (0.00 sec) mysql> change master to MASTER_AUTO_POSITION=1; Query OK, 0 rows affected (0.01 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec)
一种主从报错折腾了我半天:
Last_IO_Errno: 1236
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not open log file'
后面修改主从同步相关 参数,确认原因是my.cnf增加了如下参数:
log-bin = mysql-bin
relay-log = mysql-bin
从正常主主同步时的二进制日志文件显示,有2套二进制日志。因此推断上面2个参数导致不能产生2套二进制文件,故导致二进制文件错乱和丢失。
设计MySQL双主架构方案的思路是什么就先给大家讲到这里,对于其它相关问题大家想要了解的可以持续关注我们的行业资讯。我们的板块内容每天都会捕捉一些行业新闻及专业知识分享给大家的。