Home Database Mysql Tutorial What is the method to deploy mysql service in Docker

What is the method to deploy mysql service in Docker

May 26, 2023 pm 10:56 PM
mysql docker

Step 0: Pull the official mysql image from docker hub

docker pull mysql

Then there is a long wait, of course If you configure an image accelerator, the speed will be much faster

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

What is the method to deploy mysql service in Docker

You will see that we already have a mysql image here

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

Use the 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 , do not exit with the exit of the current command line window.

--name gives the container an alias. This container can be managed through this alias in the future.

-p 3307: 3307 Change the host's 3307 port Map to the 3306 port of the mysql container

-e The environment configuration of the mysql container

mysql_root_password=123456 Specify the password for mysql. The user name defaults to root. Note that if it is not specified password, the startup will fail

Step 3: View the mysql container we have started

Use the command: docker ps

What is the method to deploy mysql service in Docker

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

At this time, some brothers may think that although the mysql container is running happily, you only told us the port. How do we know its IP? I believe you. The old man is very bad.

No, no, no. We can use docker inspect -f ='{{. networksettings.ipaddress}}'5fef288f221f command to check the IP address of the container. Note that at the end just write the ID of the container you want to check. Those people on the Internet are very bad, and they will add a for you, which will make you very depressed, so just follow I am absolutely right

Another thing to note is: If we want to connect our mysql container externally for remote management, we need to configure the host of the root account of mysql in the container and change it to a The wildcard % allows any host to connect to our mysql. The specific method is as follows:

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

What is the method to deploy mysql service in Docker

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

What is the method to deploy mysql service in Docker

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

What is the method to deploy mysql service in Docker

You can see that our databases are List it out, and then use the mysql; command to enter the mysql database (isn’t it a convolution, hahaha, the mysql database here refers to this database, okay, maybe I still haven’t made it clear)

Then use the show tables; command to list all tables

What is the method to deploy mysql service in Docker

You can see that there are many tables. These are all mysql configurations. You don’t need to pay attention to them. We just You need to modify a user table.

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

Some students may use this command An error will be reported because your mysql may have multiple root users, so use the following command

update user set host ='%'其中user ='root'和host ='localhost';
Copy after login

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 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.

Test the remote connection

What is the method to deploy mysql service in Docker

Step 4: Import data into our mysql container

Although we The mysql container is running, but there is no data in it. You can import the database into mysql in docker through the following method

First import the file into the container, followed by cp is the sql you are going to import. File path

#docker cp **.sql mysql:/root/
进入容器
#docker exec -it mysql bash
将文件导入数据库
# mysql -uroot -p 【数据库名】 < ***.sql

mysql -h localhost -u root -p(进入mysql下面)
create database abc;(创建数据库)
show databases;(就可看到所有已经存在的数据库,以及刚刚创建的数据库abc)
use abc;(进入abc数据库下面)
show tables;(产看abc数据库下面的所有表,空的)
source /var/test.sql(导入数据库表)
show tables;(查看abc数据库下面的所有表,就可以看到表了)
desc pollution;(查看表结构设计)
select * from pollution;
exit(或者ctrl + c)退出mysql
Copy after login

The above is the detailed content of What is the method to deploy mysql service in Docker. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

How to read the docker version How to read the docker version Apr 15, 2025 am 11:51 AM

To get the Docker version, you can perform the following steps: Run the Docker command "docker --version" to view the client and server versions. For Mac or Windows, you can also view version information through the Version tab of the Docker Desktop GUI or the About Docker Desktop menu.

How to create a mirror in docker How to create a mirror in docker Apr 15, 2025 am 11:27 AM

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

How to update the image of docker How to update the image of docker Apr 15, 2025 pm 12:03 PM

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

How to copy files in docker to outside How to copy files in docker to outside Apr 15, 2025 pm 12:12 PM

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] &lt;Container Path&gt; &lt;Host Path&gt;. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to change the docker image source in China How to change the docker image source in China Apr 15, 2025 am 11:30 AM

You can switch to the domestic mirror source. The steps are as follows: 1. Edit the configuration file /etc/docker/daemon.json and add the mirror source address; 2. After saving and exiting, restart the Docker service sudo systemctl restart docker to improve the image download speed and stability.

How to save docker image How to save docker image Apr 15, 2025 am 11:54 AM

To save the image in Docker, you can use the docker commit command to create a new image, containing the current state of the specified container, syntax: docker commit [Options] Container ID Image name. To save the image to the repository, you can use the docker push command, syntax: docker push image name [: tag]. To import saved images, you can use the docker pull command, syntax: docker pull image name [: tag].

See all articles