【MySQL】Docker搭建MySQL8.0
东瑜 人气:4
[toc]
## Docker搭建MySQL8.0
### 目的:
对Docker有一个感性的认识,然后运用docker做一些实验和测试工作。
Docker是dotCloud开源的一个使用Go语言编写的基于Linux容器(Linux Containers,LXC)的容器引擎。
看到Docker的那个官方图,确实可以比喻为:宿主机Linux是一个运输船,那么Docker就是一个个相对独立的集装箱,你不用管集装箱里面装的是什么的。而Docker也是如黑盒子一般,提供着各种各样的服务,如:MySQL,Oracle,Nginx,tomcat等等那些耳熟能详的中间件,数据库等服务。但不用管里面的具体细节。
同样,Docker也可以搭建各种各样的程序员的开发环境,如:java,python,php,c等基础构件环境,方便我们站在巨人的肩膀上,继续前行。
当然它也有不错的优点:
* 性能卓越:Docker是一个“操作系统级别的虚拟化”,可以达到秒级启动。查看【Comparing Virtual Machines and Linux Containers Performance 】论文摘要。(地址:https://www.infoq.com/news/2014/08/vm-containers-performance/)对 CPU、内存、网络和 I/O 做了benchmark。同时,在KVM和Docker不同环境之下,运行mysql和redis做数据的采样,Redis 侧重于网络栈的性能。而 MySQL 侧重于内存,网络和文件系统的性能。并且提出一些质疑,建议在物理的 Linux 服务器上运行Linux Containers。
* 可移植性:便于迁移部署,减少运维和迭代的时间。
### 1.安装Docker
环境:`14.04.5 LTS`
由于docker安装可以直接看官方文档,部署简单。不赘述。Docker安装成功后,查看版本:
```
root@ubuntudoc:~# docker --version
Docker version 18.06.3-ce, build d7080c1
```
### 2.查看docker镜像
```
root@ubuntudoc:~# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 9238 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3294 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 681 [OK]
```
### 3.拉取mysql官方镜像
```
docker pull mysql
```
### 4. 查看目前的镜像
```
root@ubuntudoc:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 6678c7c2e56c 10 days ago 127MB
mysql latest 9b51d9275906 10 days ago 547MB
dongweiming/web_develop dev 43fb02d9c1a3 3 years ago 294MB
```
### 5.运行docker mysql镜像
```
docker run -p 3306:3306 --name zsdmysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql
```
上述命令的参数,有如下含义:
1. `--name`指定了你要取的名字。
2. `-p`对应,需要映射出来的端口。比如:3306:3306,意识表示为zsdmysql的容器里面的3306端口对应我外面这个虚拟机的3306端口。
3. `-e`是mysql的命令,设置root的密码为123456
4. `-d`是运行的镜像,这里是`mysql` 容器镜像
### 6.查看目前运行的容器
```
root@ubuntudoc:~# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3958ab15ea05 mysql "docker-entrypoint.s…" 8 seconds ago Up 6 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp zsdmysql
```
### 7.进入MySQL
````
root@ubuntudoc:~# docker exec -it zsdmysql bash
### 下述代表容器里面的情况了
root@3958ab15ea05:/# df -h
Filesystem Size Used Avail Use% Mounted on
overlay 53G 3.7G 47G 8% /
tmpfs 64M 0 64M 0% https://img.qb5200.com/download-x/dev
tmpfs 1000M 0 1000M 0% /sys/fs/cgroup
https://img.qb5200.com/download-x/devhttps://img.qb5200.com/download-x/dm-0 53G 3.7G 47G 8% /etc/hosts
shm 64M 0 64M 0% https://img.qb5200.com/download-x/dev/shm
tmpfs 1000M 0 1000M 0% /proc/acpi
tmpfs 1000M 0 1000M 0% /proc/scsi
tmpfs 1000M 0 1000M 0% /sys/firmware
root@3958ab15ea05:/# mysql -uroot -p123456
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 8
Server version: 8.0.19 MySQL Community Server - GPL
Copyright (c) 2000, 2020, 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>
````
---------------------------------------------------分割线--------------------------------------------------------
### 8. 建立目录映射的MySQL容器
```
docker run -p 3307:3306 --name zsdmysql02 \
-v /opt/mysql/conf:/etc/mysql \
-v /opt/mysql/logs:/var/log/mysql \
-v /opt/mysqlhttps://img.qb5200.com/download-x/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql
```
### 9.通过tree命令,可以看到数据目录映射为主机的目录
```
root@ubuntudoc:/opt/mysql# tree
.
|-- conf
|-- data
| |-- #innodb_temp
| |-- auto.cnf
| |-- ca-key.pem
| |-- ca.pem
| |-- client-cert.pem
| |-- client-key.pem
| |-- ib_buffer_pool
| |-- ib_logfile0
| |-- ib_logfile1
| |-- ibdata1
| |-- mysql
| | |-- general_log.CSM
| | |-- general_log.CSV
| | |-- general_log_201.sdi
| | |-- slow_log.CSM
| | |-- slow_log.CSV
| | `-- slow_log_202.sdi
| |-- mysql.ibd
| |-- performance_schema
| | |-- accounts_138.sdi
| | |-- cond_instances_77.sdi
| | |-- data_lock_waits_154.sdi
| | |-- data_locks_153.sdi
| | |-- events_errors_su_132.sdi
| | |-- events_errors_su_133.sdi
| | |-- events_errors_su_134.sdi
| | |-- events_errors_su_135.sdi
| | |-- events_errors_su_136.sdi
| | |-- events_stages_cu_104.sdi
| | |-- events_stages_hi_105.sdi
| | |-- events_stages_hi_106.sdi
| | |-- events_stages_su_107.sdi
| | |-- events_stages_su_108.sdi
| | |-- events_stages_su_109.sdi
| | |-- events_stages_su_110.sdi
| | |-- events_stages_su_111.sdi
| | |-- events_statement_112.sdi
| | |-- events_statement_113.sdi
| | |-- events_statement_114.sdi
| | |-- events_statement_115.sdi
| | |-- events_statement_116.sdi
| | |-- events_statement_117.sdi
| | |-- events_statement_118.sdi
| | |-- events_statement_119.sdi
| | |-- events_statement_120.sdi
| | |-- events_statement_121.sdi
| | |-- events_statement_122.sdi
| | |-- events_statement_123.sdi
| | |-- events_transacti_124.sdi
| | |-- events_transacti_125.sdi
| | |-- events_transacti_126.sdi
| | |-- events_transacti_127.sdi
| | |-- events_transacti_128.sdi
| | |-- events_transacti_129.sdi
| | |-- events_transacti_130.sdi
| | |-- events_transacti_131.sdi
| | |-- events_waits_cur_78.sdi
| | |-- events_waits_his_79.sdi
| | |-- events_waits_his_80.sdi
| | |-- events_waits_sum_81.sdi
| | |-- events_waits_sum_82.sdi
| | |-- events_waits_sum_83.sdi
| | |-- events_waits_sum_84.sdi
| | |-- events_waits_sum_85.sdi
| | |-- events_waits_sum_86.sdi
| | |-- file_instances_87.sdi
| | |-- file_summary_by__88.sdi
| | |-- file_summary_by__89.sdi
| | |-- global_status_172.sdi
| | |-- global_variables_175.sdi
| | |-- host_cache_90.sdi
| | |-- hosts_139.sdi
| | |-- keyring_keys_145.sdi
| | |-- log_status_165.sdi
| | |-- memory_summary_b_147.sdi
| | |-- memory_summary_b_148.sdi
| | |-- memory_summary_b_149.sdi
| | |-- memory_summary_b_150.sdi
| | |-- memory_summary_g_146.sdi
| | |-- metadata_locks_152.sdi
| | |-- mutex_instances_91.sdi
| | |-- objects_summary__92.sdi
| | |-- performance_time_93.sdi
| | |-- persisted_variab_178.sdi
| | |-- prepared_stateme_166.sdi
| | |-- replication_appl_158.sdi
| | |-- replication_appl_159.sdi
| | |-- replication_appl_160.sdi
| | |-- replication_appl_161.sdi
| | |-- replication_appl_163.sdi
| | |-- replication_appl_164.sdi
| | |-- replication_conn_155.sdi
| | |-- replication_conn_157.sdi
| | |-- replication_grou_156.sdi
| | |-- replication_grou_162.sdi
| | |-- rwlock_instances_94.sdi
| | |-- session_account__144.sdi
| | |-- session_connect__143.sdi
| | |-- session_status_173.sdi
| | |-- session_variable_176.sdi
| | |-- setup_actors_95.sdi
| | |-- setup_consumers_96.sdi
| | |-- setup_instrument_97.sdi
| | |-- setup_objects_98.sdi
| | |-- setup_threads_99.sdi
| | |-- socket_instances_140.sdi
| | |-- socket_summary_b_141.sdi
| | |-- socket_summary_b_142.sdi
| | |-- status_by_accoun_168.sdi
| | |-- status_by_host_169.sdi
| | |-- status_by_thread_170.sdi
| | |-- status_by_user_171.sdi
| | |-- table_handles_151.sdi
| | |-- table_io_waits_s_100.sdi
| | |-- table_io_waits_s_101.sdi
| | |-- table_lock_waits_102.sdi
| | |-- threads_103.sdi
| | |-- user_defined_fun_179.sdi
| | |-- user_variables_b_167.sdi
| | |-- users_137.sdi
| | |-- variables_by_thr_174.sdi
| | `-- variables_info_177.sdi
| |-- private_key.pem
| |-- public_key.pem
| |-- server-cert.pem
| |-- server-key.pem
| |-- sys
| | `-- sys_config.ibd
| |-- undo_001
| `-- undo_002
`-- logs
7 directories, 126 files
```
加载全部内容