Mysql主從工做示意圖:sql
mysql版本8.0.5,使用docker模擬,docker-compose配置以下:docker
mysql_master: image: mysql:latest ports: - "3306:3306" # hostname 當前容器內可以使用 hostname: msmaster volumes: - /data/conf/mysql/conf:/etc/mysql/conf.d - /data/conf/mysql/data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: 123456 mysql_slave: image: mysql:latest # links 當前容器與mysql_master創建鏈接,容器內/etc/hosts,能夠查看到mysql_master的ip配置 links: - mysql_master:mysql_master ports: - "3307:3306" hostname: msslave volumes: - /data/conf/mysql_slave/conf:/etc/mysql/conf.d - /data/conf/mysql_slave/data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: 123456
mysql主服務器爲從庫建立帳戶shell
#建立帳戶 爲了方便,沒有填從庫具體域名,%任意 CREATE USER 'repl'@'%' IDENTIFIED BY 'p4ssword'; #編碼方式 alter user 'repl'@'%' IDENTIFIED with mysql_native_password by 'p4ssword'; #分配權限 *.* 表明 庫名.表名(*表明所有) GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repl'@'msslave';
mysql從服務器爲主庫建立帳戶數據庫
#建立帳戶 CREATE USER 'repl'@'mysql_master' IDENTIFIED BY 'p4ssword'; alter user 'repl'@'mysql_master' IDENTIFIED with mysql_native_password by 'p4ssword'; #分配權限 *.* 表明 庫名.表名(*表明所有) GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repl'@'msmaster';
從庫配置帳號的目的,爲方便提供主從切換。服務器
配置文件下 /mysql/conf/ /mysql_slave/conf/網絡
master負載均衡
[mysqld] log_bin = mysql-bin server_id = 10 #設置server-id,必須惟一
slave編碼
log_bin = mysql-bin server_id = 2 # 中繼日誌的位置和命名 relay_log = /var/lib/mysql/mysql-relay-bin # 容許備庫將其重放的事件也記錄到自身二進制文件中 log_slave_updates = 1 # 阻止任何沒有特權權限的線程修改數據 read_only = 1
啓動複製線程
CHANGE MASTER TO MASTER_HOST='mysql_master', MASTER_USER='repl', MASTER_PASSWORD='p4ssword', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=0; #設置0,從日誌開頭讀起
查看從庫狀態:
SHOW SLAVE STATUS\G;
Slave_IO_Running Slave_SQL_Running Slave_IO_State 三列顯示還未開始備庫複製
開啓複製:
START SLAVE;
再次 SHOW SLAVE STATUS\G; 查看 Slave_IO_Running Slave_SQL_Running Slave_IO_State 可看出IO線程、SQL線程已經開啓;
查看線程列表:
SHOW PROCESSLIST\G;
*************************** 3. row *************************** Id: 9 User: system user Host: db: NULL Command: Connect Time: 328 State: Connecting to master Info: NULL *************************** 4. row *************************** Id: 10 User: system user Host: db: NULL Command: Query Time: 328 State: Slave has read all relay log; waiting for more updates Info: NULL
備庫的IO線程和SQL線程狀態;
切換到主庫查看
*************************** 2. row *************************** Id: 7 User: repl Host: 172.17.0.6:51074 db: NULL Command: Binlog Dump Time: 2978 State: Master has sent all binlog to slave; waiting for more updates Info: NULL
配置成功!
主從複製已經配置好,主庫修改數據,從庫拉去二進制文件發生錯誤: Last_SQL_Error: Error executing row event: 'Unknown database 'jump'',緣由在複製以前主從數據庫數據不一致。
須要初始化備庫,或者從其餘服務器克隆數據到備庫。
(1)主數據庫進行鎖表操做,不讓數據再進行寫入動做
mysql> FLUSH TABLES WITH READ LOCK;
(2)查看主數據庫狀態
mysql> show master status;
(3)記錄下 FILE 及 Position 的值。
將主服務器的數據文件(整個/opt/mysql/data目錄)複製到從服務器,建議經過tar歸檔壓縮後再傳到從服務器解壓。
(4)取消主數據庫鎖定
mysql> UNLOCK TABLES;
1) mysql8 root無grant權限,必須先建帳戶,再賦權限
CREATE USER 'root'@'%' IDENTIFIED BY '123456'; GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
2) Last_IO_Error: error connecting to master 'repl@mysql_master:3306' - retry-time: 60 retries: 1 網絡不通,檢查hosts是否存在