Docker常用安装MYSQL
程序员文章站
2024-03-25 13:56:52
...
总体步骤
- 搜索镜像
- 拉取镜像
- 查看镜像
- 启动镜像
- 停止容器
- 移除容器
安装tomcat
- docker hub上面查找tomcat镜像
- 从docker hub上拉取tomcat镜像到本地
- docker images查看是否有拉取到的tomcat
- 使用tomcat镜像创建容器(也叫运行镜像)
安装mysql
docker hub 上面查找musql镜像
[aaa@qq.com ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 8675 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3021 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 642 [OK]
percona Percona Server is a fork of the MySQL relati… 453 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 63
centurylink/mysql Image containing mysql. Optimized to be link… 61 [OK]
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 52
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
bitnami/mysql Bitnami MySQL Docker Image 36 [OK]
tutum/mysql Base docker image to run a MySQL database se… 34
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 28 [OK]
prom/mysqld-exporter 23 [OK]
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 22
centos/mysql-56-centos7 MySQL 5.6 SQL database server 16
circleci/mysql MySQL is a widely used, open-source relation… 15
mysql/mysql-router MySQL Router provides transparent routing be… 13
arey/mysql-client Run a MySQL client from a docker container 11 [OK]
imega/mysql-client Size: 36 MB, alpine:3.5, Mysql client: 10.1.… 8 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 4 [OK]
genschsa/mysql-employees MySQL Employee Sample Database 2 [OK]
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK]
jelastic/mysql An image of the MySQL database server mainta… 1
monasca/mysql-init A minimal decoupled init container for mysql 0
widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 0 [OK]
从docker hub上(阿里云加速)拉取mysql镜像到本地标签为5.6
[aaa@qq.com ~]# docker pull mysql:5.6
5.6: Pulling from library/mysql
8f91359f1fff: Pull complete
6bbb1c853362: Pull complete
e6e554c0af6f: Pull complete
f391c1a77330: Pull complete
414a8a88eabc: Pull complete
ced966b42b0e: Pull complete
509b7d623b06: Pull complete
94a03dec9143: Pull complete
d40a4af51b78: Pull complete
e01756c8536e: Pull complete
327b60c1a0e5: Pull complete
Digest: sha256:07ebe49dc810444e172c2b5a72ae1a23ad9f4942bfe70a7f0a578590da610579
Status: Downloaded newer image for mysql:5.6
docker.io/library/mysql:5.6
使用mysql5.6镜像创建容器(也叫运行镜像)
[aaa@qq.com ~]# docker run -p 12345:3306 --name mysql -v /ghj/mysql/conf:/etc/mysql/conf.d -v /ghj/mysql/logs:/logs -v /ghj/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6
5e67a99464d50661fa11c7d4d9294df0a0adea17a28826c2cbbf77880c54bff1
[aaa@qq.com ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e67a99464d5 mysql:5.6 "docker-entrypoint.s…" 9 minutes ago Up 9 minutes 0.0.0.0:12345->3306/tcp mysql
命令说明:
-p 12345:3306:将主机的12345端口映射到docker容器的3306端口。
–name mysql:运行服务器名字
-v /ghj/mysql/conf:/etc/mysql/conf.d:将主机/ghj/mysql目录下的/conf挂载到容器的/etc/mysql/conf.d
-v /ghj/mysql/logs:/logs:将主机/ghj/mysql目录下的logs目录挂载到容器的/logs
-v /ghj/mysql/data:/var/lib/mysql:将主机/ghj/mysql目录下的data目录挂载到容器的/var/lib/mysql
-e MYSQL_ROOT_PASSWORD=123456:初始化root用户的密码
-d mysql:5.6:后台程序运行mysql5.6
启动交互式运行:
[aaa@qq.com ~]# docker exec -it 5e67a99464d5 /bin/bash
aaa@qq.com:/# mysql -uroot -p
Enter password:
输入123456
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.01 sec)
mysql> create database db01;
Query OK, 1 row affected (0.00 sec)
mysql> use db01;
Database changed
mysql> create table my_book(id int not null primary key,bookName varchar(20) );
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
+----------------+
| Tables_in_db01 |
+----------------+
| my_book |
+----------------+
1 row in set (0.00 sec)
mysql> insert into my_book values(1,'java');
Query OK, 1 row affected (0.01 sec)
mysql> select * from my_book;
+----+----------+
| id | bookName |
+----+----------+
| 1 | java |
+----+----------+
1 row in set (0.00 sec)
外部Win10来连接运行在docker上的mysql服务
在可视化工具中添加一条数据,然后我们在去docker中查看
mysql> select * from my_book;
+----+----------+
| id | bookName |
+----+----------+
| 1 | java |
| 2 | C++ |
+----+----------+
2 rows in set (0.00 sec)
数据备份
[aaa@qq.com ~]# docker exec 5e67a99464d5 sh -c 'exec mysqldump --all-databases -uroot -p"123456" '>/ghj/all-databases.sql
[aaa@qq.com ~]# cd /ghj/
[aaa@qq.com ghj]# ll
总用量 3292
-rw-r--r--. 1 root root 3367587 10月 10 11:33 all-databases.sql
drwxr-xr-x. 3 root root 21 10月 8 16:17 mydockerfile
drwxr-xr-x. 5 root root 42 10月 10 10:47 mysql
上一篇: CentOS安装MySql5.7
下一篇: Mac下Docker安装