• Docker(Docker安装redis、mysql)


    1、Docker安装redis

    (1)下载redis

    利用docker pull redis命令下载reids,如果不指定版本的情况下默认是使用的最新版本

    (2)启动redis

    docker run -p 6379:6379
     --name myredis
     -v /usr/local/docker/redis.conf:/etc/redis/redis.conf 
    -v /usr/local/docker/data:/data 
    -d redis redis-server /etc/redis/redis.conf 
    --appendonly yes

    -p 6379:6379 端口映射:前表示主机部分,:后表示容器部分。

    --name :指定该容器名称,操作容器的时候可以使用名称,如退出与开启容器

    -v 挂载目录,规则与端口映射相同,docker是个沙箱隔离级别的容器,这个是它的特点及安全机制,不能随便访问外部(主机)资源目录,所以需要这个挂载目录机制

          redis.conf是redis的配置文件

    -d redis 表示后台启动redis

    --appendonly yes  开启redis 持久化

    (3)查看是否启动成功

    [root@aubin ~]# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
    63a13b7ef4dd        redis               "docker-entrypoint.s猞"   32 seconds ago      Up 29 seconds       0.0.0.0:6379->6379/tcp   myredis

    (4)查看redis容器的日志

    [root@aubin ~]# docker logs myredis
    1:C 23 Oct 2020 01:21:49.446 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    1:C 23 Oct 2020 01:21:49.447 # Redis version=6.0.8, bits=64, commit=00000000, modified=0, pid=1, just started
    1:C 23 Oct 2020 01:21:49.447 # Configuration loaded
    1:M 23 Oct 2020 01:21:49.451 * Running mode=standalone, port=6379.
    1:M 23 Oct 2020 01:21:49.451 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    1:M 23 Oct 2020 01:21:49.451 # Server initialized
    1:M 23 Oct 2020 01:21:49.451 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
    1:M 23 Oct 2020 01:21:49.451 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled (set to 'madvise' or 'never').
    1:M 23 Oct 2020 01:21:49.453 * Ready to accept connections
    1:signal-handler (1603416988) Received SIGTERM scheduling shutdown...
    1:M 23 Oct 2020 01:36:28.443 # User requested shutdown...
    1:M 23 Oct 2020 01:36:28.443 * Calling fsync() on the AOF file.
    1:M 23 Oct 2020 01:36:28.542 # Redis is now ready to exit, bye bye...
    1:C 23 Oct 2020 01:38:18.994 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    1:C 23 Oct 2020 01:38:18.994 # Redis version=6.0.8, bits=64, commit=00000000, modified=0, pid=1, just started
    1:C 23 Oct 2020 01:38:18.994 # Configuration loaded
    1:M 23 Oct 2020 01:38:19.012 * Running mode=standalone, port=6379.
    1:M 23 Oct 2020 01:38:19.012 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    1:M 23 Oct 2020 01:38:19.012 # Server initialized
    1:M 23 Oct 2020 01:38:19.012 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
    1:M 23 Oct 2020 01:38:19.012 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled (set to 'madvise' or 'never').
    1:M 23 Oct 2020 01:38:19.013 * DB loaded from append only file: 0.000 seconds
    1:M 23 Oct 2020 01:38:19.013 * Ready to accept connections

    (5)退出(停止运行中的容器)

    SIGTERM(终止进程)信号后等待一段时间后(默认是10s,可以通过-t 参数来修改),如果从containerd 收到了容器退出消息,那么容器退出成功。如果超过等待的时间之后,还是没收到容器退出的消息,那么docker 将使用docker kill方式试图终止容器

    [root@aubin ~]# docker stop myredis
    myredis

    (6)启动(启动一个或多个已经被停止的容器)

    [root@aubin ~]# docker start myredis
    myredis

    (7)redis连接redis-server

    [root@aubin ~]# docker exec -it myredis redis-cli
    127.0.0.1:6379> set 1 1
    OK
    127.0.0.1:6379> get 1
    "1"

    关闭:

    127.0.0.1:6379> shutdown
    [root@aubin ~]# 

    2、Docker安装mysql

    (1)pull

    [root@aubin tomcat9]# docker pull mysql:5.6
    5.6: Pulling from library/mysql
    babf97a3f00a: Pull complete 
    88031ca211be: Pull complete 
    be1b1d426cba: Pull complete 
    ff53757c5bd8: Pull complete 
    53127b7bc50a: Pull complete 
    e4f269bc946e: Pull complete 
    0c0f95c5641f: Pull complete 
    a6eefa19530d: Pull complete 
    12d1cff28e35: Pull complete 
    79d2fe725964: Pull complete 
    869cdc62084e: Pull complete 
    Digest: sha256:8875725ff152f77e47a563661ea010b4ca9cea42d9dda897fb565ca224e83de2
    Status: Downloaded newer image for mysql:5.6
    docker.io/library/mysql:5.6
    [root@aubin tomcat9]# 

    (2)查看

    [root@aubin tomcat9]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    zhai/centos         latest              8f28e3fd97da        19 hours ago        215MB
    tomcat              latest              625b734f984e        26 hours ago        648MB
    mysql               5.6                 c580203d8753        28 hours ago        302MB
    redis               latest              bd571e6529f3        10 days ago         104MB
    mongo               latest              ba0c2ff8d362        4 weeks ago         492MB
    centos              latest              0d120b6ccaa8        2 months ago        215MB
    hello-world         latest              bf756fb1ae65        9 months ago        13.3kB
    tomcat              8.5.32              5808f01b11bf        2 years ago         463MB
    [root@aubin tomcat9]# 

    (3)启动镜像

    docker run -p 12345:3306 
    --name mysql
    -v /zzyyuse/mysql/conf:/etc/mysql/conf.d
    -v /zzyyuse/mysql/logs:/logs
    -v /zzyyuse/mysql/data:/var/lib/mysql
    -e MYSQL_ROOT_PASSWORD=123456
    -d mysql:5.6
    -p 12345:3306:将主机的12345端口映射到docker容器的3306端口。
    --name mysql:运行服务名字
    -v /zzyyuse/mysql/conf:/etc/mysql/conf.d :将主机/zzyyuse/mysql录下的conf/my.cnf 挂载到容器的 /etc/mysql/conf.d
    -v /zzyyuse/mysql/logs:/logs:将主机/zzyyuse/mysql目录下的 logs 目录挂载到容器的 /logs。
    -v /zzyyuse/mysql/data:/var/lib/mysql :将主机/zzyyuse/mysql目录下的data目录挂载到容器的 /var/lib/mysql 
    -e MYSQL_ROOT_PASSWORD=123456:初始化 root 用户的密码。
    -d mysql:5.6 : 后台程序运行mysql5.6

    查看是否成功运行:

    [root@aubin tomcat9]# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                     NAMES
    89c7173b7764        mysql:5.6           "docker-entrypoint.s猞"   About a minute ago   Up About a minute   0.0.0.0:12345->3306/tcp   mysql

    (4)交互式运行

    [root@aubin tomcat9]# docker exec -it mysql /bin/bash
    root@89c7173b7764:/# 

    (5)登录mysql

    root@89c7173b7764:/# mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 2
    Server version: 5.6.50 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
    
    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> 

    可以进行数据库的操作:

    mysql> show databases
        -> ;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    +--------------------+
    3 rows in set (0.04 sec)
    
    mysql> use mysql
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    mysql> show tables
        -> ;
    +---------------------------+
    | Tables_in_mysql           |
    +---------------------------+
    | columns_priv              |
    | db                        |
    | event                     |
    | func                      |
    | general_log               |
    | help_category             |
    | help_keyword              |
    | help_relation             |
    | help_topic                |
    | innodb_index_stats        |
    | innodb_table_stats        |
    | ndb_binlog_index          |
    | plugin                    |
    | proc                      |
    | procs_priv                |
    | proxies_priv              |
    | servers                   |
    | slave_master_info         |
    | slave_relay_log_info      |
    | slave_worker_info         |
    | slow_log                  |
    | tables_priv               |
    | time_zone                 |
    | time_zone_leap_second     |
    | time_zone_name            |
    | time_zone_transition      |
    | time_zone_transition_type |
    | user                      |
    +---------------------------+
    28 rows in set (0.00 sec)
    
    mysql> 

    (6)sqlYog连接mysql

     在sql中创建数据库并写入数据:

     在docker中的mysql容器中查看数据:

    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | db_test            |
    | mysql              |
    | performance_schema |
    +--------------------+
    4 rows in set (0.00 sec)
    
    mysql> use db_test;
    Database changed
    mysql> show tables;
    +-------------------+
    | Tables_in_db_test |
    +-------------------+
    | test              |
    +-------------------+
    1 row in set (0.00 sec)
    
    mysql> select *
        -> from test;
    +-------+
    | name  |
    +-------+
    | ma    |
    | zhai  |
    | zhang |
    +-------+
    3 rows in set (0.00 sec)
    
    mysql> 
  • 相关阅读:
    python中F/f表达式优于format()表达式
    java8新特性-foreach&lambda
    Java实现多线程的四种方式
    Java中的字符串常量池,栈和堆的概念
    java对象只有值传递,为什么?
    面向对象和面向过程程序设计理解及区别
    String对象为什么不可变
    mybatis_plus插件——生成器
    基于grpc的流式方式实现双向通讯(python)
    Django使用DataTables插件总结
  • 原文地址:https://www.cnblogs.com/zhai1997/p/13862866.html
Copyright © 2020-2023  润新知