记录常用的运维脚本,基于docker部署常用数据库
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Date:2023-09-13 14:02
# Title:
address = '/home/data' #数据库部署的根目录
MYSQL_PWD = 'xxx' #mysql密码
MYSQL_PORT = '3306' #mysql容器开放端口
MYSQL_NAME = 'xxx' #默认创建的数据库容器名称
REDIS_PWD = 'xxx' #redis密码
REDIS_PORT = '5106' #redis容器开放的端口
REDIS_NAME = 'xxx' #redis容器名称
INFLUX_PWD = 'xxxx' #influxdb密码
INFLUX_PORT = '5107' #influxdb容器开放端口
INFLUX_NAME = 'xxxxx' #influxdb容器名称
mysql_config = '''
[mysqld]
user=mysql
character-set-server=utf8
default_authentication_plugin=mysql_native_password
secure_file_priv=/var/lib/mysql
expire_logs_days=7
sql_mode ='STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION'
max_connections=1000
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
'''
influxdb_config = '''
[meta]
dir = "/var/lib/influxdb/meta"
[data]
dir = "/var/lib/influxdb/data"
engine = "tsm1"
wal-dir = "/var/lib/influxdb/wal"
[http]
auth-enabled = false
'''
influxdb_config1 = '''
[meta]
dir = "/var/lib/influxdb/meta"
[data]
dir = "/var/lib/influxdb/data"
engine = "tsm1"
wal-dir = "/var/lib/influxdb/wal"
[http]
auth-enabled = true
'''
redis_config = f'''
protected-mode no
port 6378
tcp-backlog 511
timeout 0
tcp-keepalive 300
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
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
requirepass {REDIS_PWD}
# maxclients 10000
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush 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-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
'''
import os
os.chdir(address)
# mysql构建
mysql_command = ["mkdir -p mysql/{etc,data,logs}","mkdir -p mysql/etc/conf.d"]
for command in mysql_command:
os.system(command)
with open('mysql/etc/my.cnf','w') as f:
f.write(mysql_config)
os.system(f"""
docker run -itd -p {MYSQL_PORT}:3306 --name {MYSQL_NAME} \
--privileged=true \
-e TZ="Asia/Shanghai" \
-v {address}/mysql/etc/my.cnf:/etc/my.cnf \
-v {address}/mysql/logs:/var/log \
-e MYSQL_ROOT_PASSWORD={MYSQL_PWD} mysql
""")
# redis构建
os.system("mkdir -p redis/conf")
with open('redis/conf/redis.conf','w') as f:
f.write(redis_config)
os.system(f"""
docker run -p {REDIS_PORT}:6378 --name {REDIS_NAME} \
--privileged=true \
-e TZ="Asia/Shanghai" \
-v {address}/redis/conf/redis.conf:/etc/redis/redis.conf \
-v {address}/redis/data:/data \
-itd redis redis-server /etc/redis/redis.conf --appendonly yes
""")
# influxdb构建
os.system('mkdir -p influx/conf/')
with open('influx/conf/influxdb.conf','w') as f:
f.write(influxdb_config)
os.system(f"""
docker run -itd --name {INFLUX_NAME} \
-p {INFLUX_PORT}:8086 \
--privileged=true \
-e TZ="Asia/Shanghai" \
-e INFLUXDB_GRAPHITE_ENABLED=true \
-v {address}/influx/conf/influxdb.conf:/etc/influxdb/influxdb.conf \
influxdb:1.8.10
""")
with open('create_auth.sh', 'w') as f:
f.write(f'''#! /bin/bash
influx -execute "CREATE USER \\"root\\" WITH PASSWORD '{INFLUX_PWD}' WITH ALL PRIVILEGES;"
''')
os.system(f'docker cp create_auth.sh {INFLUX_NAME}:create_auth.sh')
os.system(f'docker exec {INFLUX_NAME} bash create_auth.sh')
with open('influx/conf/influxdb.conf','w') as f:
f.write(influxdb_config1)
os.system(f'docker restart {INFLUX_NAME}')
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
- 212.
- 213.
- 214.
- 215.
- 216.
- 217.
- 218.
- 219.
- 220.
- 221.
- 222.
- 223.
- 224.
- 225.
- 226.
- 227.