前言
本文使用docker安装常见数据库大部分没配置什么参数,只是基本的安装。
不只是数据库,还有elasticsearch、rabbitmq等和数据相关的服务。
- docker 版本: 18.06.3和19.03.9
- docker-compose 版本: 2.4.0
MySQL(v5.7)
- 下载docker镜像
docker pull mysql:5.7
- 创建并运行docker容器
# 映射宿主机3307端口到容器的3306端口 # 设置mysql的root密码为123456 # 映射宿主机的/home/heruo/mysql/data到容器的/var/lib/mysql docker run -it --name mysql-test -p 3307:3306\ -e MYSQL_ROOT_PASSWORD=123456 \ -v /home/heruo/mysql/data:/var/lib/mysql \ -v /home/heruo/mysql/conf/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf \ -d mysql:5.7
- docker-compose.yaml
version: "3" services: mysql: image: mysql:5.7 container_name: mysql-test volumes: - /home/heruo/mysql/data:/var/lib/mysql - /home/heruo/mysql/conf/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf - /etc/localtime:/etc/localtime:ro ports: - 3307:3306 environment: - MYSQL_ROOT_PASSWORD=123456
- mysqld.cnf示例
[mysqld] 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 max_allowed_packet = 32M lower_case_table_names=1 max_connections=2000 # 禁用查询缓存 query_cache_type=0 query_cache_size=0 # innodb缓冲池大小,此处设置为512MB. 512 * 1024 * 1024 innodb_buffer_pool_size=536870912 # binlog配置 #log-bin=mysql-bin #binlog-format=ROW #server-id=1 #binlog_ignore_db=information_schema,mysql,performance_schema,sys #expire_logs_days=30 sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
MariaDB
- 下载docker镜像
docker pull mariadb
- 创建并运行docker容器
# 映射宿主机4306端口到容器的3306端口 # 设置mariadb的root密码为123456 # 映射宿主机的/home/heruo/mariadb/data到容器的/var/lib/mysql docker run -it --name mariadb-test -p 4306:3306\ -e MARIADB_ROOT_PASSWORD=123456 \ -v /home/heruo/mariadb/data:/var/lib/mysql \ -d mariadb:latest
- docker-compose.yaml
version: "3" services: mariadb: image: mariadb:latest container_name: mariadb-test volumes: - /home/heruo/mariadb/data:/var/lib/mysql ports: - 4306:3306 environment: - MARIADB_ROOT_PASSWORD=123456
Redis(v6.2.4)
- 下载docker镜像
docker pull redis:6.2.4
- 创建并运行docker容器
docker run -p 6379:6379 --name redis-test -d redis:6.2.4
- docker-compose.yaml
version: "3" services: redis: image: redis:6.2.4 container_name: redis-6379 ports: - 6379:6379 volumes: - /home/apps/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf - /home/apps/redis/data:/data # 挂载redis.conf的话,需要指定启动命令中的配置文件路径 command: redis-server /usr/local/etc/redis/redis.conf
- redis.conf示例
port 6379 requirepass 123456 protected-mode no daemonize no appendonly yes aof-use-rdb-preamble yes
MongoDB
- 下载docker镜像
docker pull mongo
- 创建并运行docker容器
docker run -p 27017:27017 --name mongodb-test \ -v /home/heruo/mongodb/data:/data/db \ -e MONGO_INITDB_ROOT_USERNAME=root \ -e MONGO_INITDB_ROOT_PASSWORD=123456 \ -d mongo:latest
- docker-compose.yaml
version: "3" services: mongodb: image: mongo:latest container_name: mongodb-test ports: - 27017:27017 volumes: - /home/heruo/mongodb/data:/data/db environment: - MONGO_INITDB_ROOT_USERNAME=root - MONGO_INITDB_ROOT_PASSWORD=123456
elasticsearch(7.5.1)
- 下载docker镜像
docker pull elasticsearch:7.5.1
- 创建并运行docker容器
docker run -p 9200:9200 -p 9300:9300 --name es-test \ -e "discovery.type=single-node" \ -d elasticsearch:7.5.1
- docker-compose.yaml
version: "3" services: elasticsearch: image: elasticsearch:7.5.1 container_name: es-test ports: - 9200:9200 - 9300:9300 volumes: - /home/heruos/apps/elasticsearch/data/:/usr/share/elasticsearch/data - /home/heruos/apps/elasticsearch/logs/:/usr/share/elasticsearch/logs - /home/heruos/apps/elasticsearch/plugins/:/usr/share/elasticsearch/plugins environment: - "discovery.type=single-node" - "ES_JAVA_OPTS=-Xms512m -Xmx512m" ulimits: nofile: soft: 65535 hard: 65535
PostgreSQL
- 下载docker容器
docker pull postgres
- 创建并运行docker容器
docker run -p 5432:5432 --name postgresql \ -e POSTGRES_PASSWORD=123456 \ -v /home/heruos/apps/postgresql/data:/var/lib/postgresql/data \ -d postgres
- docker-compose.yaml
version: "3" services: postgresql: image: postgres:latest container_name: postgresql ports: - 5432:5432 volumes: - /home/heruos/apps/postgresql/data:/var/lib/postgresql/data environment: - "POSTGRES_PASSWORD=123456"
Oracle
待施工
DB2
- 拉取容器
docker pull 'ibmcom/db2'
- 创建容器并运行
# 默认实例拥有者是db2inst1 # 设置db2inst1的密码为db2inst1 docker run -itd --name mydb2 --privileged=true -p 50000:50000 -e LICENSE=accept -e DB2INST1_PASSWORD=db2inst1 -e DBNAME=testdb -v /home/apps/db2/data:/database ibmcom/db2
kafka
待施工
rocketmq
待施工
clickhouse
- 拉取镜像
docker pull clickhouse/clickhouse-server
- 创建容器并运行
# 基础创建方式 docker run -d --name some-clickhouse-server --ulimit nofile=262144:262144 clickhouse/clickhouse-server # 映射端口 docker run -d -p 18123:8123 -p19000:9000 --name clickhouse-server1 --ulimit nofile=262144:262144 clickhouse/clickhouse-server
- 容器中的数据目录为:
/var/lib/clickhouse/
- 容器中的日志目录为:
/var/log/clickhouse-server/
docker-compose.yaml示例:
version: "3" services: clickhouse: image: clickhouse/clickhouse-server:latest container_name: chserver volumes: - /home/apps/clickhouse/data:/var/lib/clickhouse - /home/apps/clickhouse/logs:/var/log/clickhouse-server ports: - 8123:8123 - 9000:9000 ulimits: nofile: soft: 262144 hard: 262144
rabbitmq
- 拉取docker镜像
# 带web端管理系统 docker pull rabbitmq:management
- 创建容器并运行
# 15672是web管理端访问端口 docker run --name rabbitmq -p 5672:5672 -p 15672:15672 \ -e RABBITMQ_DEFAULT_USER=rbmq \ -e RABBITMQ_DEFAULT_PASS=rbmq \ --hostname=rabbitmqhosta \ -v /home/rabbitmq/data:/var/lib/rabbitmq \ -d rabbitmq:management
memcache
- 拉取docker容器
docker pull memcached
- 创建容器并运行
# 映射11211端口,指定最大容量为128m。不设置的话,最大容量为64m docker run --name memcache -p 11211:11211 -d memcached -m 128
- 测试:
telnet 192.168.0.10 11211 # 访问成功后,执行 stats
etcd
待施工
influxdb2
- 拉取docker容器
docker pull influxdb:2.3.0
- 生成配置文件
# 创建挂载目录 mkdir -p /home/apps/influxdb/{conf,data} # 考虑到后续可能会修改配置,所以先把配置文件导出来了 docker run --rm influxdb:2.3.0 influxd print-config > /home/apps/influxdb/conf/config.yml
- 创建容器并运行
docker run --name influxdb -p 8086:8086 \ -v /home/apps/influxdb/data:/var/lib/influxdb2 \ -v /home/apps/influxdb/conf/config.yml:/etc/influxdb2/config.yml \ -d influxdb:2.3.0
- 测试。浏览器访问 IP:8086