Using Docker to build MySQL service

1, Install docker

windows and mac can download docker desktop directly to the official website

For linux installation method, please refer to https://www.cnblogs.com/myzone/p/9071210.html

You can check for a successful installation by entering the following command in the shell: sudo docker version

2, Create a mirror

  1. Pull the official image (we choose 5.7 here. If we don't write the later version number, the latest version will be pulled automatically)

    docker pull mysql:5.7 # Pull out mysql 5.7 docker pull mysql # Pull the latest mysql image

    MySQL document address

  2. Check whether the pull is successful

    $ sudo docker images

     

  3. In general, database containers do not need to establish directory mapping

    sudo docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
    
    
    
    –name: Container name, named here mysql
    -e: Configuration information, configure here mysql Of root User's login password
    -p: Port mapping, where the 3306 port of the host is mapped to the 3306 port of the container
    -d: Source image name, here is mysql:5.7

     

  4. If you want to create a directory map

    duso docker run -p 3306:3306 --name mysql \ -v /usr/local/docker/mysql/conf:/etc/mysql \ -v /usr/local/docker/mysql/logs:/var/log/mysql \ -v /usr/local/docker/mysql/data:/var/lib/mysql \ -e MYSQL_ROOT_PASSWORD=123456 \ -d mysql:5.7
    
        
    -v: Directory mapping relationship between host and container,":"Host directory before container directory
  5. Check the container for proper operation

        

docker container ls

    
You can see the container ID, the source image of the container, the start command, the creation time, the state, the port mapping information, and the container name

3, Connect mysql #

  1. Enter docker to connect mysql client locally

    sudo docker exec -it mysql bash mysql -uroot -p123456

     

  2. Using Navicat to connect mysql remotely

    I only found the mac version of Navicat premium (below), not windows and linux. You can find it by yourself.

    Baidu cloud connection Password: qps3 (the software package comes from the famous sharing website Pirate, and thanks to the netizens chaosgod and jor Ivy)

    After installation: copy the Chinese package "zh Hans. Lproj" to / Contents/Resources. (application right click to display package content)

  3. Pay attention to a problem when using remote connection software

    When creating a container, we have mapped the 3306 port of the container and the 3306 port of the host together, so we should visit:

    host: 127.0.0.1 port: 3306 user: root password: 123456

     

  4. If your container is running normally, but you cannot access MySQL, there are several possible reasons:

    • Firewall block

      # Open port: 
      $ systemctl status firewalld 
      $ firewall-cmd  --zone=public --add-port=3306/tcp -permanent 
      $ firewall-cmd  --reload 
      
      # Turn off firewall: 
      $ sudo systemctl stop firewalld

       

    • You need to enter the docker local client to set the remote access account

      $ sudo docker exec -it mysql bash 
      
      $ mysql -uroot -p123456 mysql> grant all privileges on *.* to root@'%' identified by "password";

      Principle:

      # mysql uses the user table in the mysql database to manage permissions. You can modify permissions by modifying the user table (only the root account can modify permissions) 
      
      mysql> use mysql; 
      Database changed 
      
      mysql> select host,user,password from user; +--------------+------+-------------------------------------------+ | host | user | password | +--------------+------+-------------------------------------------+ | localhost | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E | | 192.168.1.1 | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E | +--------------+------+-------------------------------------------+ 2 rows in set (0.00 sec) 
      
      mysql> grant all privileges on *.* to root@'%' identified by "password"; 
      Query OK, 0 rows affected (0.00 sec) 
      
      mysql> flush privileges; 
      Query OK, 0 rows affected (0.00 sec) 
      
      mysql> select host,user,password from user; +--------------+------+-------------------------------------------+ | host | user | password | +--------------+------+-------------------------------------------+ | localhost | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E | | 192.168.1.1 | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E | | % | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E | +--------------+------+-------------------------------------------+ 3 rows in set (0.00 sec)

       

136 original articles published, 133 praised, 520000 visitors+
His message board follow

Tags: MySQL Docker sudo firewall

Posted on Tue, 10 Mar 2020 01:15:42 -0400 by irishred8575