成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

微服務(wù)部署—Docker Compose搭建高可用雙機(jī)熱備MySQL數(shù)據(jù)庫

數(shù)據(jù)庫 MySQL
通常,一般業(yè)務(wù)我們使用云服務(wù)器提供的數(shù)據(jù)庫,無論是MySQL數(shù)據(jù)庫還是其他數(shù)據(jù)庫,云服務(wù)廠商都提供了主備功能,我們不需要自己配置處理。而如果需要我們自己搭建數(shù)據(jù)庫,那么考慮到數(shù)據(jù)的高可用性、故障恢復(fù)和擴(kuò)展性,必須做數(shù)據(jù)備份配置。

一、MySQL 的多種數(shù)據(jù)備份機(jī)制

mysqldump:這是 MySQL 自帶的備份工具,通過導(dǎo)出 SQL 語句來備份數(shù)據(jù)庫。它可以備份整個(gè)數(shù)據(jù)庫、特定表或特定數(shù)據(jù)。使用命令行執(zhí)行 mysqldump 命令可以生成 SQL 文件,然后可以使用該文件還原備份數(shù)據(jù)。備份命令:

# 單個(gè)數(shù)據(jù)庫
mysqldump -h hostname -u username -p dbname > backup.sql
# 多個(gè)數(shù)據(jù)庫
mysqldump -h hostname -u username -p --databases dbname1 dbname2 > backup.sql

MySQL Enterprise Backup:這是 MySQL 官方提供的高級(jí)備份工具【商用收費(fèi)工具】,可用于備份大型數(shù)據(jù)庫。它支持增量備份和并行備份,可以在運(yùn)行時(shí)備份數(shù)據(jù)庫,減少備份期間的停機(jī)時(shí)間。它以block級(jí)別進(jìn)行并行備份,性能大大優(yōu)于邏輯備份工具如mysqldump。

mysqlbackup --host=hostname --user=username --password=password --backup-dir=/path/to/backupdir backup

MySQL Workbench:MySQL Workbench是一種圖形化MySQL管理工具,可以進(jìn)行邏輯備份和還原,支持導(dǎo)出SQL腳本、CSV文件和XML文件等。

物理備份:這種備份方法直接復(fù)制數(shù)據(jù)庫文件,包括數(shù)據(jù)文件、日志文件等。可以使用文件系統(tǒng)級(jí)別的工具,如 rsync 或者文件系統(tǒng)快照功能來備份。

復(fù)制(主從復(fù)制 / 多主復(fù)制):

MySQL主從復(fù)制用于將一個(gè)MySQL服務(wù)器(稱為主服務(wù)器)上的數(shù)據(jù)變更同步到其他MySQL服 務(wù)器(稱為從服務(wù)器)。主從復(fù)制提供了數(shù)據(jù)備份、讀寫分離和負(fù)載均衡等功能,以提高系統(tǒng)的可用性和性能。

MySQL多主復(fù)制允許在多個(gè)數(shù)據(jù)庫實(shí)例之間進(jìn)行雙向數(shù)據(jù)同步。它的工作原理是每個(gè)數(shù)據(jù)庫實(shí)例都可以充當(dāng)主服務(wù)器和從服務(wù)器,可以同時(shí)接收和發(fā)送數(shù)據(jù)變更。

第三方備份工具:還有一些第三方工具可用于備份 MySQL 數(shù)據(jù)庫,例如 Percona XtraBackup、MariaDB Backup 等。這些工具提供了更多高級(jí)特性,如并行備份、壓縮備份等。

MySQL 提供的多種數(shù)據(jù)備份機(jī)制各有優(yōu)缺點(diǎn)和適應(yīng)場(chǎng)景,復(fù)制(主從復(fù)制 / 多主復(fù)制)適合我們的應(yīng)用場(chǎng)景,多主復(fù)制比主從復(fù)制會(huì)更為復(fù)雜一些,需要考慮數(shù)據(jù)沖突等問題。在實(shí)際使用過程中,主主復(fù)制存在很多數(shù)據(jù)沖突的問題需要解決,所以這里我們選擇使用主從備份機(jī)制。

二、MySQL 主從復(fù)制原理

MySQL 主從復(fù)制是一種基于日志的復(fù)制機(jī)制,用于將主服務(wù)器(Master)上的數(shù)據(jù)實(shí)時(shí)復(fù)制到一個(gè)或多個(gè)從服務(wù)器(Slave)。主從復(fù)制的原理如下:

  • 主服務(wù)器將所有修改操作記錄在二進(jìn)制日志(Binary Log)中。這些修改可以是插入、更新或刪除數(shù)據(jù)的操作。
  • 從服務(wù)器連接到主服務(wù)器,并發(fā)送一個(gè)請(qǐng)求,請(qǐng)求成為主服務(wù)器的從服務(wù)器。主服務(wù)器收到請(qǐng)求后,將記錄從服務(wù)器的信息,并開始與從服務(wù)器建立復(fù)制連接。
  • 主服務(wù)器將二進(jìn)制日志中的內(nèi)容發(fā)送給從服務(wù)器。從服務(wù)器接收并執(zhí)行這些日志中的操作,將數(shù)據(jù)修改操作反映到自己的數(shù)據(jù)庫上。
  • 從服務(wù)器還會(huì)定期向主服務(wù)器發(fā)送心跳信息以維持連接。如果主服務(wù)器長(zhǎng)時(shí)間沒有收到從服務(wù)器的心跳信息,就認(rèn)為從服務(wù)器宕機(jī),不再向其發(fā)送日志。
  • 如果主服務(wù)器發(fā)生故障,導(dǎo)致無法提供服務(wù),可以將一個(gè)從服務(wù)器提升為新的主服務(wù)器,以繼續(xù)提供服務(wù)。此時(shí),其他從服務(wù)器將切換到新的主服務(wù)器上進(jìn)行復(fù)制。

通過主從復(fù)制,可以實(shí)現(xiàn)數(shù)據(jù)的實(shí)時(shí)復(fù)制和分布式讀取,提高數(shù)據(jù)庫的可用性和讀取性能。此外,主從復(fù)制還可以用于備份數(shù)據(jù),當(dāng)主服務(wù)器發(fā)生故障時(shí),可以快速切換到從服務(wù)器,減少服務(wù)停機(jī)時(shí)間。
需要注意的是,主從復(fù)制是異步的,從服務(wù)器的數(shù)據(jù)可能稍有延遲。而且主從復(fù)制只復(fù)制數(shù)據(jù)修改操作,不復(fù)制表結(jié)構(gòu)的變更。如果需要同步表結(jié)構(gòu)的變更,可以使用主從復(fù)制搭配其他工具,如 GTID(Global Transaction Identifier)或者基于觸發(fā)器的解決方案。

三、MySQL 主主復(fù)制原理

MySQL主主復(fù)制是一種數(shù)據(jù)同步和高可用性解決方案,它能夠保持多個(gè)MySQL服務(wù)器之間的數(shù)據(jù)一致性。主主復(fù)制的原理如下:

  • 配置雙向復(fù)制:在兩臺(tái)MySQL服務(wù)器上分別配置主從復(fù)制,使每臺(tái)服務(wù)器都可以同時(shí)充當(dāng)主服務(wù)器和從服務(wù)器。
  • 生成二進(jìn)制日志:當(dāng)有數(shù)據(jù)更新操作(如插入、更新、刪除)時(shí),MySQL服務(wù)器會(huì)將這些操作記錄在二進(jìn)制日志中。
  • 傳遞二進(jìn)制日志:每臺(tái)服務(wù)器將自己的二進(jìn)制日志傳遞給另一臺(tái)服務(wù)器。這可以通過網(wǎng)絡(luò)連接實(shí)現(xiàn),通常使用基于TCP/IP的復(fù)制協(xié)議。
  • 應(yīng)用二進(jìn)制日志:每臺(tái)服務(wù)器接收到對(duì)方的二進(jìn)制日志后,會(huì)將這些日志應(yīng)用到自己的數(shù)據(jù)庫中,從而使兩臺(tái)服務(wù)器的數(shù)據(jù)保持一致。
  • 處理沖突:在主主復(fù)制中,由于兩臺(tái)服務(wù)器都可以接收寫操作,可能會(huì)出現(xiàn)沖突。為了處理沖突,MySQL提供了自動(dòng)事務(wù)回滾和主鍵沖突檢測(cè)等機(jī)制。

三、使用Docker Compose安裝MySQL 主從服務(wù)器

1、環(huán)境準(zhǔn)備

首先準(zhǔn)備至少2臺(tái)Linux服務(wù)器,一臺(tái)作為MySQL主服務(wù)器,一臺(tái)或者多臺(tái)作為MySQL從服務(wù)器。我們這里準(zhǔn)備兩臺(tái)服務(wù)器分別為:

  • 192.168.0.210 (MySQL主服務(wù)器)
  • 192.168.0.195 (MySQL從服務(wù)器)

2、準(zhǔn)備MySQL文件存放目錄

  • 準(zhǔn)備數(shù)據(jù)庫存儲(chǔ)目錄,在兩臺(tái)主從服務(wù)器上分別執(zhí)行一下命令。
mkdir -p /opt/container/mysql/data /opt/container/mysql/config /opt/container/mysql/slave/mysql-files 

chmod -R 777 /opt/container/mysql/data /opt/container/mysql/config /opt/container/mysql/slave/mysql-files

/opt/container/mysql/data 用于存放MySQL數(shù)據(jù)文件。
/opt/container/mysql/config 用于存放MySQL配置文件。
/opt/container/mysql/slave/mysql-files 用于存放MySQL數(shù)據(jù)導(dǎo)入/導(dǎo)出的數(shù)據(jù)文件存放目錄。

3、MySQL主、從服務(wù)器docker-compose-mysql.yml文件

version: '3'
services:
    mysql:
        environment:
            ## root賬號(hào)的密碼
            MYSQL_ROOT_PASSWORD: root密碼
            TZ: Asia/Shanghai
            ## 新建mysql賬號(hào)
            MYSQL_USER: 'mysql_user'
            MYSQL_PASSWORD: mysql_user密碼
            MYSQL_DATABASE:  'mysql_db'
        image: "docker.io/mysql:latest" 
        container_name: mysql
        restart: always
        ## 映射掛載
        volumes:
            ## 數(shù)據(jù)目錄,要確保先創(chuàng)建好
            - "/opt/container/mysql/data:/var/lib/mysql"
            - "/opt/container/mysql/config/my.cnf:/etc/mysql/my.cnf"
            - "/opt/container/mysql/slave/mysql-files:/var/lib/mysql-files"
            - "/etc/localtime:/etc/localtime"
            - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
            ## 初始化的腳本,初始化我們存放的init.sql文件
            - "./mysql:/docker-entrypoint-initdb.d/"
        ports:
            - "3306:3306"
        command:
            --max_connections=1000
            --character-set-server=utf8mb4
            --collation-server=utf8mb4_general_ci
            --default-authentication-plugin=mysql_native_password

4、在MySQL主服務(wù)器上新增配置文件

在/opt/container/mysql/config目錄下新增my.cnf文件, 配置文件內(nèi)容:

注意主從服務(wù)器 server-id 一定要配置為不一樣,在這里主服務(wù)器的server-id設(shè)置為150。
[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
!includedir /etc/mysql/conf.d/

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
server-id=150
log-bin=/var/lib/mysql/mysql-bin
expire_logs_days=60
binlog-format=mixed
max_allowed_packet=256M
relay-log=mysql-relay
log-slave-updates
auto_increment_increment=2     #表示自增長(zhǎng)字段每次遞增的量
auto_increment_offset=1     #表示自增長(zhǎng)字段從那個(gè)數(shù)開始

5、在MySQL從服務(wù)器上新增配置文件

在/opt/container/mysql/config目錄下新增my.cnf文件, 配置文件內(nèi)容:

注意主從服務(wù)器 server-id 一定要配置為不一樣,在這里從服務(wù)器的server-id設(shè)置為200,從服務(wù)器需要設(shè)置為read_only = 1只讀模式,這里為了測(cè)試后面的主主復(fù)制,先不設(shè)置,實(shí)際應(yīng)用中一定要設(shè)置。
[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
!includedir /etc/mysql/conf.d/

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
server-id=200
# 設(shè)置只讀模式
# read_only = 1
log-bin=/var/lib/mysql/mysql-bin
expire_logs_days=60
binlog-format=mixed
max_allowed_packet=256M
relay-log=mysql-relay
log-slave-updates
auto_increment_increment=2     #表示自增長(zhǎng)字段每次遞增的量
auto_increment_offset=1     #表示自增長(zhǎng)字段從那個(gè)數(shù)開始

6、在兩臺(tái)MySQL主備服務(wù)器上分別執(zhí)行docker-compose安裝啟動(dòng)命令

將docker-compose-mysql.yml上傳至/opt/software目錄,這個(gè)目錄可以自己選擇,然后到目錄下執(zhí)行安裝啟動(dòng)命令

docker-compose -f docker-compose-mysql.yml up -d
[root@localhost software]# docker-compose -f docker-compose-mysql.yml up -d
[+] Running 13/13
 ? mysql Pulled                                                                                                                                                                                                                            40.4s
   ? 72a69066d2fe Pull complete                                                                                                                                                                                                            14.2s
   ? 93619dbc5b36 Pull complete                                                                                                                                                                                                            14.2s
   ? 99da31dd6142 Pull complete                                                                                                                                                                                                            14.6s
   ? 626033c43d70 Pull complete                                                                                                                                                                                                            14.7s
   ? 37d5d7efb64e Pull complete                                                                                                                                                                                                            14.7s
   ? ac563158d721 Pull complete                                                                                                                                                                                                            16.2s
   ? d2ba16033dad Pull complete                                                                                                                                                                                                            16.2s
   ? 688ba7d5c01a Pull complete                                                                                                                                                                                                            16.2s
   ? 00e060b6d11d Pull complete                                                                                                                                                                                                            24.5s
   ? 1c04857f594f Pull complete                                                                                                                                                                                                            24.5s
   ? 4d7cfa90e6ea Pull complete                                                                                                                                                                                                            24.6s
   ? e0431212d27d Pull complete                                                                                                                                                                                                            24.6s
WARN[0040] Found orphan containers ([nginx]) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up. 
[+] Running 1/1
 ? Container mysql  Started                                                                                                                                                                                                                 0.3s

通過docker ps命令可以看到mysql已經(jīng)安裝并啟動(dòng)成功。

[root@localhost software]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                                                            NAMES
bf4e482dbc71   mysql:latest   "docker-entrypoint.s…"   21 minutes ago   Up 21 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp                             mysql

四、MySQL 主從復(fù)制配置及測(cè)試

1、登錄主MySQL服務(wù)器查看配置數(shù)據(jù)

  • 進(jìn)入docker容器。
[root@localhost software]# docker exec -it bf4e482dbc71 bash
  • 通過用戶名密碼登錄mysql賬戶。
root@bf4e482dbc71:/# mysql -uroot -p密碼
  • 查看需同步的主服務(wù)器數(shù)據(jù) show master status,在MySQL從服務(wù)器上配置時(shí)需要用到File和Position的值。
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |      156 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

2、登錄從MySQL服務(wù)器,并配置數(shù)據(jù)同步

  • 進(jìn)入docker容器。
[root@localhost software]# docker exec -it b168db7981c0 bash
  • 通過用戶名密碼登錄mysql賬戶。
root@bf4e482dbc71:/# mysql -uroot -p密碼
  • 登錄成功后,執(zhí)行從主數(shù)據(jù)庫同步的配置命令。
CHANGE MASTER TO master_host = '192.168.0.210',
 master_port = 3306,
 master_user = 'root',
 master_password = '密碼',
 master_log_file = 'mysql-bin.000003',
 master_log_pos = 156;
  • 啟動(dòng)從服務(wù)器。
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
  • 使用命令 show slave status\G 查看從服務(wù)器狀態(tài), 以下兩項(xiàng)顯示Yes,表示配置成功:
    Slave_IO_Running: YesSlave_SQL_Running: Yes。
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.0.210
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 156
               Relay_Log_File: mysql-relay.000002
                Relay_Log_Pos: 324
        Relay_Master_Log_File: mysql-bin.000003
             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: 156
              Relay_Log_Space: 529
              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: 150
                  Master_UUID: ce0ecbd8-667b-11ee-98e5-0242ac120003
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica 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: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set, 1 warning (0.00 sec)

3、主從數(shù)據(jù)庫復(fù)制測(cè)試

  • 在MySQL主服務(wù)器,新建一個(gè)數(shù)據(jù)庫my_test。
mysql> create database my_test;
Query OK, 1 row affected (0.01 sec)
  • 在MySQL從服務(wù)器,執(zhí)行查看數(shù)據(jù)庫命令,可以看到my_test數(shù)據(jù)庫已經(jīng)同步到MySQL從服務(wù)器。
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| dbwl               |
| information_schema |
| my_test            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)
  • 在MySQL主服務(wù)器,新建一個(gè)表t_test并新增一條數(shù)據(jù)。
mysql> use my_test;
Database changed

mysql> create table t_test(id int UNSIGNED NOT NULL AUTO_INCREMENT, name varchar(32) NOT NULL, PRIMARY KEY (id));
Query OK, 0 rows affected (0.04 sec)

mysql> insert into t_test(name) values('Test');
Query OK, 1 row affected (0.02 sec)
  • 在MySQL從服務(wù)器查詢數(shù)據(jù)庫和表數(shù)據(jù)是否同步,如果配置正常,我們可以看到,在MySQL主服務(wù)器新增的數(shù)據(jù)庫和表數(shù)據(jù),在從服務(wù)器也存在。
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| dbwl               |
| information_schema |
| my_test            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> use my_test;
Database changed
mysql> show tables;
+-------------------+
| Tables_in_my_test |
+-------------------+
| t_test            |
+-------------------+
1 row in set (0.00 sec)

mysql> select * from t_test;
+----+------+
| id | name |
+----+------+
|  1 | Test |
+----+------+
1 row in set (0.00 sec)
請(qǐng)注意,如果是主從復(fù)制,那么一定不要在從服務(wù)器進(jìn)行寫或刪除操作,將從服務(wù)器配置為只讀,否則數(shù)據(jù)將不再進(jìn)行同步。

五、MySQL 主主復(fù)制配置及測(cè)試

MySQL 主主復(fù)制的配置和主從復(fù)制基本一樣,只是需要將原本在從服務(wù)器執(zhí)行的配置命令在主服務(wù)器上再執(zhí)行一遍。

配置主主同步。
  • 在從服務(wù)器上查看狀態(tài) show master status,在MySQL從服務(wù)器上配置時(shí)需要用到File和Position的值
mysql>  show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |     2581 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
  • 在主服務(wù)器上執(zhí)行同步配置命令。
CHANGE MASTER TO master_host = '192.168.0.195',
 master_port = 3306,
 master_user = 'root',
 master_password = '密碼',
 master_log_file = 'mysql-bin.000003',
 master_log_pos = 2581;
  • 在主服務(wù)器上執(zhí)行start slave。
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
  • 使用命令 show slave status\G 查看從服務(wù)器狀態(tài), 以下兩項(xiàng)顯示Yes,表示配置成功:
    Slave_IO_Running: YesSlave_SQL_Running: Yes。
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.0.195
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 2581
               Relay_Log_File: mysql-relay.000002
                Relay_Log_Pos: 324
        Relay_Master_Log_File: mysql-bin.000003
             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: 2581
              Relay_Log_Space: 529
              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: 200
                  Master_UUID: c1d65f95-667e-11ee-bc7f-0242ac120003
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica 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: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set, 1 warning (0.00 sec)
主主數(shù)據(jù)庫復(fù)制測(cè)試,為了方便區(qū)分,下面仍以前面的主/從服務(wù)器命名進(jìn)行區(qū)分。
  • 在MySQL從服務(wù)器,新建一個(gè)數(shù)據(jù)庫my_test_slave
mysql> create database my_test_slave;
Query OK, 1 row affected (0.01 sec)
  • 在MySQL主服務(wù)器,執(zhí)行查看數(shù)據(jù)庫命令,可以看到my_test_slave數(shù)據(jù)庫已經(jīng)同步到MySQL主服務(wù)器
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| dbwl               |
| information_schema |
| my_test            |
| my_test_slave      |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.00 sec)
  • 在MySQL從服務(wù)器的my_test_slave數(shù)據(jù)庫,新建一個(gè)表t_test并新增一條數(shù)據(jù)
mysql> use my_test_slave;
Database changed

mysql> create table t_test(id int UNSIGNED NOT NULL AUTO_INCREMENT, name varchar(32) NOT NULL, PRIMARY KEY (id));
Query OK, 0 rows affected (0.04 sec)

mysql> insert into t_test(name) values('Test');
Query OK, 1 row affected (0.02 sec)
  • 在MySQL主服務(wù)器查詢數(shù)據(jù)庫和表數(shù)據(jù)是否同步,如果配置正常,我們可以看到,在MySQL從服務(wù)器新增的數(shù)據(jù)庫和表數(shù)據(jù),在主服務(wù)器也存在。
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| dbwl               |
| information_schema |
| my_test            |
| my_test_slave      |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.00 sec)

mysql> use my_test_slave;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-------------------------+
| Tables_in_my_test_slave |
+-------------------------+
| t_test                  |
+-------------------------+
1 row in set (0.00 sec)

mysql> select * from t_test;
+----+------+
| id | name |
+----+------+
|  1 | Test |
+----+------+
1 row in set (0.00 sec)
  • 測(cè)試在主數(shù)據(jù)庫插入一條數(shù)據(jù),然后在從數(shù)據(jù)庫查詢,查看是否能夠同步
mysql> insert into t_test(name) values('TestMaster');
Query OK, 1 row affected (0.02 sec)
  • 在從數(shù)據(jù)庫執(zhí)行查詢命令,可以看到數(shù)據(jù)也同步過來了,說明主主復(fù)制生效。
mysql> select * from t_test;
+----+------------+
| id | name       |
+----+------------+
|  1 | Test       |
|  3 | TestMaster |
+----+------------+
2 rows in set (0.00 sec)

很多業(yè)務(wù)場(chǎng)景中,大多數(shù)人使用主主復(fù)制+keepalived來實(shí)現(xiàn)MySQL服務(wù)器的高可用,但是存在很大的問題是處理數(shù)據(jù)沖突問題,可以通過my.cnf中配置,id自增來解決:

auto_increment_increment=2     #表示自增長(zhǎng)字段每次遞增的量
auto_increment_offset=1     #表示自增長(zhǎng)字段從那個(gè)數(shù)開始

在實(shí)際業(yè)務(wù)處理中會(huì)更加復(fù)雜,所以在數(shù)據(jù)庫到底是使用主從復(fù)制還是主主復(fù)制,需要根據(jù)自己的業(yè)務(wù)場(chǎng)景選擇。

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2023-09-08 08:14:14

2010-05-14 16:49:43

MySQL 數(shù)據(jù)庫

2017-06-16 10:39:51

雙機(jī)熱備軟件

2023-10-13 18:57:22

2023-11-13 09:03:10

2023-11-27 07:23:39

2023-06-28 15:56:10

微服務(wù)容器無服務(wù)器計(jì)算

2009-01-09 22:37:43

服務(wù)器系統(tǒng)故障

2022-04-14 11:58:48

移動(dòng)云數(shù)據(jù)庫Redis

2018-05-24 13:51:04

華為云

2024-03-27 12:14:56

數(shù)據(jù)庫高可用GDS

2010-05-12 17:15:57

2009-02-17 19:30:43

2023-09-27 07:15:46

2011-03-09 08:53:02

MySQL優(yōu)化集群

2017-03-15 15:14:03

MySQL數(shù)據(jù)庫高可用性

2013-09-09 09:39:02

云數(shù)據(jù)庫京東云

2009-11-17 10:16:45

2018-01-22 10:05:14

災(zāi)備

2017-05-23 15:53:52

docker服務(wù)容器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 日韩爱爱网站 | 精品国产乱码久久久久久88av | 综合国产第二页 | 日韩欧美在线观看视频 | 国产精品一区二区在线播放 | 国产不卡一区 | 欧美中文字幕一区 | 亚洲精品电影在线观看 | 天天爱天天操 | 国产超碰人人爽人人做人人爱 | 免费亚洲成人 | 精品一区二区观看 | 天天干天天爱天天爽 | 国产一区二区三区久久久久久久久 | 成人免费片| 欧美精品一区二区三区四区 | 久久久久久久一区 | 久久久久久高清 | 精品一区二区久久久久久久网站 | 久久精品色欧美aⅴ一区二区 | 久久9热 | 日韩成人免费视频 | 蜜桃av鲁一鲁一鲁一鲁 | 欧美久久精品一级c片 | 色婷婷久久久亚洲一区二区三区 | 一级久久久久久 | 国产精品国产亚洲精品看不卡15 | 欧美美女爱爱视频 | 在线观看你懂的网站 | 毛片免费观看视频 | 中文字幕在线三区 | 国产激情一区二区三区 | 成人精品国产一区二区4080 | 一级片av | 国产精品久久久久久网站 | 特级一级黄色片 | 日韩视频高清 | 三区四区在线观看 | 午夜成人免费视频 | 久久影音先锋 | 国外成人在线视频网站 |