Skip to content

部署 MySQL

  1. 创建配置文件和目录
sh
# 创建配置文件和初始化脚本(初始化脚本是可选的)
mkdir ./mysql_conf ./mysql_init
touch ./mysql_conf/mysqld.cnf
touch ./mysql_init/init.sql
  1. docker-compose.yaml 和配置文件内容
yaml
services:
  mysql_server:
    image: mysql:8  # mysql8
    # image: mysql:5.7  # mysql5.7
    container_name: mysql_server
    ports:
      - 3306:3306
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root_password # root 用户密码
      MYSQL_DATABASE: init_db            # 初始数据库
      MYSQL_USER: init_user              # 初始用户
      MYSQL_PASSWORD: init_user_password # 初始用户密码
    volumes:
      - ./mysql_conf:/etc/mysql/conf.d            # mysql 配置
      - ./mysql_data:/var/lib/mysql               # mysql 数据
      - ./mysql_init:/docker-entrypoint-initdb.d/ # 初始化脚本(只会在启动时执行)
ini
[mysqld]
; allow all host to connect
bind-address = '0.0.0.0'
sql
-- 初始化数据库的脚本, 创建一个数据库 demos
CREATE DATABASE IF NOT EXISTS `demos` CHARACTER SET utf8mb4;

部署 postgres

  1. 创建初始化脚本 & 允许远程连接
sh
mkdir ./postgres_init
touch ./postgres_init/init.sql
  1. 设置宿主机器的防火墙
sh
# 5433: 是宿主机的端口号, 注意要和 docker-compose.yaml 中端口号一致
iptables -A INPUT -p tcp --dport 5433 -j ACCEPT
  1. docker-compose.yaml 和 init.sql 内容
yaml
services:

  postgres_server:
    image: postgres:14
    restart: always
    container_name: postgres_server
    environment:
      POSTGRES_USER: root              # 用户
      POSTGRES_PASSWORD: root_password # 用户密码
      POSTGRES_HOST_AUTH_METHOD: trust # 允许所有用户访问
    ports:
      - 5433:5432
      # 注意需要设置宿主机的防火墙: iptables -A INPUT -p tcp --dport 5433 -j ACCEPT
    volumes:
      - ./postgres_data:/var/lib/postgresql/data     # mysql 数据
      - ./postgres_init:/docker-entrypoint-initdb.d/ # 初始化脚本(只会在启动时执行)
sql
-- 创建一个 demos 数据库
CREATE DATABASE demos
  WITH 
  OWNER = root
  ENCODING = 'UTF8'
  LC_COLLATE = 'en_US.utf8'
  LC_CTYPE = 'en_US.utf8'
  TABLESPACE = pg_default
  CONNECTION LIMIT = -1
  IS_TEMPLATE = False;

部署 redis

  1. 获取默认配置文件
sh
# 注意 redis 版本需要和 docker-compose.yaml 中一致
# 如果不是 6.0.0 版本, 请根据版本下载对应的配置文件 
# 查看所有 redis 版本: https://github.com/redis/redis/tags
mkdir redis_conf
cd redis_conf
wget https://raw.githubusercontent.com/redis/redis/6.0.0/redis.conf
  1. 修改配置文件, 允许远程连接

主要修改3个选项:

  • bind 的值修改为 0.0.0.0 或者直接注释也可以
  • protected-mode 的值修改为 no 关闭保护模式, 允许远程链接
  • requirepass 的值修改为 your_password 远程连接需要的密码,可以是其他值,默认是注释的,需要手动取消注释
ini
bind 0.0.0.0
protected-mode no
requirepass your_password
  1. docker-compose.yaml 和配置文件内容
yaml
services:
  redis_server:
    image: redis:6
    restart: always
    container_name: redis_server
    ports:
      - 6379:6379
    volumes:
      - ./redis_conf:/etc/reids
ini
## 注: 由于是笔记, 所以删除了所有的注释(因为注释太多了), 只留下默认的配置
## 需要更多 redis 信息:
## https://redis.io
## https://github.com/redis/redis 

bind 0.0.0.0
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
requirepass redis_password
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

部署 mongodb

  1. 创建配置文件和初始化脚本
sh
mkdir ./mongo_init ./mongo_conf
touch ./mongo_init/init.js

# 注意这是 4.x 版本的 配置文件, 其他版本请根据版本下载对应的配置文件
# 查看所有 mongodb 版本: https://github.com/mongodb/mongo/tags
cd mongo_conf
wget https://raw.githubusercontent.com/mongodb/mongo/r4.4.0-rc14/debian/mongod.conf
  1. 修改默认配置允许远程连接

修改 net 选项 bindIp 的值为 0.0.0.0, 其他全部使用默认值即可

yaml
net:
  port: 27017
  bindIp: 0.0.0.0
  1. docker-compose.yaml 和配置文件内容
yaml
services:
  mongo_server:
    image: mongo:4
    restart: always
    container_name: mongo_server
    environment:
      MONGO_INITDB_ROOT_USERNAME: root          # root 账户用户名
      MONGO_INITDB_ROOT_PASSWORD: root_password # root 密码
      MONGO_INITDB_DATABASE: demos              # 默认数据库名
    ports:
      - '27017:27017'
    volumes:
      - ./mongo_conf/mongod.conf:/etc/mongod.conf # mongo 配置
      - ./mongo_data:/data/db                     # mongo 数据
      - ./mongo_init:/docker-entrypoint-initdb.d/ # 初始化脚本(只会在启动时执行)
yaml
# http://docs.mongodb.org/manual/reference/configuration-options/

storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

net:
  port: 27017
  bindIp: 0.0.0.0

processManagement:
  timeZoneInfo: /usr/share/zoneinfo

客户端推荐

命令行各种客户端推荐

Mac 开源免费 MySQL 客户端 Sequel Ace

Redis 开源免费客户端

MongoDB 官方客户端

Postgres 开源客户端 PgAdmin 4

Released under the MIT License.