Docker 常规安装简介

Docker常规安装简介

欢迎关注我的B站:https://space.bilibili.com/379384819

1. 安装mysql

1.1 docker hub上面查找mysql镜像

网址:

https://hub.docker.com/_/mysql

1.2 从docker hub上(阿里云加速器)拉取mysql镜像到本地标签5.7

[root@localhost docker]# docker pull mysql:5.7

1.3 使用mysql5.7镜像创建容器(也叫运行镜像)

  • 使用mysql镜像
[root@localhost docker]# docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=XXX -d mysql:5.7

需要注意linux下是否本身就已经运行了3306端口,避免出现端口被占用的问题。

[root@localhost docker]# ps -ef|grep mysql

查看docker中的mysql是否已经启动成功

[root@localhost docker]# docker ps

  • 进入mysql容器实例
[root@localhost docker]# docker exec -it 5667939c0687 /bin/bash

进入mysql

root@5667939c0687:/# mysql -uroot -p

使用SQLyog查询数据表是否创建成功,输入虚拟机中的主机地址,并输入数据库密码:XXX,点击测试连接,测试通过后,点击连接即可进入数据库

输入如下数据库语句查询数据表内容

SELECT * FROM t1;

1.4 实战版

  • 新建mysql容器实例(使用容器卷同步数据)

在根目录下创建chenkai文件夹

[chenkai@192 /]$ sudo mkdir chenkai

将之前启动的mysql停止掉

[root@localhost docker]# docker stop 5667939c0687

之前删除容器会导致之前的数据无法进行保存,因此需要将数据保存到容器卷中

[root@192 hostdata]# 
docker run -d -p 3306:3306 --privileged=true -v /chenkai/mysql/log:/var/log/mysql -v /chenkai/mysql/data:/var/lib/mysql -v /chenkai/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=XXX --name mysql mysql:5.7

docker run -d -p 3306:3306 --privileged=true

-v /chenkai/mysql/log:/var/log/mysql

-v /chenkai/mysql/data:/var/lib/mysql

-v /chenkai/mysql/conf:/etc/mysql/conf.d

-e MYSQL_ROOT_PASSWORD=XXX

–name mysql

mysql:5.7

解析:

-e表示环境信息,数据库密码

  • 新建my.cnf

通过容器卷同步给mysql容器实例

[root@192 hostdata]# cd /chenkai/mysql/conf

[root@192 conf]# ls -l
总用量 0

创建my.cnf,并修改

[root@192 conf]# vim my.cnf

输入i进行插入,将如下的内容复制进去

[client]
default_character_set=utf8
[mysqld]
collation_server=utf8_general_ci
character_set_server=utf8

  • 重新启动mysql容器实例,再重新进入并查看字符编码
[root@192 conf]# docker ps
CONTAINER ID   IMAGE       COMMAND                   CREATED       STATUS       PORTS                                                  NAMES
ec1f0d15e2aa   mysql:5.7   "docker-entrypoint.s…"   2 hours ago   Up 2 hours   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql

[root@192 conf]# docker restart mysql
mysql

进入数据库验证数据表创建是否有错误

[root@192 conf]# docker exec -it mysql /bin/bash

root@ec1f0d15e2aa:/# mysql -uroot -p
Enter password: XXX
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.02 sec)

可以看到所有的信息都已经调整为了utf8,接着执行数据库建表操作,按如下操作所示

mysql> create database db01;
Query OK, 1 row affected (0.01 sec)

mysql> use db01;
Database changed

mysql> create table t1(id int,name varchar(20));
Query OK, 0 rows affected (0.02 sec)

mysql> insert into t1 values(1,'z3');
Query OK, 1 row affected (0.02 sec)

接着查询数据表,可以看到相关的内容如下所示,已经可以添加中文字符。

接着将容器删除,重新打开mysql,验证数据是否会被删除,如下图所示

[root@192 conf]# docker rm -f mysql
mysql

[root@192 conf]# docker run -d -p 3306:3306 --privileged=true -v /chenkai/mysql/log:/var/log/mysql -v /chenkai/mysql/data:/var/lib/mysql -v /chenkai/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=XXX --name mysql mysql:5.7
85b3c1137ae86c7f45abc91959bd882df61c61eca3ba2eec377d732beff0ac39

[root@192 conf]# docker ps
CONTAINER ID   IMAGE       COMMAND                   CREATED          STATUS          PORTS                                                  NAMES
85b3c1137ae8   mysql:5.7   "docker-entrypoint.s…"   14 seconds ago   Up 13 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql

[root@192 conf]# docker exec -it mysql /bin/bash

root@85b3c1137ae8:/# mysql -uroot -p
Enter password: XXX

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db01               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use db01;

mysql> select * from t1;
+------+--------+
| id   | name   |
+------+--------+
|    1 | z3     |
|    3 | 王五   |
+------+--------+
2 rows in set (0.00 sec)

2. 安装redis

2.1 入门命令

[root@192 conf]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    9c7a54a9a43c   3 months ago    13.3kB
mysql         5.7       c20987f18b13   19 months ago   448MB
ubuntu        latest    ba6acccedd29   21 months ago   72.8MB
redis         6.0.8     16ecd2772934   2 years ago     104MB

[root@192 conf]# docker run -d -p 6379:6379 redis:6.0.8
e4150c1a09436056850dd7efb19792f3ba2b12f2b4f439b3c28b4c78face1524

[root@192 conf]# docker ps
CONTAINER ID   IMAGE         COMMAND                   CREATED         STATUS         PORTS                                                  NAMES
e4150c1a0943   redis:6.0.8   "docker-entrypoint.s…"   7 seconds ago   Up 5 seconds  
 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp              sweet_faraday
85b3c1137ae8   mysql:5.7     "docker-entrypoint.s…"   6 minutes ago   Up 6 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql

[root@192 conf]# docker exec -it e4150c1a0943 /bin/bash

root@e4150c1a0943:/data# redis-cli

127.0.0.1:6379> set k1 v1
OK

127.0.0.1:6379> get k1
"v1"

可以看到很方便的,redis就已经操作成功,但是在实际应用过程中,需要对于redis数据进行保存,同时需要对于redis的配置文件进行修改,因此还需要进行一定的调整。

为了避免干扰,首先将redis的容器实例删除

[root@192 conf]# docker ps
CONTAINER ID   IMAGE         COMMAND                   CREATED          STATUS          PORTS                                                  NAMES
e4150c1a0943   redis:6.0.8   "docker-entrypoint.s…"   4 minutes ago    Up 4 minutes    0.0.0.0:6379->6379/tcp, :::6379->6379/tcp              sweet_faraday
85b3c1137ae8   mysql:5.7     "docker-entrypoint.s…"   10 minutes ago   Up 10 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql

[root@192 conf]# docker rm -f e4150c1a0943
e4150c1a0943

2.2 命令提醒:容器卷记得加上 - -privileged=true

Docker挂在主机目录访问出现cannot open directory : Permission denied

解决方法:在挂载目录后多加一个—privileged=true

2.3 在CentOS宿主机下新建目录/app/redis

[root@localhost chenkai]# mkdir -p /app/redis

2.4 将一个redis.conf文件模板拷贝进/app/redis目录下

[root@localhost redis]# pwd
/app/redis

[root@localhost redis]# vim redis.conf

打开后添加如下内容

# Redis configuration file example.
#
# Note that in order to read the configuration file, Redis must be
# started with the file path as first argument:
#
# ./redis-server /path/to/redis.conf

# Note on units: when memory size is needed, it is possible to specify
# it in the usual form of 1k 5GB 4M and so forth:
#
# 1k => 1000 bytes
# 1kb => 1024 bytes
# 1m => 1000000 bytes
# 1mb => 1024*1024 bytes
# 1g => 1000000000 bytes
# 1gb => 1024*1024*1024 bytes
#
# units are case insensitive so 1GB 1Gb 1gB are all the same.

################################## INCLUDES ###################################

# Include one or more other config files here.  This is useful if you
# have a standard template that goes to all Redis servers but also need
# to customize a few per-server settings.  Include files can include
# other files, so use this wisely.
#
# Notice option "include" won't be rewritten by command "CONFIG REWRITE"
# from admin or Redis Sentinel. Since Redis always uses the last processed
# line as value of a configuration directive, you'd better put includes
# at the beginning of this file to avoid overwriting config change at runtime.
#
# If instead you are interested in using includes to override configuration
# options, it is better to use include as the last line.
#
# include /path/to/local.conf
# include /path/to/other.conf

################################## MODULES #####################################

# Load modules at startup. If the server is not able to load modules
# it will abort. It is possible to use multiple loadmodule directives.
#
# loadmodule /path/to/my_module.so
# loadmodule /path/to/other_module.so

################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
#    "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode yes

# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379

# TCP listen() backlog.
#
# In high requests-per-second environments you need an high backlog in order
# to avoid slow clients connections issues. Note that the Linux kernel
# will silently truncate it to the value of /proc/sys/net/core/somaxconn so
# make sure to raise both the value of somaxconn and tcp_max_syn_backlog
# in order to get the desired effect.
tcp-backlog 511

# Unix socket.
#
# Specify the path for the Unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#
# unixsocket /tmp/redis.sock
# unixsocketperm 700

# Close the connection after a client is idle for N seconds (0 to disable)
timeout 0

# TCP keepalive.
#
# If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence
# of communication. This is useful for two reasons:
#
# 1) Detect dead peers.
# 2) Take the connection alive from the point of view of network
#    equipment in the middle.
#
# On Linux, the specified value (in seconds) is the period used to send ACKs.
# Note that to close the connection the double of the time is needed.
# On other kernels the period depends on the kernel configuration.
#
# A reasonable value for this option is 300 seconds, which is the new
# Redis default starting with Redis 3.2.1.
tcp-keepalive 300

################################# TLS/SSL #####################################

# By default, TLS/SSL is disabled. To enable it, the "tls-port" configuration
# directive can be used to define TLS-listening ports. To enable TLS on the
# default port, use:
#
# port 0
# tls-port 6379

# Configure a X.509 certificate and private key to use for authenticating the
# server to connected clients, masters or cluster peers.  These files should be
# PEM formatted.
#
# tls-cert-file redis.crt 
# tls-key-file redis.key

# Configure a DH parameters file to enable Diffie-Hellman (DH) key exchange:
#
# tls-dh-params-file redis.dh

# Configure a CA certificate(s) bundle or directory to authenticate TLS/SSL
# clients and peers.  Redis requires an explicit configuration of at least one
# of these, and will not implicitly use the system wide configuration.
#
# tls-ca-cert-file ca.crt
# tls-ca-cert-dir /etc/ssl/certs

# By default, clients (including replica servers) on a TLS port are required
# to authenticate using valid client side certificates.
#
# If "no" is specified, client certificates are not required and not accepted.
# If "optional" is specified, client certificates are accepted and must be
# valid if provided, but are not required.
#
# tls-auth-clients no
# tls-auth-clients optional

# By default, a Redis replica does not attempt to establish a TLS connection
# with its master.
#
# Use the following directive to enable TLS on replication links.
#
# tls-replication yes

# By default, the Redis Cluster bus uses a plain TCP connection. To enable
# TLS for the bus protocol, use the following directive:
#
# tls-cluster yes

# Explicitly specify TLS versions to support. Allowed values are case insensitive
# and include "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3" (OpenSSL >= 1.1.1) or
# any combination. To enable only TLSv1.2 and TLSv1.3, use:
#
# tls-protocols "TLSv1.2 TLSv1.3"

# Configure allowed ciphers.  See the ciphers(1ssl) manpage for more information
# about the syntax of this string.
#
# Note: this configuration applies only to <= TLSv1.2.
#
# tls-ciphers DEFAULT:!MEDIUM

# Configure allowed TLSv1.3 ciphersuites.  See the ciphers(1ssl) manpage for more
# information about the syntax of this string, and specifically for TLSv1.3
# ciphersuites.
#
# tls-ciphersuites TLS_CHACHA20_POLY1305_SHA256

# When choosing a cipher, use the server's preference instead of the client
# preference. By default, the server follows the client's preference.
#
# tls-prefer-server-ciphers yes

# By default, TLS session caching is enabled to allow faster and less expensive
# reconnections by clients that support it. Use the following directive to disable
# caching.
#
# tls-session-caching no

# Change the default number of TLS sessions cached. A zero value sets the cache
# to unlimited size. The default size is 20480.
#
# tls-session-cache-size 5000

# Change the default timeout of cached TLS sessions. The default timeout is 300
# seconds.
#
# tls-session-cache-timeout 60

################################# GENERAL #####################################

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize no

# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
#   supervised no      - no supervision interaction
#   supervised upstart - signal upstart by putting Redis into SIGSTOP mode
#   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
#   supervised auto    - detect upstart or systemd method based on
#                        UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
#       They do not enable continuous liveness pings back to your supervisor.
supervised no

# If a pid file is specified, Redis writes it where specified at startup
# and removes it at exit.
#
# When the server runs non daemonized, no pid file is created if none is
# specified in the configuration. When the server is daemonized, the pid file
# is used even if not specified, defaulting to "/var/run/redis.pid".
#
# Creating a pid file is best effort: if Redis is not able to create it
# nothing bad happens, the server will start and run normally.
pidfile /var/run/redis_6379.pid

# Specify the server verbosity level.
# This can be one of:
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel notice

# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile ""

# To enable logging to the system logger, just set 'syslog-enabled' to yes,
# and optionally update the other syslog parameters to suit your needs.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值