Skip to content

Commit

Permalink
perf($MySQL): Docker supports MySQL cluster based on binary log
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
johnnymillergh committed Jun 26, 2021
1 parent 6f55581 commit bab526a
Show file tree
Hide file tree
Showing 23 changed files with 209 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ spring:
devtools:
add-properties: true
datasource:
url: jdbc:mysql://maf-mysql-server-development-docker:3306/muscle_and_fitness?useSSL=true&useUnicode=true
url: jdbc:mysql://maf-mysql-server-master-development-docker:3306/muscle_and_fitness?useSSL=true&useUnicode=true
username: maf_mysql_rw
password: maf@mysql
redis:
Expand Down
2 changes: 1 addition & 1 deletion auth-center/src/main/resources/application-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ spring:
devtools:
add-properties: false
datasource:
url: jdbc:mysql://maf-mysql-server-production:3306/muscle_and_fitness?useSSL=true&useUnicode=true
url: jdbc:mysql://maf-mysql-server-master-production:3306/muscle_and_fitness?useSSL=true&useUnicode=true
username: maf_mysql_rw
password: maf@mysql
redis:
Expand Down
2 changes: 1 addition & 1 deletion auth-center/src/main/resources/application-stage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ spring:
devtools:
add-properties: false
datasource:
url: jdbc:mysql://maf-mysql-server-stage:3306/muscle_and_fitness?useSSL=true&useUnicode=true
url: jdbc:mysql://maf-mysql-server-master-stage:3306/muscle_and_fitness?useSSL=true&useUnicode=true
username: maf_mysql_rw
password: maf@mysql
redis:
Expand Down
2 changes: 1 addition & 1 deletion auth-center/src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ spring:
devtools:
add-properties: false
datasource:
url: jdbc:mysql://maf-mysql-server-test:3306/muscle_and_fitness?useSSL=true&useUnicode=true
url: jdbc:mysql://maf-mysql-server-master-test:3306/muscle_and_fitness?useSSL=true&useUnicode=true
username: maf_mysql_rw
password: maf@mysql
redis:
Expand Down
3 changes: 2 additions & 1 deletion docker/.env
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ SPRING_ENVIRONMENT=development-docker
# (also docker container hostnames) #
##################################################
############# Infrastructure Services ############
MYSQL_SERVER_CONTAINER_NAME="maf-mysql-server-${SPRING_ENVIRONMENT}"
MYSQL_SERVER_MASTER_CONTAINER_NAME="maf-mysql-server-master-${SPRING_ENVIRONMENT}"
MYSQL_SERVER_SLAVE_CONTAINER_NAME="maf-mysql-server-slave-${SPRING_ENVIRONMENT}"
REDIS_CONTAINER_NAME="maf-redis-${SPRING_ENVIRONMENT}"
ATMOZ_SFTP_CONTAINER_NAME="maf-atmoz-sftp-${SPRING_ENVIRONMENT}"
MINIO_CONTAINER_NAME="maf-minio-${SPRING_ENVIRONMENT}"
Expand Down
45 changes: 45 additions & 0 deletions docker/config/mysql-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# How to Configure MySQL Cluster

[TOC]

## File Description

These files below are MySQL initialization scripts:

```
script
mysql
init_replication_user.sql
muscle_and_fitness.sql
```

## Useful MySQL command

### On Slave MySQL Server

1. Connect to MySQL

```mysql
mysql -u root -pjm@mysql
```

2. Change master server configuration

```mysql
CHANGE MASTER TO
MASTER_HOST='maf-mysql-server-master-development-docker',
MASTER_PORT=3306,
MASTER_USER='replication_user',
MASTER_PASSWORD='replication_password',
GET_MASTER_PUBLIC_KEY=1,
MASTER_AUTO_POSITION=1;
```

3. Start slave

```mysql
START SLAVE;
```



52 changes: 52 additions & 0 deletions docker/config/mysql-server/master/mysql-master.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[mysqld]

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
secure-file-priv=/var/lib/mysql-files
user=mysql

pid-file=/var/run/mysqld/mysqld.pid

# Database default character set, mainstream character set supports some special emoticons (4 bytes of special emotions)
character-set-server=utf8mb4

# Database character set corresponds to some rules such as sorting,
# pay attention to the corresponding correspondence with CHARACTER-SET-Server
collation-server=utf8mb4_general_ci

# Set the character set when the client is connected to MySQL, prevent garbled binlog_checksum
init_connect='SET NAMES utf8mb4'

# GTID mode https://chanjarster.github.io/post/mysql-master-slave-docker-example/
server_id=1
binlog_format=ROW
log_bin=mysql-bin
gtid_mode=ON
enforce_gtid_consistency=true
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=mysql
52 changes: 52 additions & 0 deletions docker/config/mysql-server/slave-1/mysql-slave.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[mysqld]

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
secure-file-priv=/var/lib/mysql-files
user=mysql

pid-file=/var/run/mysqld/mysqld.pid

# Database default character set, mainstream character set supports some special emoticons (4 bytes of special emotions)
character-set-server=utf8mb4

# Database character set corresponds to some rules such as sorting,
# pay attention to the corresponding correspondence with CHARACTER-SET-Server
collation-server=utf8mb4_general_ci

# Set the character set when the client is connected to MySQL, prevent garbled binlog_checksum
init-connect='SET NAMES utf8mb4'

# GTID mode https://chanjarster.github.io/post/mysql-master-slave-docker-example/
server_id=2
binlog_format=ROW
gtid_mode=ON
enforce_gtid_consistency=true
read-only=ON
skip-log-bin
skip-log-slave-updates
skip-slave-start
76 changes: 40 additions & 36 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ services:
################################################################################
# Infrastructure Services #
################################################################################
mysql-server:
container_name: ${MYSQL_SERVER_CONTAINER_NAME}
mysql-server-master:
container_name: ${MYSQL_SERVER_MASTER_CONTAINER_NAME}
image: mysql/mysql-server:${MYSQL_SERVER_TAG}
environment:
MYSQL_ROOT_HOST: "%"
Expand All @@ -18,9 +18,41 @@ services:
- "3306:3306"
restart: always
volumes:
- "~/docker-file-mapping/${MYSQL_SERVER_CONTAINER_NAME}:/var/lib/mysql"
- ./script/mysql-init-script:/docker-entrypoint-initdb.d
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
- "~/docker-file-mapping/${MYSQL_SERVER_MASTER_CONTAINER_NAME}:/var/lib/mysql"
- ./config/mysql-server/master/mysql-master.cnf:/etc/my.cnf
- ./script/mysql:/docker-entrypoint-initdb.d
command: >
bash -c "
chmod 644 /etc/my.cnf
&& /entrypoint.sh mysqld
"
networks:
muscle_and_fitness_network:

mysql-server-slave:
container_name: ${MYSQL_SERVER_SLAVE_CONTAINER_NAME}
image: mysql/mysql-server:${MYSQL_SERVER_TAG}
depends_on:
mysql-server-master:
condition: service_healthy
environment:
MYSQL_ROOT_HOST: "%"
MYSQL_ROOT_PASSWORD: ${MAF_MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MAF_MYSQL_DATABASE}
MYSQL_USER: ${MAF_MYSQL_USER}
MYSQL_PASSWORD: ${MAF_MYSQL_PASSWORD}
ports:
- "3307:3306"
restart: always
volumes:
- "~/docker-file-mapping/${MYSQL_SERVER_SLAVE_CONTAINER_NAME}:/var/lib/mysql"
- ./config/mysql-server/slave-1/mysql-slave.cnf:/etc/my.cnf
- ./script/mysql:/docker-entrypoint-initdb.d
command: >
bash -c "
chmod 644 /etc/my.cnf
&& /entrypoint.sh mysqld
"
networks:
muscle_and_fitness_network:

Expand Down Expand Up @@ -67,7 +99,7 @@ services:
MINIO_ROOT_PASSWORD: ${MAF_MINIO_ROOT_PASSWORD}
command: server /data
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
test: [ "CMD", "curl", "-f", "http://localhost:9000/minio/health/live" ]
interval: 30s
timeout: 20s
retries: 3
Expand Down Expand Up @@ -227,7 +259,7 @@ services:
hostname: ${SPRING_BOOT_ADMIN_CONTAINER_NAME}
image: ijohnnymiller/muscle-and-fitness-server.spring-boot-admin:${MAF_TAG}
depends_on:
mysql-server:
mysql-server-master:
condition: service_healthy
redis:
condition: service_started
Expand Down Expand Up @@ -342,7 +374,7 @@ services:
hostname: ${MAF_MIS_CONTAINER_NAME}
image: ijohnnymiller/muscle-and-fitness-server.maf-mis:${MAF_TAG}
depends_on:
mysql-server:
mysql-server-master:
condition: service_healthy
redis:
condition: service_started
Expand All @@ -365,34 +397,6 @@ services:
networks:
muscle_and_fitness_network:

muscle-mis:
container_name: ${MUSCLE_MIS_CONTAINER_NAME}
hostname: ${MUSCLE_MIS_CONTAINER_NAME}
image: ijohnnymiller/muscle-and-fitness-server.muscle-mis:${MAF_TAG}
depends_on:
mysql-server:
condition: service_healthy
redis:
condition: service_started
consul-registry:
condition: service_started
rabbitmq:
condition: service_healthy
restart: always
volumes:
- "~/docker-file-mapping/${MUSCLE_MIS_CONTAINER_NAME}:/logs"
environment:
JAVA_TOOL_OPTIONS: ${JAVA_TOOL_OPTIONS}
TZ: ${MAF_TIMEZONE}
healthcheck:
test: "wget localhost:8802/actuator/health -q -O - > /dev/null 2>&1"
interval: 20s
timeout: 5s
retries: 5
start_period: 40s
networks:
muscle_and_fitness_network:

networks:
muscle_and_fitness_network:
ipam:
Expand Down
2 changes: 2 additions & 0 deletions docker/script/mysql/init_replication_user.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CREATE USER 'replication_user'@'%' IDENTIFIED BY 'replication_password';
GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%';
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ spring:
devtools:
add-properties: true
datasource:
url: jdbc:mysql://maf-mysql-server-development-docker:3306/muscle_and_fitness?useSSL=true&useUnicode=true
url: jdbc:mysql://maf-mysql-server-master-development-docker:3306/muscle_and_fitness?useSSL=true&useUnicode=true
username: maf_mysql_rw
password: maf@mysql
redis:
Expand Down
2 changes: 1 addition & 1 deletion maf-mis/src/main/resources/application-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ spring:
devtools:
add-properties: false
datasource:
url: jdbc:mysql://maf-mysql-server-production:3306/muscle_and_fitness?useSSL=true&useUnicode=true
url: jdbc:mysql://maf-mysql-server-master-production:3306/muscle_and_fitness?useSSL=true&useUnicode=true
username: maf_mysql_rw
password: maf@mysql
redis:
Expand Down
2 changes: 1 addition & 1 deletion maf-mis/src/main/resources/application-stage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ spring:
devtools:
add-properties: false
datasource:
url: jdbc:mysql://maf-mysql-server-stage:3306/muscle_and_fitness?useSSL=true&useUnicode=true
url: jdbc:mysql://maf-mysql-server-master-stage:3306/muscle_and_fitness?useSSL=true&useUnicode=true
username: maf_mysql_rw
password: maf@mysql
redis:
Expand Down
2 changes: 1 addition & 1 deletion maf-mis/src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ spring:
devtools:
add-properties: false
datasource:
url: jdbc:mysql://maf-mysql-server-test:3306/muscle_and_fitness?useSSL=true&useUnicode=true
url: jdbc:mysql://maf-mysql-server-master-test:3306/muscle_and_fitness?useSSL=true&useUnicode=true
username: maf_mysql_rw
password: maf@mysql
redis:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ spring:
zipkin:
base-url: http://maf-zipkin-development-docker:9411
datasource:
url: jdbc:mysql://maf-mysql-server-development-docker:3306/muscle_and_fitness?useSSL=true&useUnicode=true
url: jdbc:mysql://maf-mysql-server-master-development-docker:3306/muscle_and_fitness?useSSL=true&useUnicode=true
username: maf_mysql_rw
password: maf@mysql
redis:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ spring:
zipkin:
base-url: http://maf-zipkin-production:9411
datasource:
url: jdbc:mysql://maf-mysql-server-production:3306/muscle_and_fitness?useSSL=true&useUnicode=true
url: jdbc:mysql://maf-mysql-server-master-production:3306/muscle_and_fitness?useSSL=true&useUnicode=true
username: maf_mysql_rw
password: maf@mysql
redis:
Expand Down
2 changes: 1 addition & 1 deletion spring-boot-admin/src/main/resources/application-stage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ spring:
zipkin:
base-url: http://maf-zipkin-stage:9411
datasource:
url: jdbc:mysql://maf-mysql-server-stage:3306/muscle_and_fitness?useSSL=true&useUnicode=true
url: jdbc:mysql://maf-mysql-server-master-stage:3306/muscle_and_fitness?useSSL=true&useUnicode=true
username: maf_mysql_rw
password: maf@mysql
redis:
Expand Down
2 changes: 1 addition & 1 deletion spring-boot-admin/src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ spring:
zipkin:
base-url: http://maf-zipkin-test:9411
datasource:
url: jdbc:mysql://maf-mysql-server-test:3306/muscle_and_fitness?useSSL=true&useUnicode=true
url: jdbc:mysql://maf-mysql-server-master-test:3306/muscle_and_fitness?useSSL=true&useUnicode=true
username: maf_mysql_rw
password: maf@mysql
redis:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ spring:
devtools:
add-properties: true
datasource:
url: jdbc:mysql://maf-mysql-server-development-docker:3306/muscle_and_fitness?useSSL=true&useUnicode=true
url: jdbc:mysql://maf-mysql-server-master-development-docker:3306/muscle_and_fitness?useSSL=true&useUnicode=true
username: maf_mysql_rw
password: maf@mysql
redis:
Expand Down
Loading

0 comments on commit bab526a

Please sign in to comment.