您好,登錄后才能下訂單哦!
本文主要給大家簡單講講設計MySQL雙主架構方案的思路是什么,相關專業術語大家可以上網查查或者找一些相關書籍補充一下,這里就不涉獵了,我們就直奔主題吧,希望設計MySQL雙主架構方案的思路是什么這篇文章可以給大家帶來一些實際幫助。
在開始之前,我們先來了解主從同步復制原理。
復制分成三步:
下圖描述了這一過程:
該過程的第一部分就是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雙主架構方案的思路是什么就先給大家講到這里,對于其它相關問題大家想要了解的可以持續關注我們的行業資訊。我們的板塊內容每天都會捕捉一些行業新聞及專業知識分享給大家的。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。