dockerfile启动mysql_dockerfile运行mysql并初始化数据

这里是先导入数据,然后才是设置用户和权限,是因为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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值