Home  >  Article  >  Database  >  What is the method to deploy mysql service in Docker

What is the method to deploy mysql service in Docker

WBOY
WBOYforward
2023-05-26 22:56:041353browse

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';

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

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:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete