mysql的主從數據同步是一個異步複製過程,須要master開啓bin-log日誌功能,bin-log記錄了master庫中的增、刪、修改、更新操做的sql語句,整個過程須要開啓3個線程,分別是master開啓I/O線程,slave開啓I/O線程和SQL線程html
一、在slave服務器上執行start slave命令開啓主從複製開關,主從複製開始進行,slave I/O線程會經過master建立的受權用戶鏈接上master,並請求master從指定文件和位置以後發送bin-log日誌內容mysql
二、master接收請求後,master I/O線程更加slave發送的指定bin-log日誌position點以後的內容,而後返回給slave的I/O線程;返回的信息中除了bin-log日誌外,還有在master服務器記錄的新的bin-log文件名及新的bin-log中的下一個指定更新位置(position)web
三、slave I/O線程接收信息後,將接收的日誌內容一次添加到slave端的relay-log(中繼日誌)文件(mysql-relay-bin.xxxxxx)的最末端,並將讀取到的master端的bin-log文件名和position點記錄到master.info文件中,以便下次讀取時可以告知master從相應的bin-log文件名及最後一個position點開始發起請求sql
四、slave SQL 線程檢測到relay-log中I/O線程新增長的內容有更新,會當即分析relay-log日誌中的內容,將解析的sql語句按順序在slave裏執行,並記錄應用中繼日誌的文件名及位置點在relay-log.info中,執行成功後slave庫與master庫數據保持一致數據庫
主從複製是異步的邏輯的SQL語句級的複製centos
複製時,主庫有一個I/O線程,從庫有兩個線程,I/O和SQL線程服務器
做爲複製的全部mysql節點server-id都不能相同網絡
bin-log文件只記錄對數據庫有更改的sql語句(數據庫內容的變動),不記錄任何查詢(select,slow)語句異步
原理流程圖以下:socket
開啓binlog功能
主庫要創建帳號
從庫要配置master.info
start slave 開啓複製功能
master:192.168.216.52
slave:192.168.216.53
mariadb版本10.2.24
[root@web2 ~]# rpm -qa Maria* MariaDB-server-10.2.24-1.el7.centos.x86_64 MariaDB-compat-10.2.24-1.el7.centos.x86_64 MariaDB-common-10.2.24-1.el7.centos.x86_64 MariaDB-client-10.2.24-1.el7.centos.x86_64 [root@web2 ~]#
[root@web2 ~]# cat /etc/yum.repos.d/mariadb.repo [mariadb] name = MariaDB baseurl = https://mirrors.ustc.edu.cn/mariadb/yum/10.2/centos7-amd64/ gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB gpgcheck=1
兩臺機器都安裝
yum install mariadb-server
參考:http://www.javashuo.com/article/p-xiiiwqiz-k.html
[root@web2 my.cnf.d]# cat server.cnf # # These groups are read by MariaDB server. # Use it for options that only the server (but not clients) should see # # See the examples of server my.cnf files in /usr/share/mysql/ # # this is read by the standalone daemon and embedded servers [server] # this is only for the mysqld standalone daemon [mysqld] server-id=1 log-bin=mysql-bin #binlog-do-db=liting #binlog-ignore-db=mysql sync_binlog=1 binlog_checksum = none binlog_format = mixed port = 3306 socket = /var/lib/mysql/mysql.sock skip-external-locking key_buffer_size = 256MB max_allowed_packet = 1MB table_open_cache = 256 sort_buffer_size = 1MB read_buffer_size = 1MB read_rnd_buffer_size = 4MB myisam_sort_buffer_size = 64MB thread_cache_size = 8 query_cache_size = 16MB thread_concurrency = 8 [mysqldump] quick max_allowed_packet = 16MB [mysql] no-auto-rehash [myisamchk] key_buffer_size = 128MB sort_buffer_size = 128MB read_buffer = 2MB write_buffer = 2MB [mysqlhotcopy] interactive-timeout # # * Galera-related settings # [galera] # Mandatory settings #wsrep_on=ON #wsrep_provider= #wsrep_cluster_address= #binlog_format=row #default_storage_engine=InnoDB #innodb_autoinc_lock_mode=2 # # Allow server to accept connections on all interfaces. # #bind-address=0.0.0.0 # # Optional setting #wsrep_slave_threads=1 #innodb_flush_log_at_trx_commit=0 # this is only for embedded server [embedded] # This group is only read by MariaDB servers, not by MySQL. # If you use the same .cnf file for MySQL and MariaDB, # you can put MariaDB-only options here [mariadb] # This group is only read by MariaDB-10.2 servers. # If you use the same .cnf file for MariaDB of different versions, # use this group for options that older servers don't understand [mariadb-10.2] [root@web2 my.cnf.d]#
grant replication slave,replication client on *.* to 'tongbu'@'%' identified by '123456';
MariaDB [test3]> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 320| | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
[root@web3 my.cnf.d]# cat server.cnf # # These groups are read by MariaDB server. # Use it for options that only the server (but not clients) should see # # See the examples of server my.cnf files in /usr/share/mysql/ # # this is read by the standalone daemon and embedded servers [server] # this is only for the mysqld standalone daemon [mysqld] socket = /var/lib/mysql/mysql.sock port = 3306 skip-external-locking key_buffer_size =256MB max_allowed_packet = 1MB table_open_cache = 256 sort_buffer_size = 1MB read_buffer_size = 1MB read_rnd_buffer_size = 4MB myisam_sort_buffer_size = 64MB thread_cache_size = 8 query_cache_size = 16MB thread_concurrency = 8 server-id = 2 # # * Galera-related settings # [galera] # Mandatory settings #wsrep_on=ON #wsrep_provider= #wsrep_cluster_address= #binlog_format=row #default_storage_engine=InnoDB #innodb_autoinc_lock_mode=2 # # Allow server to accept connections on all interfaces. # #bind-address=0.0.0.0 # # Optional setting #wsrep_slave_threads=1 #innodb_flush_log_at_trx_commit=0 # this is only for embedded server [embedded] # This group is only read by MariaDB servers, not by MySQL. # If you use the same .cnf file for MySQL and MariaDB, # you can put MariaDB-only options here [mariadb] # This group is only read by MariaDB-10.2 servers. # If you use the same .cnf file for MariaDB of different versions, # use this group for options that older servers don't understand [mariadb-10.2] [root@web3 my.cnf.d]#
[root@web3 my.cnf.d]# cat mysql-clients.cnf #[mysql][mysqlcheck][mysqldump]也能夠寫在這個文件裏,master我是都卸載server.cnf裏面了 # # These groups are read by MariaDB command-line tools # Use it for options that affect only one utility # [mysql] no-auto-rehash [mysql_upgrade] [mysqladmin] [mysqlbinlog] [mysqlcheck] key_buffer_size = 128MB sort_buffer_size = 128MB read_buffer = 2MB write_buffer = 2MB [mysqldump] quick max_allowed_packet = 16MB [mysqlimport] [mysqlshow] [mysqlslap] [mysqlhotcopy] interactive-timeout [root@web3 my.cnf.d]#
slave指定master ip、用戶名、密碼、bin-log文件名、position(下面標記紅色部分)
[root@web3 my.cnf.d]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 12 Server version: 10.2.24-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> change master to master_host='192.168.216.52',master_user='tongbu',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=320;MariaDB [(none)]> master_host='192.168.216.52',master_user='tongbu',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=320;
MariaDB [(none)]> slave start;
MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.216.52 Master_User: tongbu Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000004 Read_Master_Log_Pos: 593 Relay_Log_File: web3-relay-bin.000006 Relay_Log_Pos: 828 Relay_Master_Log_File: mysql-bin.000004 Slave_IO_Running: Yes Slave_SQL_Running: Yes 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: 593 Relay_Log_Space: 1136 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 Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: No Gtid_IO_Pos: Replicate_Do_Domain_Ids: Replicate_Ignore_Domain_Ids: Parallel_Mode: conservative SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it 1 row in set (0.00 sec)
須要關注以下幾點
引用:http://www.javashuo.com/article/p-nlyvyhli-bg.html mysql主從同步(4)-Slave延遲狀態監控
# 1)Slave_IO_Running:該參數可做爲io_thread的監控項,Yes表示io_thread的和主庫鏈接正常並能實施複製工做,No則說明與主庫通信異常,多數狀況是由主從間網絡引發的問題;
2)Slave_SQL_Running:該參數表明sql_thread是否正常,YES表示正常,NO表示執行失敗,具體就是語句是否執行經過,常會遇到主鍵重複或是某個表不存在。
3)Seconds_Behind_Master:是經過比較sql_thread執行的event的timestamp和io_thread複製好的event的timestamp(簡寫爲ts)進行比較,而獲得的這麼一個差值;
NULL—表示io_thread或是sql_thread有任何一個發生故障,也就是該線程的Running狀態是No,而非Yes。
0 — 該值爲零,是咱們極爲渴望看到的狀況,表示主從複製良好,能夠認爲lag不存在。
正值 — 表示主從已經出現延時,數字越大表示從庫落後主庫越多。
負值 — 幾乎不多見,我只是聽一些資深的DBA說見過,其實,這是一個BUG值,該參數是不支持負值的,也就是不該該出現。
[root@web2 my.cnf.d]# mysql -uroot -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 11 Server version: 10.2.24-MariaDB-log MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> MariaDB [(none)]> create database test3; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> use test3; Database changed MariaDB [test3]> create table t1(id varchar(20),name varchar(20)); Query OK, 0 rows affected (0.02 sec) MariaDB [test3]> show tables; +-----------------+ | Tables_in_test3 | +-----------------+ | t1 | +-----------------+ 1 row in set (0.00 sec) MariaDB [test3]>
MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | test3 | +--------------------+ 5 rows in set (0.00 sec) MariaDB [(none)]>