婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > Mysql數(shù)據(jù)庫(kù)的主從同步配置

Mysql數(shù)據(jù)庫(kù)的主從同步配置

熱門標(biāo)簽:云南電商智能外呼系統(tǒng)價(jià)格 拉卡拉外呼系統(tǒng) 話務(wù)外呼系統(tǒng)怎么樣 臨清電話機(jī)器人 外東北地圖標(biāo)注 400電話可以辦理嗎 智能外呼系統(tǒng)復(fù)位 大眾點(diǎn)評(píng)星級(jí)酒店地圖標(biāo)注 高清地圖標(biāo)注道路

Mysql主從同步配置

配置準(zhǔn)備:

  • 需要兩個(gè)數(shù)據(jù)庫(kù)
  • mysql 可視化工具,當(dāng)然使用用命令行也可以
  • 我這里演示使用 docker 啟動(dòng)兩個(gè) mysql 容器, 你也可以安裝兩個(gè) mysql 前提版本一致

1、安裝兩個(gè) mysql

  • 創(chuàng)建 msyql 掛載目錄
[root@localhost /]# mkdir -p /opt/docker/mysql1/conf/
[root@localhost /]# mkdir -p /opt/docker/mysql1/logs/
[root@localhost /]# mkdir -p /opt/docker/mysql1/data/
  • 啟動(dòng)第一個(gè) mysql 掛載對(duì)應(yīng)的文件目錄 port: 6894
[root@localhost /]# docker run -d -p 6894:3306 --name mysql1 \

                    -v /opt/docker/mysql1/conf:/etc/mysql/ \

                    -v /opt/docker/mysql1/logs:/logs \

                    -v /opt/docker/mysql1/data:/var/lib/mysql \

                    --privileged=true \

                    -e MYSQL_ROOT_PASSWORD=qtykGhC29eP4Smp mysql:5.7
  • 通過(guò)拷貝第二個(gè) mysql

需要注意復(fù)的 mysql /opt/docker/mysql2/data/auto.cnf 目錄下有一個(gè) auth.cnf 需要?jiǎng)h除

[root@localhost docker]# cp -r /opt/docker/mysql1/ /opt/docker/mysql2/
  • 刪除 auth.cnf 文件
[root@localhost docker]# rm -f /opt/docker/mysql2/data/auto.cnf
  • 啟動(dòng)第二個(gè) mysql
[root@localhost docker]#  docker run -d -p 6895:3306 --name mysql2 \

                    -v /opt/docker/mysql2/conf:/etc/mysql/ \

                    -v /opt/docker/mysql2/logs:/logs \

                    -v /opt/docker/mysql2/data:/var/lib/mysql \

                    --privileged=true \

                    -e MYSQL_ROOT_PASSWORD=qtykGhC29eP4Smp mysql:5.7

2、編寫mysql配置文件

  • 主庫(kù) my.cnf 文件
[root@localhost docker]# vim /opt/docker/mysql1/conf/my.cnf
  • my.cnf 文件內(nèi)容
[mysqld]
# 主庫(kù)配置
server-id=1 # 服務(wù) id 唯一性
log-bin=mysql1-log # 開啟二進(jìn)制日志 
binlog-format=ROW # 日志記錄模式
replicate-do-db=db_docker # 要復(fù)制的數(shù)據(jù)名稱
# replicate-ignore-db=db_docker # 不需要復(fù)制的數(shù)據(jù)名稱
  • 從庫(kù) my.cnf 文件
[root@localhost docker]# vim /opt/docker/mysql2/conf/my.cnf
  • my.cnf 文件內(nèi)容
[mysqld]
# 從庫(kù)配置
server-id=2   # 服務(wù) id 唯一性
log-bin=mysql2-log  # 開啟二進(jìn)制日志 
binlog-format=ROW # 日志記錄模式 
binlog-do-db=db_docker # 要復(fù)制的數(shù)據(jù)名稱
# binlog-ignore-db=db_docker # 不需要復(fù)制的數(shù)據(jù)名稱
  • 重啟 docker mysql 容器
[root@localhost docker]# docker restart mysql1
[root@localhost docker]# docker restart mysql2

3、初始化數(shù)據(jù)

  • 兩個(gè) mysql 分別執(zhí)行以下 sql 語(yǔ)句創(chuàng)建數(shù)據(jù)庫(kù)創(chuàng)建表
-- 創(chuàng)建數(shù)據(jù)庫(kù)
CREATE DATABASE `db_docker`;
USE db_docker;
-- 創(chuàng)建表
CREATE TABLE `t_docker` (
    `id` INT ( 11 ) NOT NULL AUTO_INCREMENT,
    `name` VARCHAR ( 255 ) DEFAULT NULL,
     PRIMARY KEY ( `id` )
) ENGINE = INNODB AUTO_INCREMENT =0 DEFAULT CHARSET = utf8;

查看主庫(kù)二進(jìn)制日志:

  • 主庫(kù)執(zhí)行以下命令輸出二進(jìn)制日志文件的狀態(tài)信息
mysql> SHOW MASTER STATUS ;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |     2223 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

配置從庫(kù)二進(jìn)制日志

  • 從庫(kù)更改用于連接到復(fù)制主庫(kù)、讀取主庫(kù)的二進(jìn)制日志和讀取從庫(kù)的中繼日志的參數(shù)
CHANGE MASTER TO
MASTER_HOST="192.168.101.59", # 主機(jī)地址 你的主服務(wù)器 ip
Master_Port=6894, # 端口
MASTER_USER="root", # 賬號(hào)
MASTER_PASSWORD="qtykGhC29eP4Smp", # 密碼
MASTER_LOG_FILE="mysql-bin.000001", # 主庫(kù)二進(jìn)制文件名 根據(jù)實(shí)際情況填寫
MASTER_LOG_POS=377; # 主庫(kù)二進(jìn)制文件位置 根據(jù)實(shí)際情況填寫
  • 從庫(kù)執(zhí)行,啟動(dòng)復(fù)制
mysql> START SLAVE;

從庫(kù)線程的基本參數(shù)的狀態(tài)信息。從 MySQL 8.0.22 開始,使用 SHOW REPLICA STATUS代替 SHOW SLAVE STATUS,該版本已棄用。在 MySQL 8.0.22 之前的版本中,使用SHOW SLAVE STATUS. 該語(yǔ)句需要REPLICATION CLIENT特權(quán)(或已棄用的 SUPER特權(quán))。

以下兩參數(shù)為 yes 表示配置成功,否則配置有問(wèn)題。 Slave_IO_Running: Yes Slave_SQL_Running: Yes

如果以上兩個(gè)參數(shù)有一個(gè)未 No 說(shuō)明有錯(cuò)誤,請(qǐng)查看這個(gè)兩個(gè)字段 Last_Errno Last_Error

錯(cuò)誤內(nèi)容會(huì)記錄在 Last_Error 這個(gè)字段中,根據(jù)錯(cuò)誤內(nèi)容修改。

mysql>  SHOW SLAVE STATUS \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.101.59
                  Master_User: root
                  Master_Port: 6894
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 2223
               Relay_Log_File: 98394ee2fb48-relay-bin.000004
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             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: 2223
              Relay_Log_Space: 534
              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_UUID: aa58ab20-f500-11eb-aa65-0242ac110002
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version:

4、其他mysql 相關(guān)命令

  • 停止主庫(kù)的數(shù)據(jù)更新操作
mysql>flush tables with read lock;
  • 主庫(kù)解鎖
mysql>unlock tables;
  • 從庫(kù)執(zhí)行,停止復(fù)制; 當(dāng)從庫(kù)配置二進(jìn)制日志出錯(cuò)時(shí),需要停止復(fù)制或重置,再重新配置,讓后啟動(dòng)復(fù)制
mysql> STOP SLAVE;
  • 從庫(kù)執(zhí)行,重置復(fù)制
mysql> RESET SLAVE;

mysql 注意事項(xiàng)

  • 版本不同對(duì)應(yīng)的命令和配置文件可能存在差異
  • 演示使用的是 mysql 5.7
  • 如果命令或配置不起作用 詳情官網(wǎng)
  • 在實(shí)際應(yīng)用中盡量不要使用默認(rèn)端口 3306 容易被攻擊

mysql 容器:

進(jìn)入容器

docker exec -it mysql2 /bin/sh
# mysql2 容器名稱 這里也可以是容器 id

登錄 mysql

mysql -u root -pqtykGhC29eP4Smp
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.35-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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.
mysql>

my.cnf 配置解釋

[mysqld]
# 主庫(kù)配置
# 指定一個(gè)唯一的服務(wù)器 ID,可以為0但是服務(wù)器會(huì)拒絕,所以有效值 1~4294967295 之間。默認(rèn)值1
# 官網(wǎng) https://dev.mysql.com/doc/refman/5.7/en/replication-options.html
server-id=1
# 開啟二進(jìn)制日志,二進(jìn)制文件名稱,可以是路徑 例如: /logs/mysql/log 但是需要注意給目錄文件提權(quán),否則mysql可以無(wú)權(quán)寫入,導(dǎo)致報(bào)錯(cuò)。
log-bin=mysql1-log
# 日志記錄模式 有三種 
# STATEMENT 導(dǎo)致日志記錄基于語(yǔ)句。
# ROW 導(dǎo)致日志記錄基于行。這是默認(rèn)設(shè)置。
# MIXED 導(dǎo)致日志記錄使用混合格式。介于 前兩種模式之間
# 官網(wǎng) https://dev.mysql.com/doc/refman/5.7/en/binary-log-setting.html
binlog-format=ROW 
# 要復(fù)制的數(shù)據(jù)名稱,要指定多個(gè)數(shù)據(jù)庫(kù),您必須使用此選項(xiàng)的多個(gè)實(shí)例。
# 由于數(shù)據(jù)庫(kù)名稱可以包含逗號(hào),如果您提供逗號(hào)分隔列表,則該列表將被視為單個(gè)數(shù)據(jù)庫(kù)的名稱。
# 多個(gè)實(shí)例:
# replicate-do-db=db_docker1 
# replicate-do-db=db_docker2 
replicate-do-db=db_docker 
# 不需要復(fù)制的數(shù)據(jù)名稱,配置同上
# replicate-ignore-db=db_docker # 不需要復(fù)制的數(shù)據(jù)名稱

[mysqld]
# 從庫(kù)配置同上 舉一反三
server-id=2   # 服務(wù) id 唯一性
log-bin=mysql2-log  # 開啟二進(jìn)制日志 
binlog-format=ROW # 日志記錄模式 
binlog-do-db=db_docker # 要復(fù)制的數(shù)據(jù)名稱
# binlog-ignore-db=db_docker # 不需要復(fù)制的數(shù)據(jù)名稱

auth.cnf 文件

文件內(nèi)容 server-uuid

  • 這里 UUID 也是必須唯一,啟動(dòng)的時(shí)候會(huì)自動(dòng)生成。如果您也是通過(guò)復(fù)制 mysql data 目錄創(chuàng)建的數(shù)據(jù)庫(kù)需要?jiǎng)h除該文件
[auto]
server-uuid=aa58ab20-f500-11eb-aa65-0242ac110002
  • UUID相同會(huì)報(bào)以下錯(cuò)誤
Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.

官網(wǎng)二進(jìn)制日志配置:https://dev.mysql.com/doc/refman/5.7/en/replication-options-binary-log.html

到此這篇關(guān)于Mysql主從同步配置詳情的文章就介紹到這了,更多相關(guān)Mysql主從同步配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 解決MySQL主從數(shù)據(jù)庫(kù)沒(méi)有同步的兩種方法
  • 一文帶你了解Mysql主從同步原理
  • Docker 環(huán)境運(yùn)行 Mysql 和開啟 Binlog 配置主從同步的設(shè)置方法
  • MySQL 主從同步,事務(wù)回滾的實(shí)現(xiàn)原理
  • MySQL數(shù)據(jù)庫(kù)主從同步實(shí)戰(zhàn)過(guò)程詳解
  • MySQL主從同步中的server-id示例詳解
  • MySQL數(shù)據(jù)庫(kù)的主從同步配置與讀寫分離
  • MySQL主從同步原理及應(yīng)用

標(biāo)簽:阿里 福州 山西 揚(yáng)州 無(wú)錫 定西 三明 溫州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Mysql數(shù)據(jù)庫(kù)的主從同步配置》,本文關(guān)鍵詞  Mysql,數(shù)據(jù)庫(kù),的,主從,同步,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Mysql數(shù)據(jù)庫(kù)的主從同步配置》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Mysql數(shù)據(jù)庫(kù)的主從同步配置的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 缙云县| 宝鸡市| 台中县| 囊谦县| 哈尔滨市| 开封县| 双峰县| 日土县| 泽库县| 抚宁县| 塔城市| 合阳县| 柘荣县| 乐清市| 阿图什市| 雅江县| 青龙| 桓台县| 横峰县| 阿尔山市| 资兴市| 鄱阳县| 康定县| 丹棱县| 麦盖提县| 绥中县| 当涂县| 宁远县| 饶河县| 集贤县| 兴海县| 西峡县| 阿拉尔市| 汉源县| 鞍山市| 东莞市| 房山区| 武清区| 正镶白旗| 冕宁县| 浏阳市|