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

时间:2023-03-03 16:56:48

dockerfile部署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

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

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

翻译之后上面就显示咱如何启动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

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

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