Docker 部署Mysql主从,本人能力有限,只是把最基本的环境搭建成功,生产环境根据业务实际情况配置,谢谢ing,求点赞,^_^
1、在宿主机上创建mysql的配置文件(主从分别创建对应的目录)
[root@k8sdocker01 mysqlmaster]# ll
total 8
drwxr-xr-x. 2 polkitd input 6 Aug 31 14:47 conf.d
drwxr-xr-x. 6 polkitd input 4096 Sep 1 10:21 data
-rw-r--r--. 1 polkitd input 453 Aug 31 14:38 my.cnf
[root@k8sdocker01 mysqlmaster]#
2、修改mysql配置文件,(请根据实际业务来配置mysql)
[mysqld]
default-storage-engine=INNODB
character-set-server=utf8
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
#bind-address = 127.0.0.1
expire_logs_days = 7
max_connections=200
long_query_time = 1
lower_case_table_names = 1
event_scheduler = ON
server-id = 3310
log-bin = mysql-bin
explicit_defaults_for_timestamp
[client]
default-character-set=utf8
[root@k8sdocker01]# cat ../mysqlslave/my.cnf
[mysqld]
default-storage-engine=INNODB
character-set-server=utf8
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
#bind-address = 127.0.0.1
expire_logs_days = 7
max_connections=200
long_query_time = 1
lower_case_table_names = 1
event_scheduler = ON
server-id = 3311
log-bin = mysqlslave-bin
relay_log=edu-mysql-relay-bin
explicit_defaults_for_timestamp
[client]
default-character-set=utf8
3、使用docker创建mysql容器
docker run --name mysqlslave -d -p 3311:3306 --restart=always -v /app/mysql/mysqlslave/data/:/var/lib/mysql -v /app/mysql/mysqlslave/conf.d:/etc/mysql/conf.d -v /app/mysql/mysqlslave/my.cnf:/etc/mysql/my.cnf -e MYSQL_ROOT_PASSWORD=TgB@12345678 mysql:5.7.18
docker run --name mysqlmaster -d -p 3310:3306 --restart=always -v /app/mysql/mysqlmaster/data/:/var/lib/mysql -v /app/mysql/mysqlmaster/conf.d:/etc/mysql/conf.d -v /app/mysql/mysqlmaster/my.cnf:/etc/mysql/my.cnf -e MYSQL_ROOT_PASSWORD=TgB@12345678 mysql:5.7.18
[root@k8sdocker01 beef]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2387cb862930 mysql:5.7.18 "docker-entrypoint.s…" 19 hours ago Up 30 seconds 0.0.0.0:3311->3306/tcp, :::3311->3306/tcp mysqlslave
433d04524086 mysql:5.7.18 "docker-entrypoint.s…" 19 hours ago Up 30 seconds 0.0.0.0:3310->3306/tcp, :::3310->3306/tcp mysqlmaster
4、配置mysql主从
mysql主库:
CREATE USER 'synchronous'@'%' IDENTIFIED BY 'TgB@12345678';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'synchronous'@'%';
FLUSH PRIVILEGES;
mysql从库:
change master to master_host='192.168.184.139',master_port=3310, master_user='synchronous', master_password='TgB@12345678', master_log_file='mysql-bin.000003', master_log_pos= 781, master_connect_retry=30;
start slave;
5、mysql容器状态检查
[root@k8sdocker01 beef]# docker exec -it mysqlmaster /bin/bash
root@433d04524086:/#
root@433d04524086:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.18-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
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> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.02 sec)
mysql>
[root@k8sdocker01 beef]# docker exec -it mysqlslave /bin/bash
root@2387cb862930:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.18-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
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> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.184.139
Master_User: synchronous
Master_Port: 3310
Connect_Retry: 30
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 154
Relay_Log_File: edu-mysql-relay-bin.000005
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000004
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: 154
Relay_Log_Space: 744
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: 3310
Master_UUID: eca0dda4-0a27-11ec-ae31-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:
1 row in set (0.00 sec)
mysql>