Docker安装mysql


  • 拉取镜像(mysql:5.7)

$ docker pull mysql:5.7

  • 查看镜像

$ docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.7                 84164b03fa2e        11 days ago         456MB
  • 创建一个临时的mysql,以便复制出my.cnf等数据
docker run --rm \
--name mysqlTemp \
--privileged=true \
-it \
-v /usr/local/mysql/:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456  \
mysql:5.7 \
/bin/bash

命令说明:
1. -v /usr/local/mysql/:/var/lib/mysql  将主机目录/usr/local/mysql/映射到Docker中的/var/lib/mysql目录
2. -- rm 退出后就删除该容器
3. -it 交互模式
4. /bin/bash 进入 bash 命令模式
  • 复制出docker中的配置文件

复制docker容器中的配置到/var/lib/mysql目录中,也就是/usr/local/mysql中,因为这两个目录已经作了映射

cp /etc/my.cnf /var/lib/mysql
cp -R /etc/mysql/conf.d/ /var/lib/mysql/
cp -R /etc/mysql/mysql.conf.d/ /var/lib/mysql/
  • 退出临时容器

exit

  • 进入宿主机

到/usr/local/mysql目录中修改mysql配置:修改/usr/local/mysql/mysql.conf.d/mysqld.cnf,如果内网访问慢,添加skip-name-resolve配置

[mysqld]
skip-name-resolve
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
log-error       = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address   = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
  • 修改日志目录权限
mkdir -p /usr/local/mysql/log/
chown -R 999:999 /usr/local/mysql/log/
  • 运行镜像

-v 挂载本地目录到docker中
-d 表示以守护进程模式运行
-m 限制容器可以使用的内存

docker run \
--name mysql \
--restart always \
--privileged=true \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=123456 \
-m 512M --memory-swap=1024M \
-v /usr/local/mysql/data:/var/lib/mysql \
-v /usr/local/mysql/log:/var/log/mysql \
-v /usr/local/mysql/my.cnf:/etc/mysql/my.cnf \
-v /usr/local/mysql/conf.d:/etc/mysql/conf.d \
-v /usr/local/mysql/mysql.conf.d:/etc/mysql/mysql.conf.d \
-d mysql:5.7
  • 进入运行着的mysql容器
#查询当前 mysql 容器id
[root@xiaodxserver local]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
2ee0aadc1cc2        mysql:5.7           "docker-entrypoint.s…"   12 minutes ago      Up 12 minutes       0.0.0.0:3306->3306/tcp, 33060/tcp   mysql

#进入容器中执行命令
[root@xiaodxserver local]# docker exec -it mysql /bin/bash
root@2ee0aadc1cc2:/#
root@2ee0aadc1cc2:/# 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 2
  Server version: 5.7.29 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> show databases;
  +--------------------+
  | Database           |
  +--------------------+
  | information_schema |
  | mysql              |
  | performance_schema |
  | sys                |
  +--------------------+
  4 rows in set (0.00 sec)

mysql> exit

安装zipkin

  • 在mysql数据库中创建zipkin数据库,并创建初始化数据表
create database if not exists zipkin default charset utf8 collate utf8_general_ci;
create user zipkin@'%' identified by 'zipkin123';
grant all privileges on zipkin.* to zipkin@'%';
flush privileges;
--
-- Copyright 2015-2019 The OpenZipkin Authors
--
-- Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
-- in compliance with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software distributed under the License
-- is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
-- or implied. See the License for the specific language governing permissions and limitations under
-- the License.
--

CREATE TABLE IF NOT EXISTS zipkin_spans (
  `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
  `trace_id` BIGINT NOT NULL,
  `id` BIGINT NOT NULL,
  `name` VARCHAR(255) NOT NULL,
  `remote_service_name` VARCHAR(255),
  `parent_id` BIGINT,
  `debug` BIT(1),
  `start_ts` BIGINT COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',
  `duration` BIGINT COMMENT 'Span.duration(): micros used for minDuration and maxDuration query',
  PRIMARY KEY (`trace_id_high`, `trace_id`, `id`)
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;

ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTracesByIds';
ALTER TABLE zipkin_spans ADD INDEX(`name`) COMMENT 'for getTraces and getSpanNames';
ALTER TABLE zipkin_spans ADD INDEX(`remote_service_name`) COMMENT 'for getTraces and getRemoteServiceNames';
ALTER TABLE zipkin_spans ADD INDEX(`start_ts`) COMMENT 'for getTraces ordering and range';

CREATE TABLE IF NOT EXISTS zipkin_annotations (
  `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
  `trace_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.trace_id',
  `span_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.id',
  `a_key` VARCHAR(255) NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1',
  `a_value` BLOB COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB',
  `a_type` INT NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation',
  `a_timestamp` BIGINT COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp',
  `endpoint_ipv4` INT COMMENT 'Null when Binary/Annotation.endpoint is null',
  `endpoint_ipv6` BINARY(16) COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',
  `endpoint_port` SMALLINT COMMENT 'Null when Binary/Annotation.endpoint is null',
  `endpoint_service_name` VARCHAR(255) COMMENT 'Null when Binary/Annotation.endpoint is null'
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;

ALTER TABLE zipkin_annotations ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) COMMENT 'Ignore insert on duplicate';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`, `span_id`) COMMENT 'for joining with zipkin_spans';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTraces/ByIds';
ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`) COMMENT 'for getTraces and getServiceNames';
ALTER TABLE zipkin_annotations ADD INDEX(`a_type`) COMMENT 'for getTraces and autocomplete values';
ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces and autocomplete values';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id`, `span_id`, `a_key`) COMMENT 'for dependencies job';

CREATE TABLE IF NOT EXISTS zipkin_dependencies (
  `day` DATE NOT NULL,
  `parent` VARCHAR(255) NOT NULL,
  `child` VARCHAR(255) NOT NULL,
  `call_count` BIGINT,
  `error_count` BIGINT,
  PRIMARY KEY (`day`, `parent`, `child`)
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
  • 运行镜像
docker run -d \
--restart always \
-p 9411:9411 \
-v /etc/localtime:/etc/localtime:ro \
-e MYSQL_USER=zipkin \
-e MYSQL_PASS=zipkin123 \
-e MYSQL_HOST=127.0.0.1 \
-e STORAGE_TYPE=mysql \
-e MYSQL_DB=zipkin \
-e MYSQL_TCP_PORT=3306 \
--net host \
--name zipkin \
openzipkin/zipkin
docker run -d \
--restart always \
-p 9411:9411 \
-e STORAGE_TYPE=elasticsearch \
-e ES_HOSTS=172.16.32.115:9200 \
-e ES_USERNAME=elastic \
-e ES_PASSWORD=elastic \
-e ES_DATE_SEPARATOR=. \
--net host \
--name zipkin \
openzipkin/zipkin

文章作者: jhzhang_09
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 jhzhang_09 !
评论
  目录