docker实例之mysql的使用
Vagrant007 人气:7docker实例之mysql的使用
常用步骤 | 命令 |
---|---|
1:搜索镜像 | docker search xxx |
2:拉取镜像 | docker pull xxx:yy |
3:查看镜像 | docker image inspect xxx:yy |
4:运行容器 | docker run xxx:yy |
5:停止容器 | docker container stop ID |
6:删除容器 | docker container rm ID |
演示:
1:搜索mysql镜像
[root@docker ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 8864 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3112 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 655 [OK]
......
2:拉取镜像
[root@docker ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
d599a449871e: Pull complete
f287049d3170: Pull complete
08947732a1b0: Pull complete
96f3056887f2: Pull complete
871f7f65f017: Pull complete
1dd50c4b99cb: Pull complete
5bcbdf508448: Pull complete
02a97db830bd: Pull complete
c09912a99bce: Pull complete
08a981fc6a89: Pull complete
818a84239152: Pull complete
Digest: sha256:5779c71a4730da36f013a23a437b5831198e68e634575f487d37a0639470e3a8
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@docker ~]# docker image ls -a
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 1e4405fe1ea9 4 days ago 437MB
3:查看镜像信息
[root@docker ~]# docker image inspect 1e44
...
4:启动一个容器
[root@docker ~]# docker run -p 12345:3306 --name mysql -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/logs:/logs -v /root/mysqlhttps://img.qb5200.com/download-x/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
fc136870a6baf32639f08a5f4c545aaca26815cecc0c26018184b86b36c72531
[root@docker ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fc136870a6ba mysql:5.7 "docker-entrypoint.s…" 18 seconds ago Up 12 seconds 33060/tcp, 0.0.0.0:12345->3306/tcp mysql
#命令说明:
-p[小写]:将主机的 12345端口映射到容器内部端口3306
--name:设置别名 mysql
-v /root/mysql/conf:/etc/mysql/conf.d
-v /root/mysql/logs:/logs
-v /root/mysqlhttps://img.qb5200.com/download-x/data:/var/lib/mysql
-e MYSQL_ROOT_PASSWORD=123456 #初始化root用户密码
-d mysql:5.7 #后台运行
5:进入容器并使用mysql
[root@docker ~]# docker exec -it fc136870a6ba /bin/bash
root@fc136870a6ba:/#
root@fc136870a6ba:/# mysql -uroot -p
Enter password: #123456前面设置的
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.28 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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客户端
mysql> show databases; #显示数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database db01; #创建一个数据库
Query OK, 1 row affected (0.00 sec)
mysql> show databases ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db01 |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use db01; #切换数据库
Database changed
mysql> create TABLE mybook (id int not null primary key,name char(15),price int,pages int); #建表
Query OK, 0 rows affected (0.13 sec)
mysql> DESCRIBE mybook; #查询表结构
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(15) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
| pages | int(11) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
4 rows in set (0.24 sec)
mysql> INSERT INTO mybook(id,name,price,pages) VALUES('1','docker','60','438'); #插入一条数据
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM mybook; #查询数据
+----+--------+-------+-------+
| id | name | price | pages |
+----+--------+-------+-------+
| 1 | docker | 60 | 438 |
+----+--------+-------+-------+
1 row in set (0.00 sec)
mysql> quit
Bye
root@fc136870a6ba:/# read escape sequence
[root@docker ~]#
[root@docker ~]# docker ps -al
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fc136870a6ba mysql:5.7 "docker-entrypoint.s…" 27 minutes ago Up 27 minutes 33060/tcp, 0.0.0.0:12345->3306/tcp mysql
[root@docker ~]#
mysql练习结束
6:使用win主机上的Navicat for MySQL 连接 虚拟机中运行的mysql容器
1:找到容器运行的主机的IP地址
[root@docker ~]# ifconfig eno16777728 |grep broad | sed 's/inet//' |sed 's/netmask.*//'
192.168.1.10
2:在win主机中远程连接
数据和刚刚在容器中的生成的一样,成功。
加载全部内容