《MySQL系列-主从相关》Docker安装MySQL,实现主从复制

Posted DATA数据猿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《MySQL系列-主从相关》Docker安装MySQL,实现主从复制相关的知识,希望对你有一定的参考价值。

Docker安装mysql,实现主从复制

一、前言

1 Docker安装MySQL

参考历史文章Docker安装MySQL,准备两台MySQL容器

master节点

  • 容器名称

    MySQL01

  • 容器ID

    faf2312fd62a

  • 端口

    33061

slave节点

  • 容器名称

    MySQL01

  • 容器ID

    dfc693c2bb04

  • 端口

    33062

2 MySQL主从复制配置实操

参考历史文章MySQL主从复制配置实操,实现MySQL容器主从配置

二、MySQL01(主节点)

*1 修改主节点配置文件

  • log-bin = mysql-bin

    开启binlog

  • binlog_format = mixed

    指定binlog的格式

  • server-id = 1

    指定节点id

  • innodb_file_per_table = ON

    独立表空间

    在MySQL创建表时,如果设置为ON,则会生成两个文件tablename.frmtablename.idb

    如果设置为OFF,则只会生成一个table.frm文件,此表的数据将存放在idbdata*这个文件中。

  • skip_name_resolve = ON

    优化选项

    禁用DNS解析

[root@zxy_master ~]# docker exec -it faf2312fd62a /bin/bash
bash-4.2# vi /etc/my.cnf

# [mysqld]下添加如下内容
log-bin = mysql-bin
binlog_format = mixed
server-id = 1
innodb_file_per_table = ON
skip_name_resolve = ON

*2 重启主节点容器

修改配置文件后,需要重启容器

[root@zxy_master ~]# docker restart faf2312fd62a
faf2312fd62a

*3 登陆主节点MySQL

直接在主机远程登陆MySQL01容器即可

[root@zxy_master ~]# mysql -uroot -p123456 -h127.0.0.1 -P33061
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 2
Server version: 5.7.40-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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>

4 查看主节点日志

主要查看binlog日志开启状态

mysql> show global variables like '%log%';
+--------------------------------------------+---------------------------------------------+
| Variable_name                              | Value                                       |
+--------------------------------------------+---------------------------------------------+
| back_log                                   | 80                                          |
| binlog_cache_size                          | 32768                                       |
| binlog_checksum                            | CRC32                                       |
| binlog_direct_non_transactional_updates    | OFF                                         |
| binlog_error_action                        | ABORT_SERVER                                |
| binlog_format                              | MIXED                                       |
| binlog_group_commit_sync_delay             | 0                                           |
| binlog_group_commit_sync_no_delay_count    | 0                                           |
| binlog_gtid_simple_recovery                | ON                                          |
| binlog_max_flush_queue_time                | 0                                           |
| binlog_order_commits                       | ON                                          |
| binlog_row_image                           | FULL                                        |
| binlog_rows_query_log_events               | OFF                                         |
| binlog_stmt_cache_size                     | 32768                                       |
| binlog_transaction_dependency_history_size | 25000                                       |
| binlog_transaction_dependency_tracking     | COMMIT_ORDER                                |
| expire_logs_days                           | 0                                           |
| general_log                                | OFF                                         |
| general_log_file                           | /var/lib/mysql/faf2312fd62a.log             |
| innodb_api_enable_binlog                   | OFF                                         |
| innodb_flush_log_at_timeout                | 1                                           |
| innodb_flush_log_at_trx_commit             | 1                                           |
| innodb_locks_unsafe_for_binlog             | OFF                                         |
| innodb_log_buffer_size                     | 16777216                                    |
| innodb_log_checksums                       | ON                                          |
| innodb_log_compressed_pages                | ON                                          |
| innodb_log_file_size                       | 50331648                                    |
| innodb_log_files_in_group                  | 2                                           |
| innodb_log_group_home_dir                  | ./                                          |
| innodb_log_write_ahead_size                | 8192                                        |
| innodb_max_undo_log_size                   | 1073741824                                  |
| innodb_online_alter_log_max_size           | 134217728                                   |
| innodb_undo_log_truncate                   | OFF                                         |
| innodb_undo_logs                           | 128                                         |
| log_bin                                    | ON                                          |
| log_bin_basename                           | /var/lib/mysql/mysql-bin                    |
| log_bin_index                              | /var/lib/mysql/mysql-bin.index              |
| log_bin_trust_function_creators            | OFF                                         |
| log_bin_use_v1_row_events                  | OFF                                         |
| log_builtin_as_identified_by_password      | OFF                                         |
| log_error                                  | stderr                                      |
| log_error_verbosity                        | 3                                           |
| log_output                                 | FILE                                        |
| log_queries_not_using_indexes              | OFF                                         |
| log_slave_updates                          | OFF                                         |
| log_slow_admin_statements                  | OFF                                         |
| log_slow_slave_statements                  | OFF                                         |
| log_statements_unsafe_for_binlog           | ON                                          |
| log_syslog                                 | OFF                                         |
| log_syslog_facility                        | daemon                                      |
| log_syslog_include_pid                     | ON                                          |
| log_syslog_tag                             |                                             |
| log_throttle_queries_not_using_indexes     | 0                                           |
| log_timestamps                             | UTC                                         |
| log_warnings                               | 2                                           |
| max_binlog_cache_size                      | 18446744073709547520                        |
| max_binlog_size                            | 1073741824                                  |
| max_binlog_stmt_cache_size                 | 18446744073709547520                        |
| max_relay_log_size                         | 0                                           |
| relay_log                                  |                                             |
| relay_log_basename                         | /var/lib/mysql/faf2312fd62a-relay-bin       |
| relay_log_index                            | /var/lib/mysql/faf2312fd62a-relay-bin.index |
| relay_log_info_file                        | relay-log.info                              |
| relay_log_info_repository                  | FILE                                        |
| relay_log_purge                            | ON                                          |
| relay_log_recovery                         | OFF                                         |
| relay_log_space_limit                      | 0                                           |
| slow_query_log                             | OFF                                         |
| slow_query_log_file                        | /var/lib/mysql/faf2312fd62a-slow.log        |
| sql_log_off                                | OFF                                         |
| sync_binlog                                | 1                                           |
| sync_relay_log                             | 10000                                       |
| sync_relay_log_info                        | 10000                                       |
+--------------------------------------------+---------------------------------------------+
73 rows in set (0.00 sec)

5 查看主节点的server id

主要查看server_id配置是否生效


mysql> show global variables like '%server%';
+---------------------------------+--------------------------------------+
| Variable_name                   | Value                                |
+---------------------------------+--------------------------------------+
| character_set_server            | latin1                               |
| collation_server                | latin1_swedish_ci                    |
| innodb_ft_server_stopword_table |                                      |
| server_id                       | 1                                    |
| server_id_bits                  | 32                                   |
| server_uuid                     | 94404e36-802b-11ed-a49c-0242ac110004 |
+---------------------------------+--------------------------------------+
6 rows in set (0.00 sec)

*6 创建用户

主节点创建slave用户,给从节点使用

指定IP为localhost即可

mysql> create user 'slave'@'localhost' identified by 'slave';
Query OK, 0 rows affected (0.01 sec)

*7 为用户分配复制权限,并授权从机

给slave用户授予复制权限,并指定IP为主机IP

mysql> grant replication slave,replication client on *.* to 'slave'@'121.4.106.102' identified by 'slave';
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

8 查看主节点日志

mysql> show master logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       864 |
+------------------+-----------+
1 row in set (0.00 sec)

*9 查看主节点状态


mysql> show master status\\G;
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 864
     Binlog_Do_DB:
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)

三、MySQL02(从节点)

*1 修改从节点配置文件

  • relay-log = relay-log

    开启中继日志

  • relay-log-index = relay-log.index

    记录relay-log数据文件

  • server-id = 2

    指定节点id

  • innodb_file_per_table = ON

    独立表空间

    在MySQL创建表时,如果设置为ON,则会生成两个文件tablename.frmtablename.idb

    如果设置为OFF,则只会生成一个table.frm文件,此表的数据将存放在idbdata*这个文件中。

  • skip_name_resolve = ON

    优化选项

    禁用DNS解析

[root@zxy_master ~]# docker exec -it dfc693c2bb04 /bin/bash
bash-4.2# vi /etc/my.cnf

# [mysqld]下添加如下内容
relay-log = relay-log
relay-log-index = relay-log.index
server-id = 2
innodb_file_per_table = ON
skip_name_resolve = ON

*2 重启从节点容器

修改配置文件后,需要重启MySQL02容器

[root@zxy_master ~]# docker restart dfc693c2bb04
dfc693c2bb04

*3 登陆从节点MySQL

[root@zxy_master ~]# mysql -uroot -p123456 -h127.0.0.1 -P33062
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 2
Server version: 5.7.40 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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>

4 查看中继日志

mysql> show global variables like '%log%';
+--------------------------------------------+--------------------------------------+
| Variable_name                              | Value                                |
+--------------------------------------------+--------------------------------------+
| back_log                                   | 80                                   |
| binlog_cache_size                          | 32768                                |
| binlog_checksum                            | CRC32                                |
| binlog_direct_non_transactional_updates    | OFF                                  |
| binlog_error_action                        | ABORT_SERVER                         |
| binlog_format                              | ROW                                  |
| binlog_group_commit_sync_delay             | 0                                    |
| binlog_group_commit_sync_no_delay_count    | 0                                    |
| binlog_gtid_simple_recovery                | ON                                   |
| binlog_max_flush_queue_time                | 0                                    |
| binlog_order_commits                       | ON                                   |
| binlog_row_image                           | FULL                                 |
| binlog_rows_query_log_events               | OFF                                  |
| binlog_stmt_cache_size                     | 32768                                |
| binlog_transaction_dependency_history_size | 25000                                |
| binlog_transaction_dependency_tracking     | COMMIT_ORDER                         |
| expire_logs_days                           | 0                                    |
| general_log                                | OFF                                  |
| general_log_file                           | /var/lib/mysql/dfc693c2bb04.log      |
| innodb_api_enable_binlog                   | OFF                                  |
| innodb_flush_log_at_timeout                | 1                                    |
| innodb_flush_log_at_trx_commit             | 1                                    |
| innodb_locks_unsafe_for_binlog             | OFF                                  |
| innodb_log_buffer_size                     | 16777216                             |
| innodb_log_checksums                       | ON                                   |
| innodb_log_compressed_pages                | ON                                   |
| innodb_log_file_size                       | 50331648                             |
| innodb_log_files_in_group                  | 2                                    |
| innodb_log_group_home_dir                  | ./                                   |
| innodb_log_write_ahead_size                | 8192                                 |
| innodb_max_undo_log_size                   | 1073741824                           |
| innodb_online_alter_log_max_size           | 134217728                            |
| innodb_undo_log_truncate                   | OFF                                  |
| innodb_undo_logs                           | 128                                  |
| log_bin                                    | OFF                                  |
| log_bin_basename                           |                                      |
| log_bin_index                              |                                      |
| log_bin_trust_function_creators            | OFF                                  |
| log_bin_use_v1_row_events                  | OFF                                  |
| log_builtin_as_identified_by_password      | OFF                                  |
| log_error                                  | stderr                               |
| log_error_verbosity                        | 3                                    |
| log_output                                 | FILE                                 |
| log_queries_not_using_indexes              | OFF                                  |
| log_slave_updates                          | OFF                                  |
| log_slow_admin_statements                  | OFF                                  |
| log_slow_slave_statements                  | OFF                                  |
| log_statements_unsafe_for_binlog           | ON                                   |
| log_syslog                                 | OFF                                  |
| log_syslog_facility                        | daemon                               |
| log_syslog_include_pid                     | ON                                   |
| log_syslog_tag                             |                                      |
| log_throttle_queries_not_using_indexes     | 0                                    |
| log_timestamps                             | UTC                                  |
| log_warnings                               | 2                                    |
| max_binlog_cache_size                      | 18446744073709547520                 |
| max_binlog_size                            | 1073741824                           |
| max_binlog_stmt_cache_size                 | 18446744073709547520                 |
| max_relay_log_size                         | 0                                    |
| relay_log                                  | relay-log                            |
| relay_log_basename                         | /var/lib/mysql/relay-log             |
| relay_log_index                            | /var/lib/mysql/relay-log.index       |
| relay_log_info_file                        | relay-log.info                       |
| relay_log_info_repository                  | FILE                                 |
| relay_log_purge                            | ON                                   |
| relay_log_recovery                         | OFF                                  |
| relay_log_space_limit                      | 0                                    |
| slow_query_log                             | OFF                                  |
| slow_query_log_file                        | /var/lib/mysql/dfc693c2bb04-slow.log |
| sql_log_off                                | OFF                                  |
| sync_binlog                                | 1                                    |
| sync_relay_log                             | 10000                                |
| sync_relay_log_info                        | 10000                                |
+--------------------------------------------+--------------------------------------+
73 rows in set (0.00 sec)

5 查看从节点server_id

mysql> show global variables like '%server%';
+---------------------------------+--------------------------------------+
| Variable_name                   | Value                                |
+---------------------------------+--------------------------------------+
| character_set_server            | latin1                               |
| collation_server                | latin1_swedish_ci                    |
| innodb_ft_server_stopword_table |                                      |
| server_id                       | 2                                    |
| server_id_bits                  | 32                                   |
| server_uuid                     | 55ca21cf-8033-11ed-a76b-0242ac110006 |
+---------------------------------+--------------------------------------+
6 rows in set (0.00 sec)

*6 将从节点的master设置为主节点

  • MASTER_HOST

    主节点IP,使用主机IP

  • MASTER_PORT

    主节点Port,使用MySQL01容器对应的外部端口

  • MASTER_USER

    为从节点创建的有复制权限的用户

  • MASTER_PASSWORD

    MASTER_USER对应的用户密码

  • MASTER_LOG_FILE

    binlog文件名,可以通过在主节点执行show master status\\G;查看

  • MASTER_LOG_POS

    binlog文件的postition,可以通过在主节点执行show master status\\G;查看

mysql> CHANGE MASTER TO MASTER_HOST='121.4.106.102' ,MASTER_PORT=33061,MASTER_USER='slave',MASTER_PASSWORD='slave',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=864;
Query OK, 0 rows affected, 1 warning (0.06 sec)

MASTER_LOG_FILE和MASTER_LOG_POS是主节点的值

可以通过在主节点执行show master status\\G;查看

*7 开启slave

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

*8 查看slave状态

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

当这两项都为YES时,表示两台容器已经建立主从连接
mysql> show slave status\\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 121.4.106.102
                  Master_User: zxy
                  Master_Port: 33061
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1893
               Relay_Log_File: relay-log.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: 1893
              Relay_Log_Space: 687
              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: 94404e36-802b-11ed-a49c-0242ac110004
             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:
1 row in set (0.00 sec)

ERROR:
No query specified

四、测试主从

1 主节点

主节点创建数据库zxy,创建表tab1,插入一条数据

## 1.创建数据库
mysql> create database zxy;
Query OK, 1 row affected (0.00 sec)

## 2.进入数据库
mysql> use zxy;
Database changed

## 3.创建表
mysql> create table tab1 (
    -> a int null,
    -> b int null
    -> );
Query OK, 0 rows affected (0.04 sec)

## 4.插入数据
mysql> insert into tab1 values(1,2);
Query OK, 1 row affected (0.02 sec)

2 从节点

从节点可以看到创建数据库zxy命令已经同步过来

## 1.查看数据库已同步
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| zxy                |
+--------------------+
5 rows in set (0.00 sec)

## 2.进入数据库
mysql> use zxy;
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

## 3.查看表已同步
mysql> show tables;
+---------------+
| Tables_in_zxy |
+---------------+
| tab1          |
+---------------+
1 row in set (0.00 sec)

## 4.查看数据已同步
mysql> select * from tab1;
+------+------+
| a    | b    |
+------+------+
|    1 |    2 |
+------+------+
1 row in set (0.00 sec)

以上是关于《MySQL系列-主从相关》Docker安装MySQL,实现主从复制的主要内容,如果未能解决你的问题,请参考以下文章

mycat系列一基于 Docker 搭建 MySQL 主从复制的详细教程

Docker 搭建MySQL5.7 主从复制

Mysql数据库主从搭建-基于docker

《MySQL系列-主从相关》Windows生产服务器和Linux备份服务器实现“主从备份功能“

Redis整合MySQL主从集群

一.Mysql主从复制配置