docker学习4-docker安装mysql环境

Posted yoyoketang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker学习4-docker安装mysql环境相关的知识,希望对你有一定的参考价值。

前言

docker安装mysql环境非常方便,简单的几步操作就可以了

拉取mysql镜像

先拉取mysql的镜像,可以在docker的镜像仓库找到不同TAG标签的版本https://hub.docker.com/_/mysql?tab=tags
我这里选择mysql:5.7标签

[[email protected] ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
fc7181108d40: Already exists 
787a24c80112: Already exists 
a08cb039d3cd: Already exists 
4f7d35eb5394: Already exists 
5aa21f895d95: Already exists 
a742e211b7a2: Pull complete 
0163805ad937: Pull complete 
62d0ebcbfc71: Pull complete 
559856d01c93: Pull complete 
c849d5f46e83: Pull complete 
f114c210789a: Pull complete 
Digest: sha256:c3594c6528b31c6222ba426d836600abd45f554d078ef661d3c882604c70ad0a
Status: Downloaded newer image for mysql:5.7

docker images查看已经下载成功

REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
centos                     v7.5                254d4dfe9df7        3 days ago          200MB
mysql                      5.7                 a1aa4f76fab9        2 weeks ago         373MB
centos/python-36-centos7   latest              b8d15efaa8ec        2 months ago        651MB
centos                     centos7.5.1804      cf49811e3cdb        3 months ago        200MB
ubuntu                     15.10               9b9cb95443b5        2 years ago         137MB
training/webapp            latest              6fae60ef3446        4 years ago         349MB
[[email protected] ~]# 

配置mysql

创建mysql目录,用于存放mysql相关配置及数据

  • mkdir : 如果存在上级目录不存在的情况,会无法创建下级目录, 会提出ERROR
  • mkdir -p :递归创建目录,即使上级目录不存在,会按目录层级自动创建目录
  • ~ : ”/“是根目录,”~“是家目录。Linux存储是以挂载的方式,相当于是树状的,源头就是”/“,也就是根目录。
    而每个用户都有”家“目录,也就是用户的个人目录,比如root用户的”家“目录就是/root,普通用户a的家目录就是/home/a

mkdir -p ~/yoyo/mysql/conf ~/yoyo/mysql/data ~/yoyo/mysql/logs

创建完成后,在当前用户的home目录(root用户在root目录),会有三个文件夹

[[email protected] ~]# mkdir -p ~/yoyo/mysql/conf ~/yoyo/mysql/data ~/yoyo/mysql/logs
[[email protected] mysql]# ll ~/yoyo/mysql
total 12
drwxr-xr-x 2 root root 4096 Jun 30 10:31 conf
drwxr-xr-x 2 root root 4096 Jun 30 10:31 data
drwxr-xr-x 2 root root 4096 Jun 30 10:31 logs

运行mysql容器

docker run -p 3309:3306 --name yoyomysql -v ~/yoyo/mysql/conf:/etc/mysql/conf.d -v ~/yoyo/mysql/logs:/logs -v ~/yoyo/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

命令说明:

  • --name 重命名yoyomysql
  • -p 3309:3306:将容器的 3306 端口映射到主机的 3309 端口。
  • -v -v ~/yoyo/mysql/conf:/etc/mysql/conf.d:将主机~/yoyo/mysql/conf 挂载到容器的 /etc/mysql/my.cnf。
  • -v ~/yoyo/mysql/logs:/logs:将主机~/yoyo/mysql 目录挂载到容器的 /logs。
  • -v ~/yoyo/mysql/data:/var/lib/mysql :将主机~/yoyo/mysql/data目录挂载到容器的 /var/lib/mysql 。
  • -e MYSQL_ROOT_PASSWORD=123456:初始化 root 用户的密码。
  • -d 挂后台运行

启动完成后,查看运行状态

[[email protected] mysql]# docker run -p 3308:3306 --name yoyomysql -v ~/yoyo/mysql/conf:/etc/mysql/conf.d -v ~/yoyo/mysql/logs:/logs -v ~/yoyo/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
581137afbd3ba22dc02bf89d3ec1afb6e51d55eed0d99a3a4a979bdee8d5de4a
[[email protected] ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
581137afbd3b        mysql:5.7           "docker-entrypoint.s…"   4 minutes ago       Up 4 minutes       33060/tcp, 0.0.0.0:3309->3306/tcp   yoyomysql

查看~/yoyo/mysql/data目录会看到有数据

[[email protected] ~]# ll ~/yoyo/mysql/data/
total 188476
-rw-r----- 1 polkitd ssh_keys       56 Jun 30 10:38 auto.cnf
-rw------- 1 polkitd ssh_keys     1679 Jun 30 10:38 ca-key.pem
-rw-r--r-- 1 polkitd ssh_keys     1107 Jun 30 10:38 ca.pem
-rw-r--r-- 1 polkitd ssh_keys     1107 Jun 30 10:38 client-cert.pem
-rw------- 1 polkitd ssh_keys     1675 Jun 30 10:38 client-key.pem
-rw-r----- 1 polkitd ssh_keys     1340 Jun 30 10:38 ib_buffer_pool
-rw-r----- 1 polkitd ssh_keys 79691776 Jun 30 10:38 ibdata1
-rw-r----- 1 polkitd ssh_keys 50331648 Jun 30 10:38 ib_logfile0
-rw-r----- 1 polkitd ssh_keys 50331648 Jun 30 10:38 ib_logfile1
-rw-r----- 1 polkitd ssh_keys 12582912 Jun 30 10:38 ibtmp1
drwxr-x--- 2 polkitd ssh_keys     4096 Jun 30 10:38 mysql
drwxr-x--- 2 polkitd ssh_keys     4096 Jun 30 10:38 performance_schema
-rw------- 1 polkitd ssh_keys     1679 Jun 30 10:38 private_key.pem
-rw-r--r-- 1 polkitd ssh_keys      451 Jun 30 10:38 public_key.pem
-rw-r--r-- 1 polkitd ssh_keys     1107 Jun 30 10:38 server-cert.pem
-rw------- 1 polkitd ssh_keys     1675 Jun 30 10:38 server-key.pem
drwxr-x--- 2 polkitd ssh_keys    12288 Jun 30 10:38 sys

进入容器bash

输入docker exec -it mysql bash进入容器bash,接着输入mysql -uroot -p输入root用户密码123456进入mysql

[[email protected] data]# docker exec -i -t yoyomysql bash
[email protected]:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \\g.
Your MySQL connection id is 4
Server version: 5.7.26 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之后所有指令结尾都需要 ;

  • show databases; 查看所有的库
  • use 某个数据库, 如 use mysql;
  • select 查询语句;
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host, user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | root          |
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
4 rows in set (0.00 sec)

mysql> exit
Bye
[email protected]:/# exit
exit

退出mysql和容器用exit

连接mysql测试

技术图片

以上是关于docker学习4-docker安装mysql环境的主要内容,如果未能解决你的问题,请参考以下文章

使用docker快速架构Android系统源码编译环境

ApplicationDocker 安装 MySQL

Centos7.4—Docker容器安装配置与基本操作

理解Docker:Docker 容器使用 Linux namespace 进行运行环境隔离

Docker启动MySQL

Docker启动MySQL