構建docker鏡像,其中數據卷配置內容在下面,結構目錄以下html
version: '3.7' services: db: # images 8.x image: mysql restart: always environment: MYSQL_ROOT_PASSWORD: 456123 command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci --explicit_defaults_for_timestamp=true --lower_case_table_names=1 ports: - 3309:3306 volumes: - ./data:/var/lib/mysql - ./my.cnf:/etc/my.cnf
更新配置文件mysql
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # The MySQL Server configuration file. # # For explanations see # http://dev.mysql.com/doc/mysql/en/server-system-variables.html [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql secure-file-priv= NULL # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Custom config should go here # [必須]啓用二進制日誌 log-bin=mysql-bin # [必須]服務器惟一ID,默認是1 1~255 server-id=1 sql_mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
配置操做redis
啓動mysqlsql
docker-compose up -d
更新配置docker
查看容器shell
docker ps
進入mysql交互數據庫
docker exec -it 518a92715f6f /bin/bash
登陸mysqlbash
mysql -u root -p
配置服務器
#在主庫上建立同步用戶並受權 CREATE USER 'replicate'@'112.74.41.236' IDENTIFIED BY '123456'; GRANT REPLICATION SLAVE ON *.* TO 'replicate'@'112.74.41.236'; FLUSH PRIVILEGES; #最後增長遠程訪問用戶 並賦予全部權限,遠程訪問測試用 CREATE USER antsdouble IDENTIFIED BY '123456'; GRANT ALL ON *.* TO 'antsdouble'@'%'; #用navicate12及以上能夠不用修復修復遠程登陸報報 caching_sha2_password異常 # mysql8 用新的驅動 driver-class-name: com.mysql.cj.jdbc.Driver ALTER USER 'antsdouble'@'%' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER; ALTER USER 'antsdouble'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; FLUSH PRIVILEGES; #查詢master的狀態,此處File,Position數據在配置從庫時用到 show master status;
相關說明網絡
server-id是惟一的,主從不能相同,server-id爲1表示mysq數據庫爲主數據庫,server-id爲2表示mysql的數據庫爲從數據庫
爲什麼必須掛載到/etc/my.cnf 文件上,而不是/etc/mysql/my.cnf 文件上?由於mysql默認配置文件位置在/etc/mysql/my.cnf,掛在方式沒法改變容器中文件內容,my.conf內容不會改變,my.cnf中沒有咱們自定義的配置內容,啓動mysql容器會報錯
lower_case_table_names:忽略表名、列名等數據結構的大小寫(注意:不是每行記錄內容的大小寫!)。 log-bin:開啓二進制記錄。這是爲了主從複製而作的設置。本文使用RBR(Row-Based Replication)模式。 slow_query_log=1:開啓慢查詢日誌。若是某一條SQL執行的時間超過long_query_time設置的秒數,那麼就記錄下來。記錄文件路徑能夠使用show variables;命令,在變量名是slow_query_log_file下查找到具體的日誌文件路徑。 long_query_time=1:單位是秒。指若是某一條SQL語句執行時間超過1秒,就記錄下來。必須開啓慢查詢日誌了之後,此變量才能使用。 log_error:開啓錯誤日誌。show variables like 'log_error'; 就能夠查詢到日誌文件的路徑。mysql的docker官方鏡像若是設置別的取值會致使容器沒法正常啓動。
更新配置文件
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # The MySQL Server configuration file. # # For explanations see # http://dev.mysql.com/doc/mysql/en/server-system-variables.html [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql secure-file-priv= NULL # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Custom config should go here # [必須]啓用二進制日誌 log-bin=mysql-bin # [必須]服務器惟一ID,默認是1 server-id=1 sql_mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
配置操做
進入交互模式,方法同主庫
配置從庫
change master to master_host='112.74.41.236',master_port=3309,master_user='replicate',master_password='123456',master_log_file='binlog.000006',master_log_pos=3862;
說明
master_port:Master的端口號,指的是容器的端口號 master_user:用於數據同步的用戶 master_password:用於同步的用戶的密碼 master_log_file:指定 Slave 從哪一個日誌文件開始複製數據,即上文中提到的 File 字段的值 master_log_pos:從哪一個 Position 開始讀,即上文中提到的 Position 字段的值 master_connect_retry:若是鏈接失敗,重試的時間間隔,單位是秒,默認是60秒
查看
show slave status\G # 查詢slave的狀態,Slave_IO_Running及Slave_SQL_Running進程必須正常運行,即YES狀態,不然都是錯誤的狀態 錯誤能夠在2標記處查看到緣由,也能夠經過 show slave status;
可能產生的緣由
網絡不通 檢查ip,端口 密碼不對 檢查是否建立用於同步的用戶和用戶密碼是否正確 pos不對 檢查Master的 Position
Could not execute Update_rows event on table oa.bui_bill_sum; Can't find record in 'bui_bill_sum', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql-bin.000138, end_log_pos 160051747
解決,臨時
進入sql 執行 STOP SLAVE; SET GLOBAL sql_slave_skip_counter =1; #表示跳過一步錯誤,後面的數字可變 START SLAVE;
永久
在my.cnf中配置 slave-skip-errors = 1032 就會跳過全部的1032的錯誤,多個用逗號分隔
mysql並無從my.cnf文件中更新server_id,既然這樣就只能手動修改了
set global server_id=2; #此處的數值和my.cnf裏設置的同樣就行 slave start;
原文出處:https://www.cnblogs.com/ants_double/p/11763415.html