赞
踩
Docker 要求 CentOS 系统的内核版本高于 3.10
通过 uname -r 命令查看你当前的内核版本
[root@centos-nacos /]# uname -r
3.10.0-1062.1.2.el7.x86_64
安装一些必要的系统工具:
命令:
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
添加软件源信息:
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
更新 yum 缓存:
sudo yum makecache fast
安装 Docker-ce:
sudo yum -y install docker-ce
启动 Docker 后台服务
sudo systemctl start docker
[root@localhost /]# vi /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"registry-mirrors": [
"https://k7da99jp.mirror.aliyuncs.com/",
"https://dockerhub.azk8s.cn",
"https://registry.docker-cn.com"
],
"storage-driver": "overlay2"
}
重启docker,并查看docker 信息
#重启docker [root@localhost /]# systemctl restart docker #查看docker信息 [root@localhost /]# docker info Client: Debug Mode: false Server: Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 19.03.1 Storage Driver: overlay2 Backing Filesystem: xfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: systemd Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: 894b81a4b802e4eb2a91d1ce216b8817763c29fb runc version: 425e105d5a03fabd737a126ad93d62a9eeede87f init version: fec3683 Security Options: seccomp Profile: default Kernel Version: 3.10.0-862.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 1 Total Memory: 1.796GiB Name: localhost.localdomain ID: 6FNH:LEUB:ZXWH:X4G7:IJMH:AIA7:LJIC:CGZC:4XUD:6E6H:WPSF:ZPI2 Docker Root Dir: /var/lib/docker Debug Mode: false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 #以下为添加的镜像加速器 Registry Mirrors: https://k7da99jp.mirror.aliyuncs.com/ https://dockerhub.azk8s.cn/ https://registry.docker-cn.com/ Live Restore Enabled: false WARNING: bridge-nf-call-iptables is disabled WARNING: bridge-nf-call-ip6tables is disabled
以安装mysql为例
查找镜像
[root@centos-nacos /]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 8713 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3048 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 645 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 63
拉取镜像
docker pull 名称
[root@centos-nacos /]# docker pull mysql # 默认拉取最新版本即8.0 版本的 Using default tag: latest latest: Pulling from library/mysql 80369df48736: Pull complete e8f52315cb10: Pull complete cf2189b391fc: Pull complete cc98f645c682: Pull complete 27a27ac83f74: Pull complete fa1f04453414: Pull complete d45bf7d22d33: Pull complete 3dbac26e409c: Pull complete 9017140fb8c1: Pull complete b76dda2673ae: Pull complete bea9eb46d12a: Pull complete e1f050a38d0f: Pull complete Digest: sha256:7345ce4ce6f0c1771d01fa333b8edb2c606ca59d385f69575f8e3e2ec6695eee Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest
[root@centos-nacos /]# docker pull mysql:5.7 #拉取指定版本即5.7 版本的数据库 5.7: Pulling from library/mysql 80369df48736: Already exists e8f52315cb10: Already exists cf2189b391fc: Already exists cc98f645c682: Already exists 27a27ac83f74: Already exists fa1f04453414: Already exists d45bf7d22d33: Already exists c7d49ffebc56: Pull complete 511a8052b204: Pull complete 5d5df4c12444: Pull complete d482603a2922: Pull complete Digest: sha256:44b33224e3c406bf50b5a2ee4286ed0d7f2c5aec1f7fdb70291f7f7c570284dd Status: Downloaded newer image for mysql:5.7 docker.io/library/mysql:5.7
查看拉取的镜像
[root@centos-nacos /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 cd3ed0dfff7e 5 days ago 437MB
mysql latest c8ee894bd2bd 5 days ago 456MB
REPOSITORY :镜像名称
TAG :镜像版本,latest 指最新版本
IMAGE ID :镜像ID
CREATED :创建时长
SIZE:镜像大小
运行镜像获取配置文件、日志文件及数据文件路径
[root@hadoop01 ~]# docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql [root@hadoop01 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f9f12cd5c68d mysql "docker-entrypoint.s…" 2 seconds ago Up 1 second 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp happy_rhodes [root@hadoop01 ~]# docker exec -it f9f12cd5c68d /bin/bash #查看mysql日志位置 bash-4.4# cd /var/log/ bash-4.4# ls mysqld.log bash-4.4# ls -l total 0 -rw-r-----. 1 mysql mysql 0 Oct 12 21:38 mysqld.log #查看mysql二进制位置 bash-4.4# cd /var/lib/mysql mysql/ mysql-files/ mysql-keyring/ #查看mysql配置文件位置 bash-4.4# cd /etc/mysql/ bash-4.4# ls conf.d #下载日志文件和配置文件 [root@hadoop01 ~]# docker cp f9f12cd5c68d:/var/log/mysqld.log /mydata/mysql-master/log/ [root@hadoop01 ~]# docker cp a4e3bed3be0c:/etc/mysql/conf.d /mydata/mysql-master/conf [root@hadoop01 ~]# docker rm -f happy_rhodes #删掉刚刚的创建的容器
运行镜像并映射数据卷到本地硬盘
[root@centos-nacos /]# docker run -p 3306:3306 --privileged=true --restart=always --name mysql5.7 -v /usr/local/docker/mysql/8.0-01/conf:/etc/mysql -v /usr/local/docker/mysql/8.0-01/log:/var/log -v /usr/local/docker/mysql/8.0-01/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
#运行成功出现以下结果
84ef62005aad2cc2dd0aea1ab9170495e488eaded0e3c21f4861586f5147337b
-p 3306:3306:将容器的 3306 端口映射到主机的 3306 端口。
–privileged=true:赋予容器内部运行的mysql超级用户权限。
–restart=always:容器随docker自启动。
–name mysql5.7:给容器命名。
-v /usr/local/docker/mysql/8.0-01/conf:/etc/mysql:将主机当前目录下的 conf/my.cnf 挂载到容器的 /etc/mysql/my.cnf。
-v /usr/local/docker/mysql/8.0-01/log:/var/log:将主机当前目录下的 log 目录挂载到容器的 /log。
-v /usr/local/docker/mysql/8.0-01/data:/var/lib/mysql :将主机当前目录下的data目录挂载到容器的 /var/lib/mysql
**-e MYSQL_ROOT_PASSWORD=123456:**初始化 root 用户的密码。
-d mysql:5.7 : 从mysql:5.7镜像创建容器并在后台运行。
查询已启动的容器
[root@centos-nacos /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
658c3bb9cd2b mysql:5.7 "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql5.7
连接数据库
再启动mysql 最新版本的,由于我们启动5.7 占用了3306 端口,所以我们要更改映射端口号为3307
[root@centos-nacos /]# docker run -p 3307:3306 --name mysql8.0 -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest
2cff2731d3042fe3d13b8ee1466ff6ba9b71881e545b188007e40ea307e8a266
查看正在运行的镜像,分别运行了两个不同版本的数据库
[root@centos-nacos /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2cff2731d304 mysql:latest "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp mysql8.0
658c3bb9cd2b mysql:5.7 "docker-entrypoint.s…" 12 minutes ago Up 26 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql5.7
连接数据库,报错,由于mysql8.0 之后密码加密方式进行了更改
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fpkpbgiX-1638932168087)(D:\d-hs-logs\images\20191023111326.png)]
则需进入容器中修改密码
[root@centos-nacos /]# docker exec -it 2cff2731d304 /bin/bash #进入容器
登录mysql 并进行更改
root@4d3fbee0d162:/# mysql -u root -p #登录
Enter password: #输入密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.18 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> grant all PRIVILEGES on *.* to 'root'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.01 sec)
mysql> ALTER user 'root'@'%' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.01 sec)
mysql> ALTER user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> FLUSH PRIVILEGES;#使修改生效
Query OK, 0 rows affected (0.02 sec)
mysql> exit;
重新登录
问题:
mysql 5.7 + 及以上 出现mysql sql_mode=only_full_group_by
ONLY_FULL_GROUP_BY:对于GROUP BY聚合操作,如果在 SELECT 中的列,没有在 GROUP BY 中出现,那么这个SQL是不合法的,因为列不在GROUP BY从句中。
解决方案修改 etc/my.cnf
[root@centos-nacos conf]# vi my.cnf
[mysqld]
character-set-server=utf8
#加入以下内如即可 即sql_mode 中去除了 only_full_group_by
#5.7 版本加这句
sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
#8.0 版本加这句
sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"
注5.7 版本和8.0 版本的sql_mode不一样
可以试用工具查询
#全局查询,然后复制里面的值出来,删除掉only_full_group_by 即可
SELECT @@GLOBAL.sql_mode;
docker version
docker build -t image-name docker-file-location
docker run -d image-name
docker images
docker ps -l
docker ps -a
docker stop container_id
docker rmi image-name
docker rmi $(docker images -q)
docker rmi -f $(docker images -q)
docker rmi $(docker images -q -f dangling=true)
docker rm $(docker ps -a -q)
docker exec -it container-id /bin/bash
docker volume ls
docker volume rm [volume_name]
docker volume rm $(docker volume ls -qf dangling=true)
docker cp host_path containerID:container_path
docker cp containerID:container_path host_path
docker run -d --name gitlab-runner --restart always -v /srv/gitlab-runner/config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:latest
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。