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

MySQL 8.0.17 installation and simple configuration tutorial under macOS

If you don’t understand what I wrote, there may b...

Vue implements a complete process record of a single file component

Table of contents Preface Single file components ...

How to use async await elegantly in JS

Table of contents jQuery's $.ajax The beginni...

Summary of horizontal scrolling website design

Horizontal scrolling isn’t appropriate in all situ...

JS interview question: Can forEach jump out of the loop?

When I was asked this question, I was ignorant an...

How to remove the dotted border when clicking a link in FireFox

I encountered several browser compatibility issue...

Analysis of product status in interactive design that cannot be ignored in design

In the process of product design, designers always...

HTML+css to create a simple progress bar

1. HTML code Copy code The code is as follows: Ex...

JS implements dragging the progress bar to change the transparency of elements

What I want to share today is to use native JS to...

MySQL database operation and maintenance data recovery method

The previous three articles introduced common bac...

Native js implementation of magnifying glass component

This article example shares the specific code for...

Several practical scenarios for implementing the replace function in MySQL

REPLACE Syntax REPLACE(String,from_str,to_str) Th...

How to use the jquery editor plugin tinyMCE

Modify the simplified file size and download the ...