1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
| # 测试一 * 准备Dockerfile文件 mkdir /root/mysql cd /root/mysql vim Dockerfile FROM mysql:5.7 ENV MYSQL_ALLOW_ENPTY_PASSWORD yes COPY setup.sh schema.sql privileges.sql /mysql/ # 这里最后要复制到mysql目录中,mysql后面的斜线一定不能少。 CMD ["sh", "/mysql/setup.sh"] # 下载mysql:5.7镜像;ENV MYSQL_ALLOW_ENPTY_PASSWORD yes表示可以免密码登录,这是为了方便导入数据,导入后会再设置密码;复制脚本与sql脚本到容器;最后启动容器。这个mysql:5.7是安装在Debian9系统中的
* 准备脚本 vim setup.sh #!/bin/bash # set -e echo `service mysql status` echo '1. startup mysql...' service mysql start sleep 3 echo `service mysql status` echo '2. inputing...' mysql < /mysql/schema.sql echo '3. input OK' sleep 3 echo `service mysql status` echo '4. setpassword' mysql < /mysql/privileges.sql echo '5. setpasswordOK' sleep 3 echo `service mysql status` echo 'mysql_container_OK' # 启动mysql与导入数据。set -e表示如果任何语句的执行结果不是true则应该退出。写这些echo是为了之后使用此镜像创建容器时使用docker logs可以查看到相关日志,便于排错。 chmod +x setup.sh # 一定不能忘记给脚本执行权限
* 准备sql脚本 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,'[email protected]','John','Doe','user'); # 创建一个docker_mysql库、user表,加入一条测试数据 vim privileges.sql USE mysql; SELECT host, user from user; CREATE user docker identified by '123456'; GRANT all on docker_mysql.* to docker@'%' identified by '123456' with grant option; flush privileges; # 创建用户并设置密码及权限。with grant option表示它具有grant权限
* 构建镜像、启动容器 docker build -t mysqltest:test . # 构建镜像 docker images docker run -d -p 13306:3306 --name testmysql mysqltest:test # 创建容器,创建容器时最好把库名与标签名都写全 docker logs -t 7f78ca1 # 查看日志 docker inspect --format '{{ .NetworkSettings.IPAddress }}' 7f78ca1 # 查看容器IP地址 docker exec -it 7f78ca1 bash # 进入容器 mysql -udocker -p123456 # 到容器中再进入容器中的mysql SHOW DATABASES; use docker_mysql SHOW TABLES; SELECT * FROM user;
# 测试二 mkdir /root/jdycmysql cd /root/jdycmysql vim Dockerfile FROM mysql:5.7 ENV MYSQL_ALLOW_ENPTY_PASSWORD yes COPY setup.sh echarging.sql user.sql /mysql/ COPY my.cnf /etc/mysql/ CMD ["sh", "/mysql/setup.sh"] # 容器中的mysql配置文件在/etc/mysql中,echarging.sql是要用的数据库,user.sql是改密码与权限所用 vim setup.sh #!/bin/bash # #set -e echo `service mysql status` echo '1. startup mysql...' service mysql start sleep 3 echo '1.1 createing...' /usr/bin/mysql -e "create database echarging" echo 'create OK!!!!!!!' sleep 3 echo `service mysql status` echo '2. inputing...' mysql echarging < /mysql/echarging.sql echo '3. input OK' sleep 3 echo `service mysql status` echo '4. setpassword' mysql < /mysql/user.sql echo '5. setpasswordOK' sleep 3 echo `service mysql status` echo 'mysql_container_OK' # 因为set -e在返回错误代码时就会退出脚本,所以这里注释了,因为导入数据库时可能会有报错。因为echarging.sql中没有创建数据库的命令,所以这里加入了创建数据库的命令,并且在导入时也指定了数据库,如果不写明这两项,在导入数据库时都会报找不到数据库的错误。 vim my.cnf [client] default-character-set = utf8mb4 [mysqld] init-connect = 'SET NAMES utf8mb4' character-set-server = utf8mb4 vim user.sql USE mysql GRANT all on *.* to echarge@'%' identified by 'CCjd1rj@com' with grant option; GRANT all on *.* to root@'%' identified by 'CCjd1rj@com' with grant option; flush privileges; # 新建一个用户并设置root用户与新建用户的密码与权限。 docker build -t jdycm:Jmysql . docker run -d -p 3306:3306 --name jdycmysql jdycm:Jmysql watch 'docker logs -t jdycmysql' 2018-11-30T08:57:34.924250376Z MySQL Community Server 5.7.24 is not running. 2018-11-30T08:57:34.924304852Z 1. startup mysql... 2018-11-30T08:57:35.082746075Z 2018-11-30T08:57:35.078891Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timesta mp server option (see documentation for more details). 2018-11-30T08:57:35.281231444Z 2018-11-30T08:57:35.280868Z 0 [Warning] InnoDB: New log files created, LSN=45790 2018-11-30T08:57:35.330693896Z 2018-11-30T08:57:35.330333Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2018-11-30T08:57:35.390385937Z 2018-11-30T08:57:35.389987Z 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: fafeea4e-f47d-11e8-87e6-0242ac110003. 2018-11-30T08:57:35.391824469Z 2018-11-30T08:57:35.391529Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2018-11-30T08:57:35.393019043Z 2018-11-30T08:57:35.392585Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initiali ze-insecure option. 2018-11-30T08:57:41.396343436Z .. 2018-11-30T08:57:41.396406084Z MySQL Community Server 5.7.24 is started. 2018-11-30T08:57:44.399015302Z 1.1 createing... 2018-11-30T08:57:44.410619098Z create OK!!!!!!! 2018-11-30T08:57:47.486484333Z MySQL Community Server 5.7.24 is running. 2018-11-30T08:57:47.486554105Z 2. inputing... 2018-11-30T09:05:59.990855328Z 3. input OK 2018-11-30T09:06:03.065787206Z MySQL Community Server 5.7.24 is running. 2018-11-30T09:06:03.065853836Z 4. setpassword 2018-11-30T09:06:03.077077343Z 5. setpasswordOK 2018-11-30T09:06:06.151459148Z MySQL Community Server 5.7.24 is running. 2018-11-30T09:06:06.151902503Z /mysql/setup.sh: 1: /mysql/setup.sh: mysql_container_OK: not found 2018-11-30T09:06:06.151989387Z # docker logs的-t选项是显示时间戳的。 docker exec -it 901fc12e6e48 bash mysql mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | echarging | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.01 sec)
mysql> use echarging 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_echarging | +-----------------------------+ | area_dictionary | | assistant_user | | charge_record | | communication_board_version |
|