Methods for deploying MySQL services in Docker and the pitfalls encountered

Methods for deploying MySQL services in Docker and the pitfalls encountered

I have been learning porters recently. I feel like I didn’t know about such a powerful thing before. I have recorded the process of tinkering with it for my classmates to refer to.

Step 0: Pull the official MySQL image from Docker Hub

docker pull mysql

Then it's a long wait. Of course, if you configure a mirror accelerator, the speed will be a little faster.

Step 1: Use the docker images command to view the image

You will see that we already have a mirror of MySQL here

Step 2: Start our MySQL image and create a MySQL container

Use command: docker run -d --name mysql -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql

Explain the parameters here:

-d means running in the background and not exiting with the current command line window

--name gives the container an alias, which can be used to manage the container in the future

-p 3307: 3307 maps the host's port 3307 to the MySQL container's port 3306

-e MySQL container environment configuration

MYSQL_ROOT_PASSWORD=123456 specifies the password of mysql. The default username is root. Note that if no password is specified, startup will fail.

Step 3: View the mysql container we have started

Use command: docker ps

As you can see, our MySQL container is already running. Docker assigns a container number to the MySQL container for easy management. It also displays the port mapping we set.

At this time, some brothers may think, although the MySQL container is running happily, you only tell us the port, how can we know its IP? I don’t believe you, you are a bad old man.

No no no. We can use docker inspect -f = '{{. Use the NetworkSettings.IPAddress}}'5fef288f221f command to view the IP address of the container. Note that you can directly write the ID of the container you want to view at the end. Those people on the Internet are very bad and will add a <> to you, which will make you very depressed. Just follow my method and you will be right.

Another thing to note is that if you want to connect to our Mysql container externally for remote management, you need to configure the host of the mysql root account in the container and change it to a wildcard %, so that any host can connect to our MySQL. The specific method is as follows:

Enter the MySQl container: Use the docker exec command, -it is a parameter, and bash means creating an interactive interface

Log in to MySQL server: Use the root user to log in to MySQL. After entering the password, we can see that we have entered MySQL.

Use the show database; command to view the database (be careful not to forget the final semicolon, all MySQL commands must have a semicolon)

As you can see, our databases are listed, and then use the mysql; command to enter this MySQL database (is it a bit confusing? Hahaha, the MySQL database here refers to this database, okay, I may still not explain it clearly)

Then use the show tables; command to list all tables

As you can see, there are many tables. These are all MySQL configurations. Don't pay attention to them. We only need to modify one user table.

Use sql command: update user set host ='%'where user ='root';

Some students may get an error with this command because your MySQL may have multiple root users, so use the following command

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

After configuring the above steps, you can test the connection. If you can connect, congratulations, you are lucky.

If you can't connect, congratulations, because the MySQL image you downloaded is mysql8.

You may encounter the following error

At this point, the configuration is complete, use the exit; command to exit.

Testing Remote Connection

Step 4: Import data into our MySQL container

Although our MySQL container is running, there is no data in it. You can import the database to MySQL in docker by the following method:

First import the file into the container. cp is followed by the path of the SQL file you want to import.

#docker cp **.sql mysql:/root/
Enter the container#docker exec -it mysql bash
Import the file into the database# mysql -uroot -p [database name] < ***.sql

mysql -h localhost -u root -p (enter mysql)
create database abc; (create database)
show databases; (You can see all existing databases and the database abc just created)
use abc;(enter the abc database)
show tables; (show all tables under the abc database, empty)
source /var/test.sql (import database table)
show tables; (view all tables under the abc database, and you can see the table)
desc pollution;(see table structure design)
select * from pollution;
exit (or ctrl + c) to exit mysql

Summarize

The above is the method of deploying MySQL service in Docker introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Detailed explanation of using MySQL database in docker (access in LAN)
  • Solution to failure in connecting to mysql in docker
  • How to install MySQL 8.0 in Docker
  • Detailed explanation of importing/exporting MySQL data in Docker container
  • Install and run a MySQL instance on Docker
  • A practical record of a docker login mysql error problem

<<:  Complete steps for using Echarts and sub-packaging in WeChat Mini Program

>>:  MySQL database master-slave configuration tutorial under Windows

Recommend

Two ways to manage volumes in Docker

In the previous article, I introduced the basic k...

Let's talk briefly about the changes in setup in vue3.0 sfc

Table of contents Preface Standard sfc writing me...

MySQL 8.0.12 winx64 decompression version installation graphic tutorial

Recorded the installation of mysql-8.0.12-winx64 ...

How to configure static network connection in Linux

Configuring network connectivity for Linux system...

Detailed explanation of server-id example in MySQL master-slave synchronization

Preface When we build a MySQL cluster, we natural...

A brief discussion of the interesting box model of CSS3 box-sizing property

Everyone must know the composition of the box mod...

How to delete a property of an object in JavaScript

1. delete delete is the only real way to remove a...

The hottest trends in web design UI in 2013 The most popular UI designs

Time flies, and in just six days, 2013 will becom...

docker logs - view the implementation of docker container logs

You can view the container logs through the docke...

js to achieve the effect of dragging the slider

This article shares the specific code of how to d...

Detailed example of MySQL subquery

Subquery Classification Classification by returne...

Example code of setting label style using CSS selector

CSS Selectors Setting style on the html tag can s...

MySql 8.0.11 installation and configuration tutorial

Official website address: https://dev.mysql.com/d...

Several principles for website product design reference

The following analysis is about product design pr...