Centos7.2. Install docker, mysql and redis

environmental information

What is Docker?

Docker is a platform with the functions of developing, publishing and running applications. In the project life cycle, we can use docker to realize rapid delivery, testing and deployment. Docker can run on any platform where docker is installed by packaging the running environment into an image (build once and run everywhere).

Install Docker

  1. centos version 7 or above is recommended. Installing docker in centos 6 is troublesome. If docker has been installed before, you need to clean it first:

    sudo yum remove docker \
                      docker-client \
                      docker-client-latest \
                      docker-common \
                      docker-latest \
                      docker-latest-logrotate \
                      docker-logrotate \
  2. Toolkit required to install docker

    sudo yum install -y yum-utils

    If the following figure is prompted:

    This is because the current system does not have CentOS base Repo file, download one again with the command:

    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  3. Add yum source

    sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 
  4. Install docker

    // You need to enter y to confirm halfway
    sudo yum install docker-ce docker-ce-cli containerd.io
  5. Configure image acceleration address (alicloud)
    5.1 first log in to Alibaba cloud console and select container image service

    5.2 copy command and execute

  6. Configure docker auto start

    systemctl enable docker

Install mysql

We can let docker create and execute the mysql container, but the mysql data cannot only exist in the container. We need to open up an external path to store mysql data.

  1. Install mysql

    docker run -p 3306:3306 --name mysql -v /mydata/mysql/log:/var/log/mysql -v /mydata/mysql/data:/var/lib/mysql -v /mydata/mysql/conf:/etc/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
     For a brief explanation,
     -p 3306:3306: Map the 3306 port of the container to the 3306 port of the host
     -v source address(Host computer):Destination address(container),The of the host/mydata/mysql/log Mount to mysql Container/var/log/mysql catalogue
     -e Specifies the environment variables within the container,Here is the designated mysql Password is root
     -d Run the container in the background and return to the container ID
     mysql:5.7 appoint mysql Version. The specific version can be found in https://hub.docker.com query
  2. Verify that mysql is installed successfully

    docker ps

    As follows, if mysql is displayed, the installation is successful

  3. Enable mysql remote access:

    Enter first mysql container
    docker exec -it mysql /bin/bash
     Sign in mysql,The password is set when we initialize root
    mysql -uroot -p
     Then start turning on remote access
    use mysql;
    select host,user from user;

    Check that the host is not% and need to be updated to% (if it is% skip, directly carry out the following authorization):

    update user set host = '%' where user = 'root';

    Then start authorization

    // This sentence will enable the remote access permission of root. In fact, an account with small permission should be opened according to the scenario
    // Refresh permissions
    flush privileges;
  4. The remote access permission for mysql itself is enabled above. The firewall needs to be configured below. Alibaba cloud also needs to configure the security group on the console to open port 3306.

    # If firewalld is not turned on, turn it on first
    service firewalld start
    #Open 3306 ports
    firewall-cmd --permanent --add-port=3306/tcp;
    #Restart the firewall (restart the firewall after modifying the configuration)
    firewall-cmd --reload

    Finally, configure mysql auto start:

    docker update --restart=always mysql

    At this point, you can use navicat to access MySQL

Install redis

  1. First, create the redis data storage directory and configuration file

    mkdir -p /mydata/redis/conf
    touch /mydata/redis/conf/redis.conf
    // Edit the configuration file to enable persistence
    vi /mydata/redis/conf/redis.conf
    // Add the following command to the configuration file
    appendonly yes
  2. Install redis

    docker run -p 6379:6379 --name redis -v /mydata/redis/data:/data -v /mydata/redis/conf/redis.conf:/etc/redis/redis.conf -d redis redis-server /etc/redis/redis.conf --requirepass "123456"
     For a brief explanation,
     -p 6379:6379: Map the 6379 port of the container to the 6379 port of the host
     -v /mydata/redis/data:/data,The of the host/mydata/redis/data Mount to redis Container/data catalogue
     -v /mydata/redis/conf/redis.conf:/etc/redis/redis.conf,The of the host redis.conf File replacement container redis.conf File, so that we can check it on the host redis Configure
    redis-server /etc/redis/redis.conf,adopt redis.conf function redis
    --requirepass "123456",Set password to 123456

    Add firewall rules to enable redis remote access (Alibaba cloud also needs to configure security groups to open 6379 ports):

    #Open 6379 port
    firewall-cmd --permanent --add-port=6379/tcp;
    #Restart the firewall (restart the firewall after modifying the configuration)
    firewall-cmd --reload

    Configure redis auto start:

    docker update --restart=always redis

Tags: Docker

Posted by Jay_Seagrave on Wed, 04 May 2022 03:53:02 +0300