构建MySQL镜像
本文目的不仅仅是创建一个MySQL的镜像,而是在其基础上再实现启动过程中自动导入数据及数据库用户的权限设置,并且在新创建出来的容器里自动启动MySQL服务接受外部连接,主要是通过Dockerfile和shell脚本实现这一过程。
- 创建工作目录
[root@elk-node2 ~]# mkdir mysql
[root@elk-node2 ~]# cd mysql/
[root@elk-node2 mysql]# ls
Dockerfile privileges.sql schema.sql setup.sh
2.1创建Dockerfile 文件
[root@elk-node2 mysql]# vim Dockerfile
FROM mysql:5.7
#设置免密登录
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
#将所需文件放到容器中
COPY setup.sh /root/mysql/setup.sh
COPY schema.sql /root/mysql/schema.sql
COPY privileges.sql /root/mysql/privileges.sql
#设置容器启动时执行的命令
CMD ["sh", "/root/mysql/setup.sh"]
2.2 mysql权限设置命令privileges.sql:
[root@elk-node2 mysql]# vim privileges.sql
use mysql;
select host, user from user;
-- 因为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;
2.3需要导入数据的mysql脚本命令schema.sql:
[root@elk-node2 mysql]# vim schema.sql
-- 创建数据库
create database `docker_mysql` default character set utf8 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');
因为是测试,所以随便写了一个建表语句,如果是真实项目肯定不止这一张表,直接将建表语句覆盖过来就好。
2.4编写容器启动脚本setup.sh:
[root@elk-node2 mysql]# vim setup.sh
#!/bin/bash
set -e
#查看mysql服务的状态,方便调试,这条语句可以删除
echo `service mysql status`
echo '1.启动mysql....'
#启动mysql
service mysql start
sleep 3
echo `service mysql status`
echo '2.开始导入数据....'
#导入数据
mysql < /root/mysql/schema.sql
echo '3.导入数据完毕....'
sleep 3
echo `service mysql status`
#重新设置mysql密码
echo '4.开始修改密码....'
mysql < /root/mysql/privileges.sql
echo '5.修改密码完毕....'
#sleep 3
echo `service mysql status`
echo `mysql容器启动完毕,且数据导入成功`
tail -f /dev/null
3.用Dockerfile生成镜像
[root@elk-node2 mysql]# docker build -t 13/docker-mysql
执行docker images查看该镜像是否存在于镜像列表中
[root@elk-node2 mysql]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
13/docker-mysql latest 58bab73950a9 7 minutes ago 373 MB
4运行容器并将端口映射到本地的13306端口
[root@elk-node2 mysql]# docker run -d -p 13306:3306 13/docker-mysql
02f72e74a59fa84de40c077bf601901d4916e2209b0b4f7e41ae57885c410d14
[root@elk-node2 mysql]# docker ps
02f72e74a59f 13/docker-mysql "docker-entrypoint..." 33 seconds ago Up 32 seconds 33060/tcp, 0.0.0.0:13306->3306/tcp
- 验证 这个地方ID号需要看清,我这里是02f72e74a59f
前文中创建了docker_mysql数据库,并在此数据库中创建了user表,同时将数据库的连接授权赋予了新建的docker用户,因此验证过程为:
使用docker用户登录数据库:mysql -u docker -p
输入密码123456通过登录验证
切换至docker_mysql数据库:use docker_mysql;
查看数据库中的表:show tables;
查看表中的数据:select * from user;
[root@elk-node2 mysql]# docker exec -it 02f /bin/bash
root@02f72e74a59f:/#
root@02f72e74a59f:/# mysql -u docker -p
Enter password: 123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.26 MySQL Community Server (GPL)
mysql> use docker_mysql;
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_docker_mysql |
+------------------------+
| user |
+------------------------+
1 row in set (0.00 sec)
mysql> select *from user;
+----+------------+---------------+----------------------+------------+-----------+----------+
| id | created_at | last_modified | email | first_name | last_name | username |
+----+------------+---------------+----------------------+------------+-----------+----------+
| 0 | 1490257904 | 1490257904 | john.doe@example.com | John | Doe | user |
+----+------------+---------------+----------------------+------------+-----------+----------+
1 row in set (0.00 sec)