diff --git a/auth-center/src/main/resources/application-development-docker.yml b/auth-center/src/main/resources/application-development-docker.yml index 2b98f955..ee3b3345 100644 --- a/auth-center/src/main/resources/application-development-docker.yml +++ b/auth-center/src/main/resources/application-development-docker.yml @@ -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: diff --git a/auth-center/src/main/resources/application-production.yml b/auth-center/src/main/resources/application-production.yml index bb5f825e..05b7ae8e 100644 --- a/auth-center/src/main/resources/application-production.yml +++ b/auth-center/src/main/resources/application-production.yml @@ -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: diff --git a/auth-center/src/main/resources/application-stage.yml b/auth-center/src/main/resources/application-stage.yml index 421936ee..1c82ad8e 100644 --- a/auth-center/src/main/resources/application-stage.yml +++ b/auth-center/src/main/resources/application-stage.yml @@ -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: diff --git a/auth-center/src/main/resources/application-test.yml b/auth-center/src/main/resources/application-test.yml index 6ebc7134..a034608a 100644 --- a/auth-center/src/main/resources/application-test.yml +++ b/auth-center/src/main/resources/application-test.yml @@ -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: diff --git a/docker/.env b/docker/.env index b70239c0..108178d0 100644 --- a/docker/.env +++ b/docker/.env @@ -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}" diff --git a/docker/config/mysql-server/README.md b/docker/config/mysql-server/README.md new file mode 100644 index 00000000..b1d70a51 --- /dev/null +++ b/docker/config/mysql-server/README.md @@ -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; + ``` + + + diff --git a/docker/config/mysql-server/master/mysql-master.cnf b/docker/config/mysql-server/master/mysql-master.cnf new file mode 100644 index 00000000..1815e65f --- /dev/null +++ b/docker/config/mysql-server/master/mysql-master.cnf @@ -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 diff --git a/docker/config/mysql-server/slave-1/mysql-slave.cnf b/docker/config/mysql-server/slave-1/mysql-slave.cnf new file mode 100644 index 00000000..61348e76 --- /dev/null +++ b/docker/config/mysql-server/slave-1/mysql-slave.cnf @@ -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 diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index de7fd12d..30eeb5d2 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -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: "%" @@ -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: @@ -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 @@ -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 @@ -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 @@ -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: diff --git a/docker/script/mysql/init_replication_user.sql b/docker/script/mysql/init_replication_user.sql new file mode 100644 index 00000000..c733c9c3 --- /dev/null +++ b/docker/script/mysql/init_replication_user.sql @@ -0,0 +1,2 @@ +CREATE USER 'replication_user'@'%' IDENTIFIED BY 'replication_password'; +GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%'; diff --git a/docker/script/mysql-init-script/muscle_and_fitness.sql b/docker/script/mysql/muscle_and_fitness.sql similarity index 100% rename from docker/script/mysql-init-script/muscle_and_fitness.sql rename to docker/script/mysql/muscle_and_fitness.sql diff --git a/maf-mis/src/main/resources/application-development-docker.yml b/maf-mis/src/main/resources/application-development-docker.yml index e0e987b4..091478aa 100644 --- a/maf-mis/src/main/resources/application-development-docker.yml +++ b/maf-mis/src/main/resources/application-development-docker.yml @@ -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: diff --git a/maf-mis/src/main/resources/application-production.yml b/maf-mis/src/main/resources/application-production.yml index 3bc03631..eb630758 100644 --- a/maf-mis/src/main/resources/application-production.yml +++ b/maf-mis/src/main/resources/application-production.yml @@ -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: diff --git a/maf-mis/src/main/resources/application-stage.yml b/maf-mis/src/main/resources/application-stage.yml index 9f1182b4..453882ba 100644 --- a/maf-mis/src/main/resources/application-stage.yml +++ b/maf-mis/src/main/resources/application-stage.yml @@ -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: diff --git a/maf-mis/src/main/resources/application-test.yml b/maf-mis/src/main/resources/application-test.yml index 4cef5f4a..a0b5b09e 100644 --- a/maf-mis/src/main/resources/application-test.yml +++ b/maf-mis/src/main/resources/application-test.yml @@ -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: diff --git a/spring-boot-admin/src/main/resources/application-development-docker.yml b/spring-boot-admin/src/main/resources/application-development-docker.yml index 754539e9..d19ec0bd 100644 --- a/spring-boot-admin/src/main/resources/application-development-docker.yml +++ b/spring-boot-admin/src/main/resources/application-development-docker.yml @@ -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: diff --git a/spring-boot-admin/src/main/resources/application-production.yml b/spring-boot-admin/src/main/resources/application-production.yml index 728214e5..2b594ddc 100644 --- a/spring-boot-admin/src/main/resources/application-production.yml +++ b/spring-boot-admin/src/main/resources/application-production.yml @@ -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: diff --git a/spring-boot-admin/src/main/resources/application-stage.yml b/spring-boot-admin/src/main/resources/application-stage.yml index 6815fa02..38349997 100644 --- a/spring-boot-admin/src/main/resources/application-stage.yml +++ b/spring-boot-admin/src/main/resources/application-stage.yml @@ -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: diff --git a/spring-boot-admin/src/main/resources/application-test.yml b/spring-boot-admin/src/main/resources/application-test.yml index 17158005..b38aca99 100644 --- a/spring-boot-admin/src/main/resources/application-test.yml +++ b/spring-boot-admin/src/main/resources/application-test.yml @@ -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: diff --git a/static-resource-center/src/main/resources/application-development-docker.yml b/static-resource-center/src/main/resources/application-development-docker.yml index 29d8b641..6bbb701f 100644 --- a/static-resource-center/src/main/resources/application-development-docker.yml +++ b/static-resource-center/src/main/resources/application-development-docker.yml @@ -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: diff --git a/static-resource-center/src/main/resources/application-production.yml b/static-resource-center/src/main/resources/application-production.yml index 5b1f5b4e..05718045 100644 --- a/static-resource-center/src/main/resources/application-production.yml +++ b/static-resource-center/src/main/resources/application-production.yml @@ -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: diff --git a/static-resource-center/src/main/resources/application-stage.yml b/static-resource-center/src/main/resources/application-stage.yml index 6e5575a6..b337d76d 100644 --- a/static-resource-center/src/main/resources/application-stage.yml +++ b/static-resource-center/src/main/resources/application-stage.yml @@ -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: diff --git a/static-resource-center/src/main/resources/application-test.yml b/static-resource-center/src/main/resources/application-test.yml index 4d5bcdde..171f1d84 100644 --- a/static-resource-center/src/main/resources/application-test.yml +++ b/static-resource-center/src/main/resources/application-test.yml @@ -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: