本机部署mysql并测试

先下载一个5.7的镜像拉取进行测试:

[root@docker ~]# docker pull mysql:5.7

查看:

[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
mysql        5.7       be16cf2d832a   4 days ago      455MB
centos       7         eeb6ee3f44bd   16 months ago   204MB
[root@docker ~]#

浏览器访问官网可以看详细介绍:

https://hub.docker.com/

7,docker基础之---Dockerfile部署MySQL_mysql

7,docker基础之---Dockerfile部署MySQL_docker_02

7,docker基础之---Dockerfile部署MySQL_MySQL_03

翻译之后上面就显示咱如何启动mysql镜像:

#原
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
#修改后
docker run --name some-mysql -p 3307:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:5.7

#-p加端口表示端口映射

执行启动:

[root@docker ~]# docker run --name some-mysql -p 3307:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:5.7                              5138979c1240205e602bf332f318182e0dd733eb4cb01eef8bf6bdeb408ca551
[root@docker ~]#
[root@docker ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                   CREATED          STATUS          PORTS                                                  NAMES
5138979c1240   mysql:5.7   "docker-entrypoint.s…"   29 seconds ago   Up 28 seconds   33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   some-mysql
[root@docker ~]#

进入容器:

[root@docker ~]# docker exec -it 5138979c1240 /bin/bash

进入MySQL数据库:

bash-4.2# mysql -uroot -pmy-secret-pw
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.41 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

进行测试:(建库)

mysql> create database `db_student`;
Query OK, 1 row affected (0.00 sec)

mysql> SET character_set_client = utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> use db_student;
Database changed
mysql>

建表:

mysql> drop table if exists `user`;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE user (
    -> id tinyint(5) zerofill auto_increment not null comment '',
    -> name varchar(20) default null comment '',
    -> age tinyint default null comment '',
    -> class varchar(20) default null comment '',
    -> sex char(5) not null comment '',
    -> unique key (id)
    -> )engine=innodb charset=utf8;
Query OK, 0 rows affected (0.01 sec)

mysql>

插入数据:

mysql> insert into user values('1','','15','','');
Query OK, 1 row affected (0.01 sec)

mysql> insert into user values('2','','13','','');
Query OK, 1 row affected (0.00 sec)

mysql> insert into user values('3','','14','','');
Query OK, 1 row affected (0.01 sec)

mysql> insert into user values('4','','12','','');
Query OK, 1 row affected (0.00 sec)

mysql>

进行查看:

mysql> show tables;
+----------------------+
| Tables_in_db_student |
+----------------------+
| user                 |
+----------------------+
1 row in set (0.00 sec)

mysql> select * from user;
+-------+------+------+-------+-----+
| id    | name | age  | class | sex |
+-------+------+------+-------+-----+
| 00001 |      |   15 |       |     |
| 00002 |      |   13 |       |     |
| 00003 |      |   14 |       |     |
| 00004 |      |   12 |       |     |
+-------+------+------+-------+-----+
4 rows in set (0.00 sec)

mysql>

通过Navicat软件连接数据库进行查看:

7,docker基础之---Dockerfile部署MySQL_docker_04

7,docker基础之---Dockerfile部署MySQL_docker_05

7,docker基础之---Dockerfile部署MySQL_MySQL_06

dockerfile部署mysql并测试

dockerfile文件内容
[root@docker test]# cat dockerfile
FROM mysql:5.7
WORKDIR /docker-entrypoint-initdb.d
ENV LANG=C.UTF-8
ADD init.sql .

[root@docker test]#
查看init.sql文件
[root@docker test]# cat init.sql
create database `db_student`;
SET character_set_client = utf8;
use db_student;
drop table if exists `user`;
CREATE TABLE user (
id tinyint(5) zerofill auto_increment not null comment '学生学号',
name varchar(20) default null comment '学生姓名',
age tinyint default null comment '学生年龄',
class varchar(20) default null comment '学生班级',
sex char(5) not null comment '学生性别',
unique key (id)
)engine=innodb charset=utf8;
insert into user values('1','小明','15','初三','男');
insert into user values('2','小红','13','初二','女');
insert into user values('3','小东','14','初一','男');
insert into user values('4','小西','12','初二','男');

[root@docker test]#
进行构建:
[root@docker test]# docker build -t mycentos:mysql2 .
查看:
[root@docker test]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
mycentos     mysql2    cab914a5a287   12 minutes ago   448MB
mycentos     redis     b7aa4955980a   5 weeks ago      599MB
mycentos     nginx     1cdfb823cd10   5 weeks ago      522MB
mycentos     jdk       27177c9bb814   5 weeks ago      1.04GB
mysql        5.7       c20987f18b13   15 months ago    448MB
centos       7         eeb6ee3f44bd   18 months ago    204MB
[root@docker test]#
执行启动设置密码为abc123456:
[root@docker test]# docker run --name some-mysql -p 3307:3306 -e MYSQL_ROOT_PASSWORD=abc123456 -d mycentos:mysql2
53098b59649d2f8ba75864cb12432b19aab49e72d91b4bf974ce56befd676cb6
数据库连接查看:

7,docker基础之---Dockerfile部署MySQL_docker_07