In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Summary of this article
The purpose of this paper is not only to create a MySQL image, but also to automatically import data and database user permissions during startup, and automatically start MySQL services to accept external connections in the newly created container, mainly through Dockerfile and shell scripts.
Building steps
1. First create a Dckerfile:
FROM mysql:5.7# Settings Secret-Free login ENV MYSQL_ALLOW_EMPTY_PASSWORD yes# puts the required files in the container COPY setup.sh / mysql/setup.shCOPY schema.sql / mysql/schema.sqlCOPY privileges.sql / mysql/privileges.sql# sets the command CMD to be executed when the container starts ["sh", "/ mysql/setup.sh"]
2. Write the container startup script setup.sh:
#! / bin/bashset-e # View the status of mysql service to facilitate debugging. This statement can delete echo `service mysql status`echo'1. Start mysql....'# to start mysqlservice mysql startsleep 3echo `service mysql status`echo'2. Start importing data....'# Import data mysql
< /mysql/schema.sqlecho '3.导入数据完毕....'sleep 3echo `service mysql status`#重新设置mysql密码echo '4.开始修改密码....'mysql < /mysql/privileges.sqlecho '5.修改密码完毕....'#sleep 3echo `service mysql status`echo `mysql容器启动完毕,且数据导入成功`tail -f /dev/null 这里是先导入数据,然后才是设置用户和权限,是因为mysql容器一开始为免密登录,Dockerfile中有如下设置:ENV MYSQL_ALLOW_EMPTY_PASSWORD yes,此时执行导入数据命令不需要登录验证操作,如果是先执行权限操作,那么导入数据则需要登录验证,整个过程就麻烦了许多。 3、需要导入数据的mysql脚本命令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'); 因为是测试,所以随便写了一个建表语句,如果是真实项目肯定不止这一张表,直接将建表语句覆盖过来就好。 4、mysql权限设置命令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; 5、创建镜像 docker build -t 13/docker-mysql . docker build 为创建镜像命令,名称为13/docker-mysql,'.'表示当前目录,即Dockerfile文件所在的目录,创建过程如下: 执行docker images查看该镜像是否存在于镜像列表中: 创建成功。 6、启动容器 docker run -d -p 13306:3306 13/docker-mysql 启动容器,并将端口映射到本地的13306端口,命令行如图所示:The container started successfully.
Check the log records of the container. The startup process is consistent with the steps specified in the startup script setup.sh. The data import and permission settings are successful:
Verification result
1. Verify on the command line by entering the container
The id of the container at startup is 9db491b1d760, so execute the exec command to enter the container:
Docker exec-it 9db491b1d760 / bin/bash
Do not use this command directly, because the id may be different on your machine, just replace the id value.
In the previous article, we created the docker_mysql database, created the user table in this database, and granted the connection authorization to the new docker user, so the verification process is as follows:
Log in to the database using the docker user: mysql-u docker-p enter password 123456 to switch to the docker_mysql database by login authentication: use docker_mysql; view tables in the database: show tables; view data in tables: select * from user
The whole process is as follows:
Through the comparison of the results in the figure, it is consistent with the previous article, and the verification is successful.
2. Verified by mysql client management software
Through the comparison of the results in the figure, it is consistent with the previous article, and the verification is successful.
Conclusion
This article is a separate introduction to the steps of creating a mysql image. I hope it will be helpful to your study, and I hope you will support it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.