Docker安装MySQL
os:CentOS7.3
一、安装docker
Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker 。
# uname -r
3.10.0-514.el7.x86_64
使用 root 权限登录 Centos,确保 yum 包更新到最新。
# yum update
3、卸载旧版本(如果安装过旧版本的话)
# yum remove docker docker-common docker-selinux docker-engine
4、安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
# yum install -y yum-utils device-mapper-persistent-data lvm2
5、设置yum源
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
6、可以查看所有仓库中所有docker版本,并选择特定版本安装
# yum list docker-ce --showduplicates | sort -r
已加载插件:fastestmirror, priorities
已安装的软件包
可安装的软件包
* updates: mirrors.huaweicloud.com
Loading mirror speeds from cached hostfile
* extras: mirrors.huaweicloud.com
* epel: mirrors.huaweicloud.com
docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.0-3.el7 @docker-ce-stable
docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.3.ce-1.el7 docker-ce-stable
docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable
* base: mirrors.huaweicloud.com
7、安装docker
$ yum install docker-ce #由于repo中默认只开启stable仓库,故这里安装的是最新稳定版
$ yum install <FQPN> # 例如:yum install docker-ce-18.06.1.ce
8、启动并加入开机启动
# systemctl start docker
# systemctl enable docker
9、验证安装是否成功(有client和service两部分表示docker安装启动都成功了)
# docker version
Client:
Version: 18.09.0
API version: 1.39
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:48:22 2018
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.0
API version: 1.39 (minimum version 1.12)
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:19:08 2018
OS/Arch: linux/amd
二:Docker 安装 MySQL
1:docker pull mysql查找Docker Hub上的mysql镜像
# docker search mysql
拉取官方的镜像,标签为5.7
# docker pull mysql:5.7
等待下载完成后,可以在本地镜像列表里查到REPOSITORY为mysql,标签为5.7的镜像。
# docker images |grep mysql
mysql 5.7 ae6b78bedf88 5 weeks ago 372MB
2:根据镜像创建容器:
# docker create -it mysql:5.7
f36bb5730eab124df75ba4b72249f2636f8b4556d44fb7599f8dd8c632fb80e5
3:启动MySQL容器
# docker run --name mysqlserver -e MYSQL_ROOT_PASSWORD=root -d -i -p 33071:33071 mysql:5.7
0c3aa2595d15a88c24152b6356335fb95e059474dc6ef4ed09a106dfe339cdea
命令说明:
-p 33071:33071:将容器的33071端口映射到主机的33071端口
-v -v $PWD/conf:/etc/mysql/conf.d:将主机当前目录下的 conf/my.cnf 挂载到容器的 /etc/mysql/my.cnf
-v $PWD/logs:/logs:将主机当前目录下的 logs 目录挂载到容器的 /logs
-v $PWD/data:/var/lib/mysql :将主机当前目录下的data目录挂载到容器的 /var/lib/mysql
-e MYSQL_ROOT_PASSWORD=root:初始化 root 用户的密码。
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0c3aa2595d15 mysql:5.7 "docker-entrypoint.s…" 5 seconds ago Up 3 seconds 3306/tcp, 33060/tcp, 0.0.0.0:33071->3371/tcp mysqlserver
4:进入MySQL终端
# docker exec -it 0c3aa2595d15 /bin/bash
root@0c3aa2595d15:/#
root@0c3aa2595d15:/# mysql -h 127.0.0.1 -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.7.24 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
实现远程连接:
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
mysql> FLUSH PRIVILEGES;
其他:
1:查看所有容器(启动状态或者关闭状态)
$ sudo docker ps -a
2:启动和关闭容器
启动命令:
$ sudo docker start mysqlserver //通过指定容器名字
$ sudo docker start 0c3aa2595d15 //通过指定容器ID
关闭命令:
$ sudo docker stop mysqlserver //通过指定容器名字
$ sudo docker stop 0c3aa2595d15 //通过指定容器ID
3.修改MySQL配置文件有两种方法:
一:是进入容器,修改容器里的MySQL的配置文件,然后重新启动容器,例如:
$ sudo docker exec -it mysqlserver /usr/bin/bash
然后可以进入容器的命令行模式,接着修改 /etc/mysql/my.cnf 文件即可
二:是挂载主机的mysql配置文件,官方文档如下:
The MySQL startup configuration is specified in the file /etc/mysql/my.cnf, and that file in turn includes any files found in the /etc/mysql/conf.d directory that end with .cnf. Settings in files in this directory will augment and/or override settings in /etc/mysql/my.cnf. If you want to use a customized MySQL configuration, you can create your alternative configuration file in a directory on the host machine and then mount that directory location as /etc/mysql/conf.d inside the mysql container.
If /my/custom/config-file.cnf is the path and name of your custom configuration file, you can start your mysql container like this (note that only the directory path of the custom config file is used in this command):
$ docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
This will start a new container some-mysql where the MySQL instance uses the combined startup settings from /etc/mysql/my.cnf and /etc/mysql/conf.d/config-file.cnf, with settings from the latter taking precedence.