您好,登錄后才能下訂單哦!
下文主要給大家帶來MySQL-5.5主從復制原理是什么及如何配置,希望MySQL-5.5主從復制原理是什么及如何配置能夠帶給大家實際用處,這也是我編輯這篇文章的主要目的。好了,廢話不多說,大家直接看下文吧。
環境:
[root@SQL-M ~]# cat /etc/redhat-release
CentOS release 6.8 (Final)
[root@SQL-M ~]# uname -r
2.6.32-642.el6.x86_64
Master IP 192.168.0.88/24 eth0
Slave IP 192.168.0.90/24 eth0
主從復制原理:
當用戶對數據有增刪改操作時,主庫本地存一份,另外會把用戶增刪改的操作記錄在 binlog 里面(binlog是實現主從復制的基礎),binlog的索引文件是mysql-bin.index;從庫的IO線程根據本地master.info文件里面記錄的ip、port、user、password、binlog name、pos連接主庫IO線程,主庫判斷信息,正確就返回數據,返回的數據里包括下次復制起始點的binlog名稱和pos值;從庫收到數據后寫入relay-log,同時把下次復制起始點的binlog名稱和pos值刷新進master.info文件,之后從庫的SQL線程讀取relay-log里面的SQL語句,執行語句將數據寫入本地磁盤,主從復制完成。
配置要點:
主從復制,主庫開啟 bin-log 從庫開啟 relay-log ,主從 server-id 不能相同。
Master 配置:
[root@SQL-M ~]# vim /etc/my.cnf [client] port=3306 socket= /usr/local/mysql/mysql.sock default-character-set = utf8 [mysql] no-auto-rehash prompt=Master>\_ [mysqld] user = mysql port = 3306 socket = /usr/local/mysql/mysql.sock basedir = /usr/local/mysql datadir = /usr/local/mysql/data character-set-server = utf8 skip-character-set-client-handshake init-connect = 'SET NAMES utf8' open_files_limit=1024 back_log = 600 max_connections = 800 max_connect_errors = 3000 table_cache = 614 external-locking = FALSE max_allowed_packet =8M sort_buffer_size = 1M join_buffer_size = 1M thread_cache_size = 100 thread_concurrency = 2 query_cache_size = 2M query_cache_limit = 1M query_cache_min_res_unit = 2k thread_stack = 192K tmp_table_size = 2M max_heap_table_size = 2M server-id = 1 <<--- id 為 1 log-bin = /usr/local/mysql/data/mysql-bin <<--- 開啟 bin log binlog_cache_size = 1M max_binlog_cache_size = 1M max_binlog_size = 2M expire_logs_days = 7 key_buffer_size = 16M read_buffer_size = 1M read_rnd_buffer_size = 1M bulk_insert_buffer_size = 1M lower_case_table_names = 1 skip-name-resolve slave-skip-errors = 1032,1062,1007,1008,1050 replicate-ignore-db=mysql innodb_additional_mem_pool_size = 4M innodb_buffer_pool_size = 16M innodb_file_io_threads = 4 innodb_thread_concurrency = 8 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 2M innodb_log_file_size = 4M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 innodb_file_per_table = 0 [mysqldump] quick max_allowed_packet = 2M [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/usr/local/mysql/mysqld.pid
Slave 配置:
[root@SQL-S1 ~]# vim /etc/my.cnf [client] port=3306 socket= /usr/local/mysql/mysql.sock default-character-set = utf8 [mysql] no-auto-rehash prompt=Slave>\_ [mysqld] user = mysql port = 3306 socket = /usr/local/mysql/mysql.sock basedir = /usr/local/mysql datadir = /usr/local/mysql/data character-set-server = utf8 skip-character-set-client-handshake init-connect = 'SET NAMES utf8' open_files_limit=1024 back_log = 600 max_connections = 800 max_connect_errors = 3000 table_cache = 614 external-locking = FALSE max_allowed_packet =8M sort_buffer_size = 1M join_buffer_size = 1M thread_cache_size = 100 thread_concurrency = 2 query_cache_size = 2M query_cache_limit = 1M query_cache_min_res_unit = 2k thread_stack = 192K tmp_table_size = 2M max_heap_table_size = 2M server-id = 2 <<--- id 為 2 relay-log =/usr/local/mysql/data/relay-bin <<--- 開啟 relay log relay-log-info-file = /usr/local/mysql/data/relay-log.info key_buffer_size = 16M read_buffer_size = 1M read_rnd_buffer_size = 1M bulk_insert_buffer_size = 1M lower_case_table_names = 1 skip-name-resolve slave-skip-errors = 1032,1062,1007,1008,1050 replicate-ignore-db=mysql innodb_additional_mem_pool_size = 4M innodb_buffer_pool_size = 16M innodb_file_io_threads = 4 innodb_thread_concurrency = 8 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 2M innodb_log_file_size = 4M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 innodb_file_per_table = 0 [mysqldump] quick max_allowed_packet = 2M [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
Master 端準備數據和創建復制用戶
[root@SQL-M ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.55-log MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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. Master> ster> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school | +--------------------+ 4 rows in set (0.00 sec) Master> use school; Database changed Master> Master> show tables; +------------------+ | Tables_in_school | +------------------+ | student | | test01 | | test02 | +------------------+ 3 rows in set (0.00 sec) Master> select * from student; +----+--------+-----+-----+ | id | name | sex | age | +----+--------+-----+-----+ | 1 | 小東 | 男 | 0 | | 3 | 小北 | 女 | 12 | +----+--------+-----+-----+ 2 rows in set (0.00 sec) Master> grant replication slave on *.* to rep@'192.168.0.%' identified by '123'; # 創建專門用于主從復制的用戶 Query OK, 0 rows affected (0.00 sec) Master> select user,host from mysql.user; +------+-------------+ | user | host | +------+-------------+ | root | 127.0.0.1 | | rep | 192.168.0.% | | root | localhost | +------+-------------+ 3 rows in set (0.00 sec) Master> show grants for replicaton@'192.168.0.%'; ERROR 1141 (42000): There is no such grant defined for user 'replicaton' on host '192.168.0.%' Master> show grants for rep@'192.168.0.%'; +--------------------------------------------------------------------------------------------------------------------------+ | Grants for rep@192.168.0.% | +--------------------------------------------------------------------------------------------------------------------------+ | GRANT REPLICATION SLAVE ON *.* TO 'rep'@'192.168.0.%' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' | +--------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
slave 端沒有數據,處于初始狀態
[root@SQL-S1 ~]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.55 MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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. Slave01> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec)
master 端用 mysqldump 導出數據
由于 mysqldump 是邏輯備份程序,所以要確保 MySQL 服務是啟動狀態。
[root@SQL-M ~]# mysqldump -uroot -p -A -B -F --master-data=1 --events >/tmp/sql_full_back.sql Enter password: [root@SQL-M ~]# ll -h /tmp/ total 152K -rw-r--r-- 1 root root 151K Apr 23 12:48 sql_full_back.sql
mysqldump 參數:
-A 備份所有庫表 -B 在導出的 sql 文件里加入建庫語句,從庫導入文件時就省事很多 -F 刷新 bin log,這個在增量恢復時有用 --master-data=1 這個參數的作用是在導出的 sql 文件里會加入一條語句“CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.xxxxxx', MASTER_LOG_POS=xxx;” ,這樣在從庫導入數據后執行 CHANGE MASTER 時就不用加上bin-log和pos值了;=2 則是注釋。 --events 忽略警告 Warning: Skipping the data of table mysql.event. Specify the --events option explicitly. 數據量大可以備份時gzip壓縮: mysqldump -uroot -p -A -B -F --master-data=1 --events|gzip >/tmp/sql_full_back.sql.gz
把備份文件 scp 到從庫
[root@SQL-M ~]# scp /tmp/sql_full_back.sql 192.168.0.90:/tmp/ The authenticity of host '192.168.0.90 (192.168.0.90)' can't be established. RSA key fingerprint is fb:9f:50:cd:ac:59:8b:a3:83:83:95:7c:62:d1:64:d2. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.0.90' (RSA) to the list of known hosts. root@192.168.0.90's password: sql_full_back.sql 100% 546KB 546.1KB/s 00:00
從庫導入備份文件并檢查
[root@SQL-S1 ~]# ll /tmp/ total 548 -rw-r--r-- 1 root root 559192 Apr 23 13:10 sql_full_back.sql [root@SQL-S1 ~]# [root@SQL-S1 ~]# mysql -uroot -p </tmp/sql_full_back.sql Enter password: [root@SQL-S1 ~]# mysql -uroot -p # 登錄檢查一下 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.5.55 MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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. Slave> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school | +--------------------+ 4 rows in set (0.00 sec) Slave> Slave> use school; Database changed Slave> show tables; +------------------+ | Tables_in_school | +------------------+ | student | | test01 | | test02 | +------------------+ 3 rows in set (0.00 sec) Slave> select * from student; +----+--------+-----+-----+ | id | name | sex | age | +----+--------+-----+-----+ | 1 | 小東 | 男 | 0 | | 3 | 小北 | 女 | 12 | +----+--------+-----+-----+ 2 rows in set (0.00 sec)
從庫導入備份文件檢查成功后 CHANGE MASTER
Slave> CHANGE MASTER TO MASTER_HOST='192.168.0.88',MASTER_PORT=3306,MASTER_USER='rep',MASTER_PASSWORD='123'; Query OK, 0 rows affected (0.01 sec) Slave> start slave; # 啟動 slave Query OK, 0 rows affected (0.00 sec) Slave> show slave status\G # 查看狀態 *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.88 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000016 Read_Master_Log_Pos: 297 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 253 Relay_Master_Log_File: mysql-bin.000016 Slave_IO_Running: Yes <<--- 正常 Slave_SQL_Running: Yes <<--- 正常 Replicate_Do_DB: Replicate_Ignore_DB: mysql 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: 297 Relay_Log_Space: 403 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: 1 1 row in set (0.00 sec)
slave 的狀態判斷:
Slave_IO_Running: Yes # IO線程負責與主庫通信傳輸數據 Slave_SQL_Running: Yes # SQL線程,讀取中繼日志(rely-log),再把數據寫入本地存儲 Seconds_Behind_Master: 0 # 延遲時間,從主獲取數據的延遲時間,
這三個參數是主從復制健康檢查的監控的重點。
主庫查看線程狀態
Master> show processlist; +----+------+--------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+--------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+ | 27 | root | localhost | NULL | Query | 0 | NULL | show processlist | | 29 | rep | 192.168.0.90:64017 | NULL | Binlog Dump | 497 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL | +----+------+--------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+ 2 rows in set (0.00 sec)
測試主從復制
Master 添加數據
Master> use school; Database changed Master> select * from student; +----+--------+-----+-----+ | id | name | sex | age | +----+--------+-----+-----+ | 1 | 小東 | 男 | 0 | | 3 | 小北 | 女 | 12 | +----+--------+-----+-----+ 2 rows in set (0.00 sec) Master> insert into student values(4,'楠楠','男',15); # 插入新數據 Query OK, 1 row affected (0.00 sec) Master> select * from student; +----+--------+-----+-----+ | id | name | sex | age | +----+--------+-----+-----+ | 1 | 小東 | 男 | 0 | | 3 | 小北 | 女 | 12 | | 4 | 楠楠 | 男 | 15 | +----+--------+-----+-----+ 3 rows in set (0.00 sec)
Slave 端檢查
Slave> use school; Database changed Slave> show tables; +------------------+ | Tables_in_school | +------------------+ | student | | test01 | | test02 | +------------------+ 3 rows in set (0.00 sec) Slave> select * from student; +----+--------+-----+-----+ | id | name | sex | age | +----+--------+-----+-----+ | 1 | 小東 | 男 | 0 | | 3 | 小北 | 女 | 12 | | 4 | 楠楠 | 男 | 15 | <<---- 可以看到新的數據已經復制到位 +----+--------+-----+-----+ 3 rows in set (0.00 sec)
以上主從復制配置完成
===================== 開啟半同步模式 =========================
主從復制實際是異步的過程:
Master IO_thread --> Slave IO_thread -->Slave SQL_thread -->Slave localdisk
Master IO_thread 把數據交給 Slave IO_thread 之后就不管了,后面的數據存儲有沒有成功Master是不知道的,這樣對數據來說顯然是不夠安全的,無法保證數據完整正確地存儲在Slave端。
半同步復制
介于異步復制和全同步復制之間,主庫在執行完客戶端提交的事務后不是立刻返回給客戶端,而是等待至少一個從庫接收到并寫到relay log中才返回給客戶端。相對于異步復制,半同步復制提高了數據的安全性,同時它也造成了一定程度的延遲,這個延遲最少是一個TCP/IP往返的時間。所以,半同步復制最好在低延時的網絡中使用。
半同步是以已經實現主從復制為前提,并且MySQL版本為5.5及以上。
實現半同步的插件:
[root@SQL-M ~]# ll /usr/local/mysql/lib/plugin/ -rwxr-xr-x 1 mysql mysql 170878 Mar 18 13:14 semisync_master.so -rwxr-xr-x 1 mysql mysql 88959 Mar 18 13:14 semisync_slave.so
很清楚,一個Master用的,一個Slave用的。
Master 端操作
Master> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'; # 安裝插件 Query OK, 0 rows affected (0.13 sec) Master> SET GLOBAL rpl_semi_sync_master_enabled = 1; # 啟用插件 Query OK, 0 rows affected (0.00 sec) Master> show status like 'Rpl_semi_sync_master_status'; +-----------------------------+-------+ | Variable_name | Value | +-----------------------------+-------+ | Rpl_semi_sync_master_status | ON | +-----------------------------+-------+ 1 row in set (0.00 sec) Master> show variables like 'rpl%'; +------------------------------------+-------+ | Variable_name | Value | +------------------------------------+-------+ | rpl_recovery_rank | 0 | | rpl_semi_sync_master_enabled | ON | | rpl_semi_sync_master_timeout | 10000 | <<--- 默認超時,單位毫秒;數據傳輸超時會自動轉為異步復制,傳輸正常后會自動恢復為半同步。 | rpl_semi_sync_master_trace_level | 32 | | rpl_semi_sync_master_wait_no_slave | ON | +------------------------------------+-------+ 5 rows in set (0.00 sec) Master> SELECT PLUGIN_NAME, PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME LIKE '%semi%'; +----------------------+---------------+ | PLUGIN_NAME | PLUGIN_STATUS | +----------------------+---------------+ | rpl_semi_sync_master | ACTIVE | +----------------------+---------------+ 1 row in set (0.00 sec) Master> show status like 'rpl%'; +--------------------------------------------+-------------+ | Variable_name | Value | +--------------------------------------------+-------------+ | Rpl_semi_sync_master_clients | 0 | <<--- 還沒有從庫連接 | Rpl_semi_sync_master_net_avg_wait_time | 0 | | Rpl_semi_sync_master_net_wait_time | 0 | | Rpl_semi_sync_master_net_waits | 0 | | Rpl_semi_sync_master_no_times | 0 | | Rpl_semi_sync_master_no_tx | 0 | | Rpl_semi_sync_master_status | ON | | Rpl_semi_sync_master_timefunc_failures | 0 | | Rpl_semi_sync_master_tx_avg_wait_time | 0 | | Rpl_semi_sync_master_tx_wait_time | 0 | | Rpl_semi_sync_master_tx_waits | 0 | | Rpl_semi_sync_master_wait_pos_backtraverse | 0 | | Rpl_semi_sync_master_wait_sessions | 0 | | Rpl_semi_sync_master_yes_tx | 0 | | Rpl_status | AUTH_MASTER | +--------------------------------------------+-------------+ 15 rows in set (0.00 sec)
Slave 端操作
Slave> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'; # 安裝slave插件 Query OK, 0 rows affected (0.11 sec) Slave> SET GLOBAL rpl_semi_sync_slave_enabled = 1; # 啟用插件 Query OK, 0 rows affected (0.00 sec) Slave> show status like 'Rpl_semi_sync_slave_status'; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | Rpl_semi_sync_slave_status | OFF | +----------------------------+-------+ 1 row in set (0.00 sec) Slave> stop slave; # 重啟slave Query OK, 0 rows affected (0.00 sec) Slave> start slave; Query OK, 0 rows affected (0.00 sec) Slave> show status like 'Rpl_semi_sync_slave_status'; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | Rpl_semi_sync_slave_status | ON | +----------------------------+-------+ 1 row in set (0.00 sec) Slave> show status like 'rpl%'; +----------------------------+-------------+ | Variable_name | Value | +----------------------------+-------------+ | Rpl_semi_sync_slave_status | ON | | Rpl_status | AUTH_MASTER | +----------------------------+-------------+ 2 rows in set (0.00 sec) Slave> SELECT PLUGIN_NAME, PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME LIKE '%semi%'; +---------------------+---------------+ | PLUGIN_NAME | PLUGIN_STATUS | +---------------------+---------------+ | rpl_semi_sync_slave | ACTIVE | +---------------------+---------------+ 1 row in set (0.00 sec)
Master端檢查從庫連接情況
Master> show status like 'rpl%'; +--------------------------------------------+-------------+ | Variable_name | Value | +--------------------------------------------+-------------+ | Rpl_semi_sync_master_clients | 1 | <<--- 可以看到有一個從庫已經成功連接 | Rpl_semi_sync_master_net_avg_wait_time | 0 | | Rpl_semi_sync_master_net_wait_time | 0 | | Rpl_semi_sync_master_net_waits | 0 | | Rpl_semi_sync_master_no_times | 0 | | Rpl_semi_sync_master_no_tx | 0 | | Rpl_semi_sync_master_status | ON | | Rpl_semi_sync_master_timefunc_failures | 0 | | Rpl_semi_sync_master_tx_avg_wait_time | 0 | | Rpl_semi_sync_master_tx_wait_time | 0 | | Rpl_semi_sync_master_tx_waits | 0 | | Rpl_semi_sync_master_wait_pos_backtraverse | 0 | | Rpl_semi_sync_master_wait_sessions | 0 | | Rpl_semi_sync_master_yes_tx | 0 | | Rpl_status | AUTH_MASTER | +--------------------------------------------+-------------+ 15 rows in set (0.00 sec)
半同步測試
正常情況下master插入數據的速度很快
Master> insert into student values(6,'小欣','女',13);
Query OK, 1 row affected (0.00 sec) <<--- 速度很快
Master> insert into student values(7,'小倩','女',13);
Query OK, 1 row affected (0.00 sec) <<--- 一樣
接下來模仿從庫故障,停掉slave的 IO 線程
Slave> stop slave io_thread;
Query OK, 0 rows affected (0.00 sec)
Master 再插入數據
Master> insert into student values(8,'姍姍','女',13);
Query OK, 1 row affected (10.00 sec) <<--- 超時了,自動轉為異步
從庫恢復正常
Slave> start slave io_thread;
Query OK, 0 rows affected (0.00 sec)
主庫插入數據
Master> insert into student values(9,'小強','男',13);
Query OK, 1 row affected (0.00 sec) <<--- 自動恢復為半同步了
============================================================
到此半同步配置完成
對于以上關于MySQL-5.5主從復制原理是什么及如何配置,大家是不是覺得非常有幫助。如果需要了解更多內容,請繼續關注我們的行業資訊,相信你會喜歡上這些內容的。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。