簡單工做原理:
(1)從庫執行 change master to 語句,會當即將主庫信息記錄到master.info中
(2)從庫執行 start slave語句,會當即生成IO_T和SQL_T
(3)IO_T 讀取master.info文件,獲取到主庫信息
(4)IO_T 鏈接主庫,主庫會當即分配一個DUMP_T,進行交互
(5)IO_T 根據master.info binlog信息,向DUMP_T請求最新的binlog
(6)主庫DUMP_T,通過查詢,若是發現有新的,截取並反回給從庫IO_T
(7)從庫IO_T會收到binlog,存儲在TCP/IP緩存中,在網絡底層返回ACK
(8)從庫IO_T會更新master.info ,重置binlog位置點信息
(9)從庫IO_T會將binlog,寫入到relay-log中
(10)從庫SQL_T 讀取Relay-log.info 文件,獲取上次執行過的位置點
(11)SQL_T按照位置點往下執行relaylog日誌
(12)SQL_T執行完成後,從新更新relay-log.info
(13)relaylog按期自動清理的功能。
線程:
主庫:
binlog_dump_thread 二進制日誌投遞線程
mysql -S /data/3307/mysql.sock -e "show processlist"
從庫:
IO_Thread : 從庫IO線程 : 請求和接收binlog
SQL_Thread: 從庫的SQL線程 : 回放日誌
主從環境的基本準備 html
1. mysql主從服務器說明node
192.168.1.219 mysql主服務器
192.168.1.17 mysql從服務器
192.168.1.151 mysql從服務器
#中間配置IP和網絡及防火牆設置省略
2. 安裝mysql
2.1 yum安裝所需相關依賴包。
yum -y install gcc-c++
yum -y install zlib zlib-devel pcre pcre-devel
yum -y install openssl-devel
yum -y install libaio-devel.x86_64
2.2搜索關鍵字:MySQL5.7 linux二進制安裝
參考網址:https://www.cnblogs.com/chenmh/p/5413881.htmlMySQL5.7 linux二進制安裝
下載路徑:
https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
2.2.1#建立mysql的數據目錄,該目錄在初始化數據庫的候會用到
mkdir -p /data/mysql/data
mkdir -p /data/mysql/log
2.2.2#移動mysql文件到/data/mysql/mysql
tar -zxvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
cd /usr/local/
ln -s mysql-5.7.26-linux-glibc2.12-x86_64 mysql ## 軟鏈接
2.2.3 #先建立mysql組
groupadd mysql
2.2.4#將MySQL用戶加入mysql組
useradd -r -g mysql -s /bin/false mysql
2.2.5#修改目錄權限
chown -R mysql:mysql /data/mysql/
chmod 750 /data/mysql
2.2.6#配置my.cnf
ls -l /etc/my.cnf
rpm -e mariadb-libs --nodeps
vim /etc/my.cnf #建立my.cnf文件要命名爲my.cnf,另外刪除/etc/下的全部my.*文件
=====================================================
/etc/my.cnf配置修改以下配置
主庫配置
[root@mysqltest01 ~]# cat /etc/my.cnf
[client]
port = 3306
socket = /data/mysql/mysql.sock
[mysqld]
port = 3306
user = mysql
character-set-server = utf8mb4
default_storage_engine = innodb
log_timestamps = SYSTEM
socket = /data/mysql/mysql.sock
basedir =/usr/local/mysql
datadir = /data/mysql/data
pid-file = /data/mysql/mysql.pid
max_connections = 1000
max_connect_errors = 1000
table_open_cache = 1024
max_allowed_packet = 128M
open_files_limit = 65535
server-id=1
gtid_mode=on
enforce_gtid_consistency=on
log-slave-updates=1
log-bin=master-bin
log-bin-index = master-bin.index
relay-log = relay-log
relay-log-index = relay-log.index
binlog_format=row
log_error = /data/mysql/log/mysql-error.log
skip-name-resolve
log-slave-updates=1
relay_log_purge = 0
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/log/mysql-slow.log
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
==========================================
從庫1配置:
[client]
port = 3306
socket = /data/mysql/mysql.sock
[mysqld]
port = 3306
user = mysql
character-set-server = utf8mb4
default_storage_engine = innodb
log_timestamps = SYSTEM
socket = /data/mysql/mysql.sock
basedir =/usr/local/mysql
datadir = /data/mysql/data
pid-file = /data/mysql/mysql.pid
max_connections = 1000
max_connect_errors = 1000
table_open_cache = 1024
max_allowed_packet = 128M
open_files_limit = 65535
server-id=2
gtid_mode=on
enforce_gtid_consistency=on
log-slave-updates=1
log-bin=master-bin
log-bin-index = master-bin.index
relay-log = relay-log
relay-log-index = relay-log.index
binlog_format=row
log_error = /data/mysql/log/mysql-error.log
skip-name-resolve
log-slave-updates=1
relay_log_purge = 0
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/log/mysql-slow.log
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
=========================================
從庫2配置:
[client]
port = 3306
socket = /data/mysql/mysql.sock
[mysqld]
port = 3306
user = mysql
character-set-server = utf8mb4
default_storage_engine = innodb
log_timestamps = SYSTEM
socket = /data/mysql/mysql.sock
basedir =/usr/local/mysql
datadir = /data/mysql/data
pid-file = /data/mysql/mysql.pid
max_connections = 1000
max_connect_errors = 1000
table_open_cache = 1024
max_allowed_packet = 128M
open_files_limit = 65535
server-id=3
gtid_mode=on
enforce_gtid_consistency=on
log-slave-updates=1
log-bin=master-bin
log-bin-index = master-bin.index
relay-log = relay-log
relay-log-index = relay-log.index
binlog_format=row
log_error = /data/mysql/log/mysql-error.log
skip-name-resolve
log-slave-updates=1
relay_log_purge = 0
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/log/mysql-slow.log
============================================
2.2.7#初始化數據庫
/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql --basedir=/data/mysql/mysql --datadir=/data/mysql/data --innodb_undo_tablespaces=3 --explicit_defaults_for_timestamp #初始化mysql
2.2.7#根據配置了my.cnf的error.log,查看初始密碼
grep 'password' /data/mysql/log/mysql-error.log
2.2.8#建立ssl加密
/usr/local/mysql/bin/mysql_ssl_rsa_setup --datadir=/data/mysql/data
2.2.9#將裏面的basedir和datadir改成和my.cnf一致
vi /usr/local/mysql/support-files/mysql.server
basedir=/usr/local/mysql
datadir=/data/mysql/data
2.2.10#拷貝和配置啓動文件
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
2.2.11設置開機啓動
chkconfig --add mysqld
chkconfig mysqld on
2.2.12配置環境變量
echo 'PATH=/usr/local/mysql/bin:$PATH'>>/etc/profile
tail -1 /etc/profile
source /etc/profile #讓環境變量生效
echo $PATH #檢查變量是否添加成功
##/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
2.2.13#啓動mysql
service mysqld start
2.2.14#進入mysql
mysql -uroot -p #密碼經過前面的2.2.7找到
2.2.15#修改初始密碼
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
2.2.16#刷新權限
flush privileges;
2.2.17#查看路徑信息
SHOW GLOBAL VARIABLES LIKE '%log%';
2.2.18日誌查詢
1.查找錯誤日誌文件路徑
show variables like 'log_error';
2.查找日誌文件路徑
show variables like 'general_log_file';
3.慢查詢日誌文件路徑
show variables like 'slow_query_log_file';
2.2.19加入開機啓動項
vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
chmod +x /usr/lib/systemd/system/mysqld.service ##給予執行權限
systemctl daemon-reload ###從新加載
systemctl enable mysqld.service
systemctl start mysqld.service
============================================mysql
GTID主從複製: linux
配置步驟:
1.檢查gtid是否運行正常
啓動後可使用show global variables like '%gtid%';查看GTID狀態
gtid_mode | ON c++
enforce_gtid_consistency | ON sql
注:主庫從庫都須要開啓GTID不然在作主從複製的時候就會報錯:
查看數據庫的uuid
在數據目錄的auto.cnf
在mysql裏面使用命令查看show global variables like 'server_uuid'
2.登陸主庫給從庫複製權限
grant replication slave on *.* to 'replication'@'192.168.1.%' identified by '1234';
flush privileges;
select host,user from mysql.user;
3.登陸從庫使用命令進行復制
change master to
master_host='192.168.1.219',
master_port=3306,
master_user='replication',
###用戶名和密碼要與在主庫上創建的一致
master_password='1234',
master_auto_position =
1; ##此參數能夠一直不變化
start slave;
##啓動同步
show slave status\G;
##查看同步狀態
Slave_IO_Running: Yes 數據庫
Slave_SQL_Running: Yesvim
這兩個參數必須爲yes不然同步沒有成功 緩存
4.主從的binlog日誌觀察
show binlog events in 'master-bin.000001';
5.測試同步
增長數據來驗證是否同步成功
create database shijiange;
use shijiange;
create table test (id int);
insert into test values (1);
update test set id = 3 where id = 1;
delete from test;
drop database shijiange;
主庫的信息(master.info):
Master_Host: 10.0.0.51 主庫的IP
Master_User: repl
複製用戶名
Master_Port: 3307
主庫的端口
Connect_Retry: 10
斷連以後重試次數
Master_Log_File: mysql-bin.000001 已經獲取獲得binlog的文件名
Read_Master_Log_Pos: 444 已經獲取獲得binlog的位置號
從庫的relaylog的信息(relay-log.info):
Relay_Log_File: db01-relay-bin.000002 從庫已經運行過的relaylog的文件名
Relay_Log_Pos: 320
從庫已經運行過的relaylog的位置點
從庫複製線程工做狀態:
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:
從庫延時主庫的時間:
Seconds_Behind_Master: 0 從庫延時主庫的時間(秒爲單位)
從庫線程報錯詳細信息:
Last_IO_Errno: 0 IO報錯的號碼
Last_IO_Error: IO報錯的具體信息
Last_SQL_Errno: 0 SQL報錯的號碼
Last_SQL_Error: SQL線程報錯的具體緣由
延時從庫:
SQL_Delay: 0 延時從庫設定的時間
SQL_Remaining_Delay: NULL 延時操做剩餘時間
GTID複製信息:
Retrieved_Gtid_Set: 接收到的GTID的個數
Executed_Gtid_Set:
執行了的GTID的個數
SQL線程故障
緣由一:
讀relay-log.info
讀relay-log ,並執行日誌
更新relay-log.info
以上文件損壞,最好是從新構建主從
緣由二:
爲何一條SQL語句執行不成功?
1. 主從數據庫版本差別較大
2. 主從數據庫配置參數不一致(例如:sql_mode等)
3. 想要建立的對象已經存在
4. 想要刪除或修改的對象不存在
5. 主鍵衝突
6. DML語句不符合表定義及約束時
歸根結底是從庫寫入了。
IO線程No的狀態分析:
緣由一: 日誌名不對
從庫信息:
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 444
對比備份的位置號。
緣由二:日誌損壞,日誌不連續