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.frm
和tablename.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 | ./