Skip to content

Instantly share code, notes, and snippets.

@fenying
Last active April 3, 2023 06:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fenying/4b95e5f91c08d77ea0447a06cec23c44 to your computer and use it in GitHub Desktop.
Save fenying/4b95e5f91c08d77ea0447a06cec23c44 to your computer and use it in GitHub Desktop.
Run MySQL server by docker with configurations
version: "3"
services:
mysql.loc:
container_name: "mysql.loc"
image: "mysql:5.7"
deploy:
resources:
limits:
cpus: '0.50'
memory: 500M
reservations:
cpus: '0.01'
memory: 50M
volumes:
- ./data:/var/lib/mysql
- ./conf/my.cnf:/etc/mysql/conf.d/my.cnf
network_mode: "host"
environment:
- LANG=C.UTF-8 # Allow input Chinese/Japanses, see https://t.me/funny_codes/20
- MYSQL_ROOT_PASSWORD=mylocaldb
ulimits:
nofile:
soft: 20000 # Prevent from case of infinite error, see https://t.me/funny_codes/52
hard: 40000
#!/bin/bash
# This script will pull down the latest mysql 5.7 and run a container.
LOCAL_MYSQL_ROOT=/docker/mysql/data # The local mysql data directory.
LOCAL_MYSQL_CNF_DIR=/docker/mysql/conf.d # The local mysql configuration directory.
LOCAL_MYSQL_PORT=3306 # The exported port of mysql.
LOCAL_MYSQL_ADDR=0.0.0.0
DOCKER_MAX_MEMORY=512m
DOCKER_MAX_CPUS=1
DOCKER_MYSQL_ROOT=/var/lib/mysql
DOCKER_MYSQL_CNF_DIR=/etc/mysql/conf.d
DOCKER_MYSQL_NAME="mysql-server"
DOCKER_MYSQL_PORT=3306
ROOT_PASSWORD="<password>"
docker pull mysql:5.7
docker run \
-m $DOCKER_MAX_MEMORY \
--cpus=$DOCKER_MAX_CPUS \
--cpuset-cpus="0" \
--name $DOCKER_MYSQL_NAME \
--restart always \
-p$LOCAL_MYSQL_ADDR:$LOCAL_MYSQL_PORT:$DOCKER_MYSQL_PORT \
-v$LOCAL_MYSQL_ROOT:$DOCKER_MYSQL_ROOT \
-v$LOCAL_MYSQL_CNF_DIR:$DOCKER_MYSQL_CNF_DIR \
-e MYSQL_ROOT_PASSWORD="$ROOT_PASSWORD" \
-d mysql:5.7
[client]
default-character-set=utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
default-time-zone = '+08:00'
[mysql]
default-character-set=utf8mb4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment