dockerfile運行mysql並初始化數據

 

本文目的不單單是建立一個MySQL的鏡像,而是在其基礎上再實現啓動過程當中自動導入數據及數據庫用戶的權限設置,而且在新建立出來的容器裏自動啓動MySQL服務接受外部鏈接,主要是經過Dockerfile和shell腳本實現這一過程。mysql

搭建步驟

一、首先建立Dckerfile:sql

FROM mysql:5.7 #設置免密登陸 ENV MYSQL_ALLOW_EMPTY_PASSWORD yes #將所需文件放到容器中 COPY setup.sh /mysql/setup.sh COPY schema.sql /mysql/schema.sql COPY privileges.sql /mysql/privileges.sql #設置容器啓動時執行的命令 CMD ["sh", "/mysql/setup.sh"]

二、編寫容器啓動腳本setup.sh:docker

#!/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 < /mysql/schema.sql echo '3.導入數據完畢....' sleep 3 echo `service mysql status` #從新設置mysql密碼 echo '4.開始修改密碼....' mysql < /mysql/privileges.sql echo '5.修改密碼完畢....' #sleep 3 echo `service mysql status` echo `mysql容器啓動完畢,且數據導入成功` tail -f /dev/null

這裏是先導入數據,而後纔是設置用戶和權限,是由於mysql容器一開始爲免密登陸,Dockerfile中有以下設置:ENV MYSQL_ALLOW_EMPTY_PASSWORD yes,此時執行導入數據命令不須要登陸驗證操做,若是是先執行權限操做,那麼導入數據則須要登陸驗證,整個過程就麻煩了許多。shell

三、須要導入數據的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');

由於是測試,因此隨便寫了一個建表語句,若是是真實項目確定不止這一張表,直接將建表語句覆蓋過來就好。bash

四、mysql權限設置命令privileges.sql:session

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;

五、建立鏡像ide

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

docker build 爲建立鏡像命令,名稱爲docker-mysql-init-data,'.'表示當前目錄,即Dockerfile文件所在的目錄ui

$ docker build -t docker-mysql-init-data . Sending build context to Docker daemon 6.144kB Step 1/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 image for mysql:5.7
 ---> a1aa4f76fab9 Step 2/6 : ENV MYSQL_ALLOW_EMPTY_PASSWORD yes ---> Running in 7ef903100274 Removing intermediate container 7ef903100274 ---> e0b13ef4cdea Step 3/6 : COPY setup.sh /mysql/setup.sh ---> e3e3d110e677 Step 4/6 : COPY schema.sql /mysql/schema.sql ---> a518ec11da67 Step 5/6 : COPY privileges.sql /mysql/privileges.sql ---> 3122063dfdd5 Step 6/6 : CMD ["sh", "/mysql/setup.sh"] ---> Running in 8f551037fa01 Removing intermediate container 8f551037fa01 ---> 8fb5362648b9 Successfully built 8fb5362648b9

 

 六、找到生成的鏡像,啓動容器

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

 

查看日誌

$ docker logs bc4lcbc9ansba MySQL Community Server 5.7.26 is not 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 for more 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 Server 5.7.26 is started. MySQL Community Server 5.7.26 is running. 2.開始導入數據.... 3.導入數據完畢.... MySQL Community Server 5.7.26 is running. 4.開始修改密碼.... host user localhost mysql.session localhost mysql.sys localhost root 5.修改密碼完畢.... MySQL Community Server 5.7.26 is running. /mysql/setup.sh: 1: /mysql/setup.sh: mysql容器啓動完畢,且數據導入成功: not found
相關文章
相關標籤/搜索