参考链接:
https://www.cnblogs.com/wtzbk/p/15125977.html
目标:完成docker-compse功能编写,实现springboot连接mysql
解决问题:springboot连接mysql connect refuse
环境:JDK8+Springboot+gradle+mysql
开发工具:IntelliJ IDEA Community、Navicat
文章目录
1. 学习docker基础语句
1. docker images 查看镜像
2. docker ps 查看容器
3. docker network ls 查看网络
4. docker network inspect NETWORK ID 查看网络信息
5. docker exec 在运行的容器中执行命令
2. 学习docker-compose基础语句
1. docker-compose up 创建并启动容器
2. docker-compose down 停止并删除容器
3. docker-compose config 校验并查看compose文件
4. docker-compose build --no-cache 无缓存构建(很重要!!!)
3.配置jar包
3.1搭配项目
3.1.1 创建项目
我是用社区版,免费,功能不强大,就是创建一个项目。
创建地址:https://start.spring.io/
打开项目后等待依赖下载结束。
在这里插入图片描述
3.1.2 配置项目build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '2.7.6'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
jar {
manifest {
attributes 'Main-Class': 'com.example.demo.BookController'
}
}
repositories {
maven {
allowInsecureProtocol = true
url 'http://maven.aliyun.com/nexus/content/groups/public/'
}
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.0'
implementation "mysql:mysql-connector-java:8.0.30"
}
tasks.named('test') {
useJUnitPlatform()
}
3.1.3 配置连接数据库
3.1.3.1 项目结构
3.1.3.2 项目数据
连接数据库配置
spring:
dataSource:
#高版本需要指明是否进行SSL连接
url: jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations:
classpath:mapper/*.xml
3.1.4测试项目
3.2 Centos8存放jar包
4. 编写Dockfile文件
#镜像基础
FROM java:8
#镜像信息
LABEL name="docker-compose-demo" version="1.0" autor="myc"
#拷贝
COPY ./Book-0.0.1-SNAPSHOT.jar ./docker-compose-demo.jar
#暴露端口
EXPOSE 8082
#运行
CMD ["java","-jar","docker-compose-demo.jar"]
5. docker-compose
docker-compose需要安装,这边就演示了。需要的话可以看最上面的参考连接。
5.1 编写docker-compose文件
version: '3'
#网络
networks:
docker-compose-demo-net:
#桥接模式
driver: bridge
ipam:
config:
- subnet: 192.168.62.56/24
gateway: 192.168.62.1
services:
docker-compose-demo02:
#DockerFile文件所在地方
build:
context: /usr/local/docker-compose-demo
dockerfile: Dockerfile
#DockerFile生成的镜像
image: docker-compose-demo
container_name: docker-compose-demo02
depends_on:
- mysql
networks:
- docker-compose-demo-net
ports:
- "8082:8080"
restart: always
mysql:
image: mysql:8.0.30
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: '123456'
MYSQL_ALLOW_EMPTY_PASSWORD: 'no'
MYSQL_DATABASE: 'book'
ports:
- "3306:3306"
volumes:
- /usr/local/docker-compose-demo/mysql/db:/var/lib/mysql
- /usr/local/docker-compose-demo/mysql/conf/my.cnf:/etc/my.conf
- /usr/local/docker-compose-demo/mysql/init:/docker-entrypoint-initdb.d
restart: always
networks:
- docker-compose-demo-net
command:
--default-authentication-plugin=mysql_native_password
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
volumes:
docker-compose-demo-volume: {}
5.2 测试docker-compose文件
无报错,正常显示配置文件内容。
docker-compose config
5.3 启动容器
使用docker-compose up启动项目,如果是第一次运行会行拉取镜像,比较慢的。
6. 测试访问
7. 数据库配置
7.1 Navicat
7.2 容器
[root@localhost ~]# docker ps(查看容器)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b34ea8492a6f docker-compose-demo "java -jar docker-co…" 7 minutes ago Up 7 minutes 8082/tcp, 0.0.0.0:8082->8080/tcp, :::8082->8080/tcp docker-compose-demo02
7f20a56083fe mysql:8.0.30 "docker-entrypoint.s…" 7 minutes ago Up 7 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
[root@localhost ~]# docker exec -it 7f20a56083fe(mysql容器ID) bash
bash-4.4# mysql -uroot(账号) -p123456(密码)
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 25
Server version: 8.0.30 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| book |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.03 sec)
mysql> use book;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_book |
+----------------+
| book |
+----------------+
1 row in set (0.00 sec)
7.3 数据库语句
/*
Navicat Premium Data Transfer
Source Server : AAA
Source Server Type : MySQL
Source Server Version : 50639
Source Host : localhost:3306
Source Schema : book
Target Server Type : MySQL
Target Server Version : 50639
File Encoding : 65001
Date: 28/11/2022 22:03:15
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for book
-- ----------------------------
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book` (
`bid` int(11) NOT NULL,
`bname` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
`nprice` decimal(10, 2) NULL DEFAULT NULL,
PRIMARY KEY (`bid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of book
-- ----------------------------
INSERT INTO `book` VALUES (1, 'ww', 225.50);
INSERT INTO `book` VALUES (3, 'Java', 35.25);
SET FOREIGN_KEY_CHECKS = 1;