这里是先导入数据,然后才是设置用户和权限,是因为mysql容器一开始为免密登录,Dockerfile中有如下设置:ENV MYSQL_ALLOW_EMPTY_PASSWORD yes,此时执行导入数据命令不需要登录验证操作,如果是先执行权限操作,那么导入数据则需要登录验证,整个过程就麻烦了许多。

3、需要导入数据的mysql脚本命令schema.sql:

--创建数据库

create database `docker_mysql`default character setutf8 collate utf8_general_ci;

use docker_mysql;--建表

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (

`id` bigint(20) NOT NULL,

`created_at` bigint(40) DEFAULT NULL,

`last_modified` bigint(40) DEFAULT NULL,

`email` varchar(255) DEFAULT NULL,

`first_name` varchar(255) DEFAULT NULL,

`last_name` varchar(255) DEFAULT NULL,

`username` varchar(255) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;--插入数据

INSERT INTO `user` (`id`, `created_at`, `last_modified`, `email`, `first_name`, `last_name`, `username`)

VALUES

(0,1490257904,1490257904,'john.doe@example.com','John','Doe','user');

因为是测试,所以随便写了一个建表语句,如果是真实项目肯定不止这一张表,直接将建表语句覆盖过来就好。

4、mysql权限设置命令privileges.sql:

use mysql;select host, user fromuser;-- 因为mysql版本是5.7,因此新建用户为如下命令:

create user docker identified by'123456';--将docker_mysql数据库的权限授权给创建的docker用户,密码为123456:

grant all on docker_mysql.* to docker@'%' identified by '123456'with grant option;--这一条命令一定要有:

flush privileges;

5、创建镜像

docker build -t docker-mysql-init-data .

docker build 为创建镜像命令,名称为docker-mysql-init-data,'.'表示当前目录,即Dockerfile文件所在的目录

$ docker build -t docker-mysql-init-data .

Sending build context to Docker daemon6.144kB

Step1/6 : FROM mysql:5.7

5.7: Pulling from library/mysql

fc7181108d40: Already exists

787a24c80112: Pull complete

a08cb039d3cd: Pull complete

4f7d35eb5394: Pull complete

5aa21f895d95: Pull complete

a742e211b7a2: Pull complete

0163805ad937: Pull complete

62d0ebcbfc71: Pull complete

559856d01c93: Pull complete

c849d5f46e83: Pull complete

f114c210789a: Pull complete

Digest: sha256:c3594c6528b31c6222ba426d836600abd45f554d078ef661d3c882604c70ad0a

Status: Downloaded newer imagefor mysql:5.7

--->a1aa4f76fab9

Step2/6: ENV MYSQL_ALLOW_EMPTY_PASSWORD yes---> Running in7ef903100274

Removing intermediate container 7ef903100274--->e0b13ef4cdea

Step3/6 : COPY setup.sh /mysql/setup.sh--->e3e3d110e677

Step4/6 : COPY schema.sql /mysql/schema.sql--->a518ec11da67

Step5/6 : COPY privileges.sql /mysql/privileges.sql--->3122063dfdd5

Step6/6 : CMD ["sh", "/mysql/setup.sh"]---> Running in8f551037fa01

Removing intermediate container 8f551037fa01--->8fb5362648b9

Successfully built 8fb5362648b9

6、找到生成的镜像,启动容器

$ docker run -p 13306:3306 -d docker-mysql-init-data

查看日志

$ docker logs bc4lcbc9ansba

MySQL Community Server5.7.26 isnot running.1.启动mysql....2019-07-08T03:50:47.131210Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation formore details).2019-07-08T03:50:47.331141Z 0 [Warning] InnoDB: New log files created, LSN=45790

2019-07-08T03:50:47.355405Z 0[Warning] InnoDB: Creating foreign key constraint system tables.2019-07-08T03:50:47.414068Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 91ddb324-a133-11e9-9a7c-0242ac110002.2019-07-08T03:50:47.415870Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed'cannot be opened.2019-07-08T03:50:47.416972Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

..

MySQL Community Server5.7.26 isstarted.

MySQL Community Server5.7.26 isrunning.2.开始导入数据....3.导入数据完毕....

MySQL Community Server5.7.26 isrunning.4.开始修改密码....

host user

localhost mysql.session

localhost mysql.sys

localhost root5.修改密码完毕....

MySQL Community Server5.7.26 isrunning./mysql/setup.sh: 1: /mysql/setup.sh: mysql容器启动完毕,且数据导入成功: not found

dockerfile启动mysql_dockerfile运行mysql并初始化数据相关推荐

  1. dockerfile 安装mysql_dockerfile构建mysql镜像

    使用dockerfile构建mysql,镜像构建完成后,运行容器后,启动mysql服务总是报错 Fatal error: Can't open and lock privilege tables: T ...

  2. 如何以用户身份登录MySQL_解析:如何以普通用户的身份运行 MySQL

    在Windows平台上,能够使用普通用户账户以Windows服务方式运行服务器. 在Unix平台上,不是任何用户都能启动并运行MySQL服务器mysqld的.但是,处于安全方面的原因,应避免以Unix ...

  3. 用Dockerfile构建MySQL镜像并实现容器启动过程中MySQL数据库系统的初始化

    前一段时间就在研究用Dockerfile构建MySQL镜像并实现容器启动过程中MySQL数据库系统的初始化,但被一些无关紧要的事儿给耽误了,经过查阅<dockerfile最佳实践>及MyS ...

  4. docker mysql数据库初始化_如何在Mysql的Docker容器启动时初始化数据库

    1.前言 Docker在开发中使用的越来越多了,最近搞了一个Spring Boot应用,为了方便部署将Mysql也放在Docker中运行.那么怎么初始化 SQL脚本以及数据呢? 我这里有两个传统方案. ...

  5. docker 运行mysql镜像_docker 生成mysql镜像启动时自动执行sql

    在docker 创建 mysql 容器时,往往需要在创建容器的过程中创建database 实例,代码如下: docker run -d -p 3308:3306 -e MYSQL_ROOT_PASSW ...

  6. Docker 制作 MySQL 镜像并使用 `/docker-entrypoint-initdb.d/` 机制初始化数据

    需求 制作一个 MySQL Docker 镜像并初始化数据库信息 环境 win 11 Docker-Desktop 4.14.0 (91374) 分析 启动一个MySQL容器很容易.如何初始化数据呢? ...

  7. docker mysql初始化数据及数据持久化

    1.初始化默认数据 我们经常会在mysql容器启动时,需要初始化一些数据库.数据表或者其他默认数据.其实mysql早已支持我们这种需求,具体方案如下: # 其实主要的做法就是重新build mysql ...

  8. mysql主从同步数据初始化_mysql主从同步配置

    Mysql的主从复制至少是需要两个Mysql的服务,当然Mysql的服务是可以分布在不同的服务器上,也可以在一台服务器上启动多个服务. 首先确保主从服务器上的Mysql版本相同. 一.安装部署mysq ...

  9. springboot初始化逻辑_SpringBoot——启动初始化数据

    前言 在我们用 springboot 搭建项目的时候,有时候会碰到在项目启动时初始化一些操作的需求 ,针对这种需求 spring boot为我们提供了以下几种方案供我们选择: ApplicationR ...

最新文章

  1. 【中级软考】什么是二维数组元素的偏移量?(就是把数组ravel[拉平]后存到内存中的元素位置)
  2. php mysql source_详解MySQL数据库中有关source命令
  3. python计算机视觉编程调试问题
  4. 匿名内部类探究——它是一个实例
  5. Windows平台RTMP|RTSP播放器为什么要兼容GDI绘制
  6. shell编程之【告警系统】
  7. 海康威视监控摄像头接入到微信直播流程
  8. Android应用 中英文切换
  9. 笔记本键盘字母变数字解决方法
  10. word快捷键复制粘贴无法使用
  11. FPGA浮点运算实战
  12. map返回另一个对象
  13. 最简单的无线充电传输电路
  14. 软考高级软件架构师学习笔记二(软件工程)
  15. redis 查看键空闲时间
  16. 银行卡识别,录入银行卡号助手
  17. DYA10面向对象中--断点调试
  18. 在Linux上安装Git
  19. 同志们,免费版的Ant Design Pro Vue3 来啦
  20. 一份门户网站SEO优化报告(附上海SEO评析)

热门文章

  1. Python爬虫——零基础实现基于scrapy框架的项目
  2. python中执行linux命令(调用linux命令)_常见Python中调用Linux命令
  3. java计算机毕业设计社区老人健康服务跟踪系统源码+系统+数据库+lw文档+mybatis+运行部署
  4. jquery.html加换行符,在使用jQuery时添加元素间的换行符或空格.append()
  5. 裸辞,在某个程度上是在解救自己?
  6. 大津法优化之在飞卡智能车中的应用
  7. C语言位操作详解(全网最全)
  8. Mac OS X 10.2 完美的数码解决方案(转)
  9. 奥运开幕式之烟花灿烂
  10. JavaScript中函数作用域之精辟,函数原理的浅入深出,及程序执行预编译之通天编译???