Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of deploying mysql services in Docker

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian for you to introduce in detail "what is the method of deploying mysql services in Docker", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what is the method of deploying mysql services in Docker" can help you solve your doubts.

Step 0: pull the official mysql image from docker hub

Docker pull mysql

And then there's a long wait, and of course, if you configure a mirror accelerator, it will be fast and lost.

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

You will see that we already have a mirror image of mysql.

Step 2: start our mysql image and create a mysql container

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

Explain the parameters here:

-d means to run in the background and does not exit with the exit of the current command line window

-- name gives the container an alias through which it can be managed in the future

-p 3307 3307 maps port 3307 of the host to port 3306 of the mysql container

-e mysql container environment configuration

Mysql_root_password=123456 specifies the password of mysql. The user name defaults to root. Note that if no password is specified, startup will fail.

Step 3: check the mysql container that we have started

Use the command: docker ps

As you can see, our mysql's container is already running. Dockeer assigns a container number to the mysql's container, which is convenient for us to manage, and also shows the port mapping we set.

At this time, some brothers may think, although the container of mysql is running happily, but you only tell us the port, how do we know its ip, I believe your old man is very bad.

No no no. We can use docker inspect-f ='{. Networksettings.ipaddress}} '5fef288f221f command to check the ip of the container, notice that you can write the id of the container you want to view directly at the end. Those people on the Internet are very bad, and I will add one to you, which will make you very depressed. It must be right for me to do so.

It is also important to note that if you want to connect our mysql container externally for remote management, you need to configure the host of the root account of mysql in the container and change it to a wildcard%, so that any host can connect to our mysql. The specific methods are as follows:

Enter the mysql container: use the docker exec command,-it is the parameter, and bash means to create an interactive interface

Log in to the 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 (don't forget that all mysql commands have a semicolon in the final semicolon)

As you can see, our databases are listed, and then use the mysql; command to enter the mysql database (is it very roundabout, , the mysql database here refers to this database, OK, I may not be clear)

Then use the show tables; command to list all the tables

As you can see, there are many tables, all of which are the configuration of mysql. We only need to modify one user table.

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

Some students may report an error in 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 very lucky.

If you can't connect, congratulations, because your mysql image is mysql8.

You may encounter the following mistake

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

Test remote connection

Step 4: import data into our mysql container

Although our mysql's container is running, there is no data in it. You can import the database to the mysql in docker by the following methods

First import the file into the container. Cp is followed by the path of the sql file you are going to import.

# docker cp * * .sqlmysql: / root/ into the container # docker exec-it mysql bash to import the file into the database # mysql-uroot-p [database name] < * * .sqlmysql-h localhost-uroot-p (under mysql) create database abc; (create database) show databases; (you can see all existing databases and the newly created database abc) use abc; (under abc database) show tables (see all tables under abc database, empty) source / var/test.sql (import database tables) show tables; (view all tables under abc database, you can see tables) desc pollution; (view table structure design) select * from pollution Exit (or ctrl + c) quit mysql to read here, this article "what is the method of deploying mysql services in Docker" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report