介绍

Mysql的官方镜像是支持数据初始化脚本的,在容器启动的时候自动执行指定的sql脚本或者shell脚本,通过MySql8.0 Dockerfile文件可以发现
image

如上图所示已经设定ENTRYPOINT,同时也会调用entrypoint.sh这个脚本,下面就先来看看entrypoint.sh脚本,通过下面的脚本可以看出支持sh、sql等脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 查看docker容器
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ae97feca4aa 8253bde3e769 "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql8

# 进入mysql容器
[root@localhost ~]# docker exec -it mysql8 bash
root@9ae97feca4aa:/# ls
bin boot dev docker-entrypoint-initdb.d entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@9ae97feca4aa:/# cat entrypoint.sh
# 执行脚本的方法
# usage: process_init_file FILENAME MYSQLCOMMAND...
# ie: process_init_file foo.sh mysql -uroot
# (process a single initializer file, based on its extension. we define this
# function here, so that initializer scripts (*.sh) can use the same logic,
# potentially recursively, or override the logic used in subsequent calls)
process_init_file() {
local f="$1"; shift
local mysql=( "$@" )

case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
}

一. IDEA远程部署

我的项目结构仅供参数,懒得新建目录演示(主要文件下图已标记):
image

1. 创建Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 镜像
FROM mysql:8.0.16

# 作者
MAINTAINER lmay Zhou <lmay@lmaye.com>

# 配置MySql
ENV MYSQL_DATABASE sw-docker
ENV MYSQL_ROOT_PASSWORD root
ENV MYSQL_ROOT_HOST '%'

# 定义会被容器自动执行的目录
ENV AUTO_RUN_DIR ./docker-entrypoint-initdb.d

# 定义初始化sql文件
ENV INIT_SQL sw-docker.sql

# 把要执行的sql文件放到/docker-entrypoint-initdb.d/目录下,容器会自动执行这个sql
COPY ./$INIT_SQL $AUTO_RUN_DIR/

# 给执行文件增加可执行权限
RUN chmod a+x $AUTO_RUN_DIR/$INIT_SQL

# 暴露端口
EXPOSE 3306

2. 准备SQL脚本文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
Navicat Premium Data Transfer

Source Server : VM - 127.0.0.1
Source Server Type : MySQL
Source Server Version : 80016
Source Host : 127.0.0.1:3306
Source Schema : sw-docker

Target Server Type : MySQL
Target Server Version : 80016
File Encoding : 65001

Date: 02/06/2019 11:04:41
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '主键',
`icon` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '头像',
`name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '名字',
`email` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '邮箱',
`telephone` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '手机号',
`birthday` date NULL DEFAULT NULL COMMENT '生日',
`password` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '密码',
`gender` tinyint(1) NOT NULL DEFAULT 0 COMMENT '性别: 0. Secrecy 1. Male 2. Female',
`status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态: 0. 禁用 1. 启用',
`ext` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '拓展字段',
`version` int(20) NOT NULL DEFAULT 1 COMMENT '版本号',
`create_date` datetime(0) NOT NULL ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '创建时间',
`last_modify_date` datetime(0) NOT NULL ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '最后修改时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('10000', NULL, 'lmay', 'lmay@lmaye.com', '15072330003', '2019-06-02', 'root@2019', 1, 1, NULL, 1, '2019-06-02 12:36:47', '2019-06-02 12:36:50');

SET FOREIGN_KEY_CHECKS = 1;

3. Idea远程部署

配置如图所示(网上idea远程docker容器部署的教程很多都不够清晰,琢磨好久才成功部署),下图已经很详细了,只要按下图配置基本上可以避免很多坑!
注: Docker远程服务配置就不说了,那些网上教程基本上没啥问题;
Centos7.x 开启Docker远程API访问端口

  1. 启动配置
    image

  2. Docker 日志
    image

4. 验证结果

image

二. 本地部署

本地部署基本上都是命令操作,最关键的还是docker启动容器命令,把Dockerfile和脚本文件复制过去

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 查看当前目录
[root@localhost mysql]# ll
total 2
-rw-r--r--. 1 root root 583 May 1 16:52 Dockerfile
-rw-r--r--. 1 root root 2190 May 1 16:52 sw-docker.sql
# 查看当前docker容器
[root@localhost mysql]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4260bae9e49d mongo "docker-entrypoint.s…" 29 hours ago Up 27 hours 0.0.0.0:27017->27017/tcp mongo
# 编译并执行容器
[root@localhost mysql]# docker build -t mysql:8.0.16 . && docker run -d -p 3306:3306 -v /home/project/sw-docker/mysql/my.cnf:/etc/my.cnf --name mysql8 mysql:8.0.16

MySQL init process done. Ready for start up.

2019-05-01T20:57:03.481159Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2019-05-01T20:57:03.481231Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2019-05-01T20:57:03.481375Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.16) starting as process 1
2019-05-01T20:57:04.330788Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-05-01T20:57:04.334729Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2019-05-01T20:57:04.378381Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.16' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
2019-05-01T20:57:04.634790Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
# 校验结果
[root@localhost ~]# docker exec -it mysql8 mysql -uroot -proot
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 10
Server version: 8.0.16 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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 |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sw-docker |
| sys |
+--------------------+
5 rows in set (0.00 sec)

mysql> use sw-docker;
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_sw-docker |
+---------------------+
| user |
+---------------------+
1 row in set (0.00 sec)

mysql> select * from user;
+-------+------+------+----------------+-------------+------------+-----------+--------+--------+------+---------+---------------------+---------------------+
| id | icon | name | email | telephone | birthday | password | gender | status | ext | version | create_date | last_modify_date |
+-------+------+------+----------------+-------------+------------+-----------+--------+--------+------+---------+---------------------+---------------------+
| 10000 | NULL | lmay | lmay@lmaye.com | 15072330003 | 2019-06-02 | root@2019 | 1 | 1 | NULL | 1 | 2019-06-02 12:36:47 | 2019-06-02 12:36:50 |
+-------+------+------+----------------+-------------+------------+-----------+--------+--------+------+---------+---------------------+---------------------+
1 row in set (0.00 sec)

最后更新: 2021年04月01日 11:03

原始链接: https://www.lmaye.com/2019/06/02/20190602133656/

× 多少都行~
打赏二维码