Docker: MySQL, Redis

2019-06-20

## Installing MySQL

Installing MySQL

1 2 3

  1. Pull the image

docker pull mysql:5.6

  1. Start the container

docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456

  -v $PWD/conf:/etc/mysql/conf.d

  -v $PWD/data:/var/lib/mysql

  -v $PWD/logs:/logs

  --name test_mysql mysql:5.6

  1. Parameter explanation:

-d Runs the container in the background

-p 3306:3306 Maps the container's port 3306 to the host's port 3306

-e Sets an environment variable — here it sets the initial password for MySQL's root user, which is required

-v $PWD/conf:/etc/mysql/conf.d Mounts conf/my.cnf from the host's current directory to /etc/mysql/my.cnf in the container

-v $PWD/data:/var/lib/mysql Mounts the data directory under the host's current directory to /var/lib/mysql in the container

-v $PWD/logs:/logs Mounts the logs directory under the host's current directory to /logs in the container

–name The container's name — it can be anything, but it must be unique

  1. Remote access

On Alibaba Cloud, you need to add a security group rule

Installing Redis

  1. Pull the image

docker pull redis

  1. Create the files

Create a new folder: mkdir redis

Create redis.conf inside that folder

  1. Add the configuration to the config file

The configuration goes between the two lines below

  1. Run the container

docker run -p 6379:6379

  --name redis

  -v $PWD/redis.conf:/root/redis/redis.conf

  -v $PWD/data:/root/redis/data

  -d redis redis-server

  1. Remote access

On Alibaba Cloud, you need to add a security group rule


# Enable daemon mode

daemonize yes

# Set the process lock file

pidfile redis/redis.pid

# Port

port 6379

# Client timeout

timeout 300

# Log level

loglevel debug

# Log file location

logfile redis/log-redis.log

# Set the number of databases. The default database is 0; you can use the SELECT command on a connection to specify the database id

databases 8

## Specifies that after a given number of update operations within a given time, data is synced to the data file; multiple conditions can be combined

#save

# The default Redis configuration file provides three conditions:

save 900 1

save 300 10

save 60 10000

# Specifies whether to compress data when saving to the local database; defaults to yes, using LZF compression. To save CPU time,

# you can turn this option off, but the database file will become huge

rdbcompression yes

# Specifies the local database file name

dbfilename dump.rdb

# Specifies the local database path

dir redis/db/

# Specifies whether to log after every update operation. By default Redis writes to disk asynchronously; if this is not enabled,

# data may be lost for a period during a power failure. Since redis syncs the data file according to the save conditions above,

# some data may exist only in memory for a while

appendonly no

# Specifies the append-log fsync policy; there are 3 possible values:

# no: waits for the OS to sync the data cache to disk (fast)

# always: manually calls fsync() after every update to write data to disk (slow, safe)

# everysec: syncs once per second (a compromise, and the default)

appendfsync everysec

# Configure redis for external network access:

protected-mode no

# Set the password requirepass password