services:
docker-mysql:
container_name: docker-mysql
image: mysql:5.7.44
privileged: true
restart: always
ports:
- "3306:3306"
volumes:
- /opt/mysql/data:/var/lib/mysql
- /home/ota/sql:/home
environment:
TZ: Asia/Shanghai
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: root
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_MAX_BINLOG_SIZE: 1G
MYSQL_EXPIRE_LOGS_DAYS: 3
healthcheck:
test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost", "-proot" ]
interval: 5s
timeout: 10s
retries: 200
start_period: 10s
docker-ota:
container_name: docker-ota
image: docker-ota:20240618161912
ports:
- "9009:9009"
environment:
TZ: Asia/Shanghai
restart: always
logging:
driver: none
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
- "~/.docker:/root/.docker"
- "/home/docker:/home/docker"
- "/home/ota:/home/ota"
- "/usr/bin/docker-compose:/usr/bin/docker-compose"
command: ["java","-jar","-Xmx2048M", "-Xms256M", "ota-service.jar"]
public class MySqlBakShellConstant {
public static String EXEC_SQL_CMD = "docker-compose -f /home/docker/running/docker-compose.yml exec docker-mysql /bin/bash -c 'mysql -uroot -p数据库密码 < %s'";
public static String EXPORT_DATABASE_SQL = "docker-compose -f /home/docker/running/docker-compose.yml exec docker-mysql /bin/bash -c 'mysqldump -uroot -p -p数据库密码 -B 数据库名称 > /home/%s'";
}
@GetMapping(value = "bakSql")
public ResultWrapper bakSql() throws Exception {
Process process = Runtime.getRuntime().exec(String.format(String.format(MySqlBakShellConstant.EXPORT_DATABASE_SQL, "database_bak.sql")));
Integer exitCode = process.waitFor();
return ResultWrapper.success(exitCode);
}
@GetMapping(value = "execSql")
public ResultWrapper execSql() throws Exception {
Process process = Runtime.getRuntime().exec(String.format(MySqlBakShellConstant.EXEC_SQL_CMD, "/home/database_bak.sql"));
Integer exitCode = process.waitFor();
return ResultWrapper.success(exitCode);
}