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

Detailed explanation of Mysql data Import / Export in Docker Container

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Preface

We all know that the import and export of Mysql data can be solved by a mysqldump command, but what about mysql running in docker?

The solution is actually to use the mysqldump command, but we need to go into the mysql container of docker to execute it, and configure volumes so that the exported data files can be copied to the disk of the host

So the steps can be divided into:

Configure the volumes of docker to enter the mysql container of docker and export the data file

As for data import, it is too simple to say.

Let's first take a look at the common options for the mysqldump command:

-- all-databases,-A: backup all databases-- databases,-B: used to back up multiple databases. If this option is not available, mysqldump takes the first name parameter as the database name, followed by the table name. With this option, mysqldum treats each name as the database name. -- force,-f: continue backup even if sql error is found-- host=host_name,-h host_name: backup hostname, default is localhost--no-data,-d: only export table structure-- password [= password],-p [password]: password-- port=port_num,-P port_num: Port number when making TCP/IP connection-- quick,-Q: fast export-- tables: overwrite-- databases or-B option The following parameters are treated as table names-user=user_name,-u user_name: user name-- xml,-X: exported to xml file

Configure volumes

First of all, I use docker-compose to orchestrate the docker container. For the complete configuration code, please see this project: liumapp/rabbitmq-mysql-redis-in-docker

Notice that the docker-compose.yml configuration file for this project has the following lines:

Mysql: container_name: mysql image: mysql:5.5.60 restart: always volumes: -. / mysql/data:/var/lib/mysql -. / mysql/conf/mysqld.conf:/etc/mysql/mysql.conf.d/mysqld.cnf

The volumes I configured for the mysql container is to establish a mapping relationship between the project's mysql/data directory and the / var/lib/mysql in the docker container

So when I execute the export command in the mysql container of docker, I only need to export the data in the / var/lib/mysql/ directory and find the corresponding data file in the. / mysql/data/ directory of the host.

Enter the container to export data

First execute

Docker ps

Find the name of the mysql container

And then execute

Docker exec-it mysql / bin/bash

Enter the container

Execute a command

Whereis mysql

Find the running path of mysql, here is: / usr/local/mysql/bin, enter with cd

Cd / usr/local/mysql/bin

Please note that the path here refers to the path within the docker container, which has nothing to do with your host path.

Execute the export command

Mysqldump-u user name-p database name > save file. Sql

After entering the password, the export is basically successful. Please note that the path to save the file should be set under volumes, that is, / var/lib/mysql/.

Then enter

Exit

Exit the container, return to the host, and we will be able to find the exported data file

If you want to export csv format, change the command of mysqldump to:

Mysql-u user name-- password= password-- database= database name-- execute='SELECT `FIELD`, `FIELD` FROM `TABLE` LIMIT 0, 10000'- X > Save the file. Sql

Can

Summary

The above is the whole content of this article, I hope that the content of this article has a certain reference and learning value for your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.

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

Servers

Wechat

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

12
Report