使用docker-compose创建mysql容器
- 创建并进入mysql文件夹
mkdir -p /opt/docker/mysql && cd /opt/docker/mysql
- 创建net网络
docker network create net
- 创建临时的docker-compose-mysql.yml文件,导入以下脚本
vim docker-compose-mysql.yml
version: "3.7"
services:
mysql:
image: mysql:5.7
container_name: mysql
restart: always
tty: true
privileged: true
ports:
- 13357:3306
volumes:
- /etc/localtime:/etc/localtime
command:
--max_connections=1000
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--default-authentication-plugin=mysql_native_password
networks:
- net
environment:
- TZ=Asia/Shanghai
- LANG=en_US.UTF-8
- MYSQL_ROOT_PASSWORD=root
networks:
net:
external:
true
- 创建临时nysql容器
docker-compose -f docker-compose-mysql up -d
- 下拉mysql容器中的数据卷
#mmysql配置文件
docker cp mysql:/etc/mysql ./conf
#mysql数据
docker cp mysql:/var/lib/mysql ./data
- 删除临时mysql容器
docker-compose -f docker-compose-mysql.yml down
- 向docker-compose-mysql.yml导入以下脚本
启动sql目录放sql脚本
version: "3.7"
services:
mysql:
image: mysql:5.7
container_name: mysql
restart: always
tty: true
privileged: true
ports:
- 13357:3306
volumes:
- /etc/localtime:/etc/localtime
- ./conf:/etc/mysql
- ./data:/var/lib/mysql
- ./sql:/sql
command:
--max_connections=1000
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--default-authentication-plugin=mysql_native_password
networks:
- net
environment:
- TZ=Asia/Shanghai
- LANG=en_US.UTF-8
- MYSQL_ROOT_PASSWORD=root
networks:
net:
external:
true
- 创建mysql容器
docker-compose -f docker-compose-mysql.yml up -d
- 进入容器
docker exec -it mysql bash
- 进入mysql终端
mysql -uroot -p
root
- 退出终端、退出容器
exit
- 使用终端向mysql导入sql脚本
- 将test.sql脚本放入到sql目录下
test.sql
-- MySQL dump 10.13 Distrib 5.7.36, for Linux (x86_64)
--
-- Host: localhost Database: test
-- ------------------------------------------------------
-- Server version 5.7.36
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `test`
--
DROP TABLE IF EXISTS `test`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `test` (
`id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `test`
--
LOCK TABLES `test` WRITE;
/*!40000 ALTER TABLE `test` DISABLE KEYS */;
INSERT INTO `test` VALUES (1);
/*!40000 ALTER TABLE `test` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2022-04-17 23:35:27
cd /opt/docker/mysql/sql
ls /opt/docker/mysql/sql #test.sql
- 进入mysql容器的sql目录下
docker exec -it mysql bash
cd /sql
ls /sql # test.sql
#如果发现sql目录下没有test.sql文件,则在mysql容器外的sql目录下执行以下命令
docker cp ./test.sql mysql:/sql/
- 进入mysql终端,并创建test数据库
mysql -uroot -p
root
create databases test;
- 在mysql容器的sql目录下导入test.sql脚本
mysql -uroot -p test < test.sql
- 将test数据库数据导出为test2.sql文件
- 进入mysql容器的sql目录
docker exec -it mysql bash
cd /sql
- 使用mysqldump导出test库的脚本到test2.sql文件中
mysqldump -uroot -p test > test2.sql
root
- 退出mysql容器,查看test2.sql文件
exit
cd /opt/docker/mysql
ls # test2.sql
#如果没发现test2.sql文件,则执行以下脚本
docker cp mysql:/sql/test2.sql ./test2.sql