Home  >  Article  >  Database  >  How docker creates a centos container and downloads MySQL for local connection

How docker creates a centos container and downloads MySQL for local connection

WBOY
WBOYforward
2023-05-31 16:26:41815browse

    1. Pull the image

    docker pull guyton/centos6     #因为是docker命令,所以命令前加docker

    2. Check whether the pull is successful

    docker images   #查看所有镜像

    3. Several methods of creating docker containers

    Method 1: (Recommended for novices) Step by step creation

    #依赖guyton/centos6创建一个名为mycentos_mysql的容器,并存在/bin/bash目录
     
    docker create -it --name mycentos_mysql guyton/centos6 /bin/bash
     
    #查看是否创建成功
    docker ps -a
     
    #启动容器(进入容器前必须启动容器)
    docker start mycentos_mysql
     
    #进入容器
    docker attach mycentos_mysql

    Method 2: (Create and enter the container)

    docker run -it --name mycentos_mysql guyton/centos6

    Method 3: (Create and set up the port directly and then Enter the container) Set up a port to facilitate local connection to MySQL

    docker run -it --name mycentos_mysql -p 3307:3306 guyton/centos6

    4. Download MySQL in the container

    After entering the container, download MySQL. There is a high probability of encountering problems. Solutions will be provided later. Write:

    #下载MySQL
    yum install -y mysql mysql-devel mysql-server
    #报错,下载不了
     
    #更换一下yum源,依次在容器里输入以下代码
    sed -i "s|enabled=1|enabled=0|g" /etc/yum/pluginconf.d/fastestmirror.conf
     
    mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
     
    curl -o /etc/yum.repos.d/CentOS-Base.repo https://www.xmpan.com/Centos-6-Vault-Aliyun.repo
     
    yum clean all && yum makecache
     
    #更换完yum源后,再次尝试下载MySQL,下载成功。

    How docker creates a centos container and downloads MySQL for local connection

    5. Start MySQL and enter MySQL

    #启动 
    service mysqld start
     
    #进入
    mysql -uroot -p

    If a green ok appears, it means the startup is successful. The first time There is no password to enter MySQL. Ignore the prompt to enter the password. Just press Enter and enter

    How docker creates a centos container and downloads MySQL for local connection

    6. Configure permissions

    #复制mysql>后面的代码就可以
     
    mysql> GRANT ALL ON *.* TO root@'%' IDENTIFIED BY '1' WITH GRANT OPTION;
     
    mysql> FLUSH PRIVILEGES;

    Then you can open the local connection Software, such as MySQL, or Navicat Premium

    Enter the IP of the virtual machine and the previously set port number. The username and password are the username and password of the virtual machine, and then Once the connection is successful, you can start using it

    How docker creates a centos container and downloads MySQL for local connection

    The above is the detailed content of How docker creates a centos container and downloads MySQL for local connection. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete