Docker MySQL
The address of the Docker image of MySQL on the dockerhub: https://hub.docker.com/_/mysql(opens new window)
Current (2021-04-02) latest and 8.0.23 Is the same mirror. In addition, 5.7 Version and 5.7.33 Is the same mirror.
# 1. Installation and test
# Query central warehouse docker search -f is-official=true mysql # Download from central warehouse docker pull mysql:8.0.16 # Or, import the existing image file directly # docker load -i mysql-8.0.16.tar # View local mirror docker images # The following will appear: # REPOSITORY TAG IMAGE ID CREATED SIZE # mysql 8.0.16 de764ad211de 2 years ago 443MB
Run container:
# Delete an existing container with the same name docker rm -f mysql-test # Syntax for creating and running mysql containers docker run \ -d \ --name <Specify container name> \ -p <Specify host port>:3306 \ -e MYSQL_ROOT_PASSWORD=<appoint root Login password> \ mysql:8.0.16
For example:
docker run \ -d \ --name mysql-3306 \ -p 3306:3306 \ -e MYSQL_ROOT_PASSWORD=123456 \ mysql:8.0.16 # docker run -d --name mysql-3306 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:8.0.16
Verify that the installation was successful
# View the operation information of the container docker ps # Enter MySQL test container docker exec -it mysql-test /bin/bash # Execute MySQL cli connection command mysql -uroot -p123456
Subsequent optional actions:
-
Remote connection authorization for root
The container is logically equivalent to another computer. In mysql, by default, root can only log in from the computer where MySQL Server is located, and cannot log in from another computer through remote connection.
Of course, this "default" behavior can be configured.
Follow the above command to connect to MySQL Server from inside the container and execute the following SQL statement:
> use mysql; > select user, host from user; > GRANT ALL ON *.* TO 'root'@'%'; > flush privileges;
Copied! -
Connect from outside the container
> mysql -h <Host computer IP> -P <Host port number> -u root -p <root Login password>
Copied!
# 2. Configuration and mounting
-
The data of MySQL Server is stored in the directory container / var/lib/mysql/ , The data you store in MySQL is placed in this directory by mysql.
-
The MySQL Server configuration file is in the container / etc/mysql/my.cnf + / etc/mysql/conf.d/mysql.cnf .
About MySQL configuration files and configuration items:
MySQL Server will be loaded when it is started / etc/mysql/my.cnf Configuration file. There are some configurations in this configuration file. Generally, we will not move this part of the content, nor will we add content to this configuration file.
stay my.cnf At the end of the configuration file, it introduces another directory / etc/mysql/conf.d/ , MySQL will load the configuration information of all. CNF configuration files in this directory. Although you can create a new configuration file under this, MySQL has helped us create a configuration file called mysql.cnf Configuration file (by default, there is nothing in it except comments. It is an empty file). For the configuration items we want to set, we write them here.
Common [mysqld], [client] and [mysql] in configuration files
[mysqld] Represents the default settings for the database itself
[client] Represents the default setting content of the client
[mysql] It represents the default setting when we use MySQL command to connect and log in to MySQL Server
Since the MySQL command itself is also one of the mysql server clients, when you use the MySQL command to connect to the mysql server, [client] and [mysql] Configuration will work.
-
Example
# Delete an existing container with the same name docker rm -f mysql-3306 # Create host mount directory mkdir -p ~/docker/3306/{config,data} # Create mysql.cnf configuration file touch ~/docker/3306/config/mysql.cnf # Create an arbitrarily named `. cnf 'file in the * * conf * * directory of the host computer. See the last for the contents. # Create and run containers docker run \ -d \ --name mysql-3306 \ -v ~/docker/3306/data:/var/lib/mysql \ -v ~/docker/3306/config/mysql.cnf:/etc/mysql/conf.d/mysql.cnf \ -p 3306:3306 \ -e MYSQL_ROOT_PASSWORD=123456 \ mysql:8.0.16
Copied!
# 3. Start in docker compse mode
version: '3' services: mysql-3306: image: mysql:8.0.16 network_mode: "bridge" # Default value container_name: mysql-3306 mem_limit: 512m # Limit docker container memory size environment: - MYSQL_ROOT_PASSWORD=123456 volumes: - /etc/localtime:/etc/localtime:ro # time zone - ~/docker/3306/data:/var/lib/mysql # data - ~/docker/3306/config/mysql.cnf:/etc/mysql/conf.d/mysql.cnf # to configure ports: # network_mode: "host" does not require port mapping - "3306:3306"
Start command: docker compose up - D mysql-3306
# 4. Initialize the database
The docker image of mysql has a function: when it creates a container for the first time (restart does not count), it will be displayed in the container / docker-entrypoint-initdb.d Check whether there is any sql script under the directory. If so, execute the sql script. Therefore, you can use this function to complete the initialization of database creation and so on.
You can create a. SQL script (such as init.sql) in a directory, write SQL statements such as database creation and table creation, and then map this directory to / docker-entrypoint-initdb.d .
#5. mysql.cnf
The default is empty. Let's add something: specify the character set.
[mysqld] character_set_server=utf8 [mysql] default-character-set=utf8
Docker Redis
docker hub website: https://hub.docker.com/_/redis(opens new window)
Current (April 2, 2020) latest Version and 6.2.1 Is the same version.
# 1. Installation and test
# Query central warehouse docker search -f is-official=true redis # Download from central warehouse docker pull redis:6.2.1 # Or, import the existing image file directly # docker load -i redis-6.2.1.tar # View local mirror docker images # Something similar to the following appears: # REPOSITORY TAG IMAGE ID CREATED SIZE # redis 6.2.1 7f33e76fcb56 45 hours ago 105MB
Run container:
# Delete an existing container with the same name docker stop redis-test docker rm redis-test # Syntax for creating and running redis containers docker run -d --name <Specify container name> -p <Specify host port>:6379 redis:6.2.1
For example:
docker run \ -d \ --name redis-test \ -p 6379:6379 \ redis:6.2.1
Verify that the installation was successful
-
adopt docker ps Command to view the operation information of the container:
slightly
-
Enter redis test container
docker exec -it redis-test /bin/bash
Copied! -
implement redis-cli Connect to redis server
redis-cli
Copied!
Subsequent optional actions:
-
Set key value pair
After connecting to the Redis Server, execute the redis command: set test 1, and the OK .
-
Connect from outside the container
redis-cli -h <Host computer IP> -p <Host port> -a <Login password>
Copied!
# 2. Storage of configuration files and data in the container
-
The data storage directory of Docker Redis Server is in the internal directory of the container
/data
Copied!Obviously, only when the persistence function is enabled will there be data stored here.
-
Earlier Docker Redis had a default configuration file in the container, but now it doesn't.
Since version 5, Docker Redis has changed its thinking. The container does not contain configuration files, but requires that a file in the host machine be specified as the configuration file when the container is started.
For example:
docker run \ ... \ -v <On the host redis.conf>:/usr/local/etc/redis/redis.conf \ redis:6.2.1 \ redis-server /usr/local/etc/redis/redis.conf \ ...
Copied!explain
Of course, if you don't provide a configuration file, it still has a default configuration, but you can't find the configuration file in the container.
The valid contents of the default configuration file of 5.0.6 are shown at the end.
# 3. Configuration and mounting
-
Delete an existing container with the same name
docker rm -f redis-6379
Copied!Subsequent adoption docker run of -- rm This option eliminates the "delete" step.
-
Create native mount directory
mkdir -p ~/docker/redis/6379/data touch ~/docker/redis/6379/redis.conf
Copied! -
Create and run containers
docker run \ -d \ --name redis-6379 \ -p 6379:6379 \ -v ~/docker/redis/6379/redis.conf:/usr/local/etc/redis/redis.conf \ -v ~/docker/redis/6379/data:/data \ redis:6.2.1 \ redis-server /usr/local/etc/redis/redis.conf \ --appendonly yes
Copied!--appendonly yes Enable Redis persistence.
# 4. Docker compose startup
version: '3' services: redis: image: redis:5.0 container_name: redis-6379 mem_limit: 512m ports: - 6379:6379 volumes: - "~/docker/6379/data:/data" - "./docker/6379/redis.conf:/usr/local/etc/redis/redis.conf" command: redis-server /usr/local/etc/redis/redis.conf --appendonly yes
#5. redis.conf
bind 127.0.0.1 protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile "" databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir ./ replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no replica-priority 100 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes
Docker Nginx
docker hub website: https://hub.docker.com/_/nginx (opens new window).
Current (2021-04-02) latest Version and 1.19.9 The version is the same version; stable Version and 1.18.0 The version is the same version.
# 1. Installation and test
# Query central warehouse docker search -f is-official=true mysql # Download from central warehouse docker pull nginx:1.18.0 # Or, import the existing image file directly # docker load -i nginx-1.1.18.0.tar # View local mirror docker images
The following will appear:
REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1.18.0 b9e1dc12387a 2 months ago 133MB nginx 1.19.9 7ce4f91ef623 2 days ago 133MB
Run container:
# Delete an existing container with the same name docker stop nginx-test docker rm nginx-test # Syntax for creating and running nginx containers docker run \ -d \ --name <Specify container name> \ -p <Specify host port>:80 \ nginx:1.18.0
For example:
docker run \ -d \ --name nginx-test \ -p 80:80 \ nginx:1.16.1
Verify that the installation was successful
-
adopt docker ps Command to view the operation information of the container:
docker ps
Copied!You will see the following information:
slightly
-
Visit nginx home page
After starting the container, access http: / / host IP:80 You can see the welcome interface of Nginx: Welcome to nginx!
# 2. Storage of configuration files and data in the container
The configuration file of Docker Nginx is in the container
/etc/nginx/nginx.conf
The log directory of Docker Nginx is in the container
/var/log/nginx/
The static resource file directory of Docker Nginx is in the container
/usr/share/nginx/html
# 3. Configuration and mounting
-
Delete an existing container with the same name
docker stop nginx-80 docker rm nginx-80
Copied!Subsequent adoption docker run of -- rm This option eliminates the "delete" step.
-
Create directories and configuration files on the host.
mkdir -p ~/docker/nginx/80/log touch ~/docker/nginx/80/nginx.conf
Copied! -
Create and run containers
docker run \ -d \ --rm \ --name nginx-80 \ -p 80:80 \ -v ~/docker/nginx/80/nginx.conf:/etc/nginx/nginx.conf:ro \ -v ~/docker/nginx/80/log:/var/log/nginx \ nginx:1.18.0
Copied!
The following are the default contents of the nginx configuration file:
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }
# 4. Docker compose startup
version: '3' services: nginx: image: nginx:stable container_name: nginx-zgg volumes: - ~/docker/nginx/80/logs:/etc/nginx/logs - ~/docker/nginx/80/nginx.conf:/etc/nginx/nginx.conf - ~/docker/nginx/80/html:/usr/share/nginx/html ports: - 80:80