search
HomeDatabaseMysql TutorialWhat is the method to deploy mysql service in 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';

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:亿速云. If there is any infringement, please contact admin@php.cn delete
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool