有数TIDB部署指南
更新时间: 2024-12-19 16:29:03
阅读 34
背景
客户想要将数据库更换为tidb,不使用mysql
1. 准备
镜像:localhost:5000/yddocker/tidb:v8.1.1
下载地址:59.111.178.240:20280/stable/tidb.tar.gz
mkdir -p /youdata/tidb/conf/
mkdir -p /youdata/tidb/data/
2. YAML
环境变量
所有的DB变成如下(注意flow的库名为youdata_flow)
DB: mysql://youdata:youdata@tidb:4000/youdata
服务
mysql服务可以保留,不过要分清楚,使用的是tidb,不是mysql.
保留MYSQL的好处是mysql备份脚本需要使用mysqldump。而这个在tidb中不存在。
tidb:
image: localhost:5000/yddocker/tidb:v8.1.1
deploy:
replicas: 1
networks:
- youdata
volumes:
- /youdata/tidb/conf/tidb.toml:/tidb.toml
- /youdata/tidb/data:/tmp/tidb
#ports:
# - "4000:4000"
logging:
options:
max-size: "10m"
max-file: "10"
配置文件
/youdata/tidb/conf/tidb.toml
# TiDB Configuration.
# TiDB server host.
host = "0.0.0.0"
# tidb server advertise IP.
advertise-address = ""
# TiDB server port.
port = 4000
# Registered store name, [tikv, mocktikv, unistore]
store = "unistore"
# TiDB storage path.
path = "/tmp/tidb"
# The socket file to use for connection.
socket = "/tmp/tidb-{Port}.sock"
# Schema lease duration, very dangerous to change only if you know what you do.
lease = "45s"
# When create table, split a separated region for it. It is recommended to
# turn off this option if there will be a large number of tables created.
split-table = true
# The limit of concurrent executed sessions.
token-limit = 1000
# The temporary directory to store the intermediate compute results.
temp-dir = "/tmp/tidb"
# Specifies the temporary storage path for some operators when a single SQL statement exceeds the memory quota specified by the memory quota.
# It defaults to a generated directory in `<TMPDIR>/<os/user.Current().Uid>_tidb/` if it is unset.
# It only takes effect when `tidb_enable_tmp_storage_on_oom` is `true`.
# tmp-storage-path = "/tmp/<os/user.Current().Uid>_tidb/MC4wLjAuMDo0MDAwLzAuMC4wLjA6MTAwODA=/tmp-storage"
# Specifies the maximum use of temporary storage (bytes) for all active queries when `tidb_enable_tmp_storage_on_oom` is enabled.
# If the `tmp-storage-quota` exceeds the capacity of the temporary storage directory, tidb-server would return an error and exit.
# The default value of tmp-storage-quota is under 0 which means tidb-server wouldn't check the capacity.
tmp-storage-quota = -1
# Make "kill query" behavior compatible with MySQL. It's not recommend to
# turn on this option when TiDB server is behind a proxy.
compatible-kill-query = false
# Make SIGTERM wait N seconds before starting the shutdown procedure. This is designed for when TiDB is behind a proxy/load balancer.
# The health check will fail immediately but the server will not start shutting down until the time has elapsed.
graceful-wait-before-shutdown = 0
# treat-old-version-utf8-as-utf8mb4 use for upgrade compatibility. Set to true will treat old version table/column UTF8 charset as UTF8MB4.
treat-old-version-utf8-as-utf8mb4 = true
# max-index-length is used to deal with compatibility issues from v3.0.7 and previous version upgrades. It can only be in [3072, 3072*4].
max-index-length = 3072
# index-limit is used to deal with compatibility issues. It can only be in [64, 64*8].
index-limit = 64
# enable-table-lock is used to control table lock feature. Default is false, indicate the table lock feature is disabled.
enable-table-lock = false
# delay-clean-table-lock is used to control the time (Milliseconds) of delay before unlock the table in the abnormal situation.
delay-clean-table-lock = 0
# Maximum number of the splitting region, which is used by the split region statement.
split-region-max-num = 1000
# alter-primary-key is used to control whether the primary keys are clustered.
# Note that this config is deprecated. Only valid when @@global.tidb_enable_clustered_index = 'int_only'.
# Default is false, only the integer primary keys are clustered.
# If it is true, all types of primary keys are nonclustered.
alter-primary-key = false
# server-version is used to change the version string of TiDB in the following scenarios:
# 1. the server version returned by builtin-function `VERSION()`.
# 2. the server version filled in handshake packets of MySQL Connection Protocol, see https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_connection_phase_packets_protocol_handshake_v10.html for more details.
# if server-version = "", the default value(original TiDB version string) is used.
server-version = ""
# repair mode is used to repair the broken table meta in TiKV in extreme cases.
repair-mode = false
# Repair table list is used to list the tables in repair mode with the format like ["db.table",].
# In repair mode, repairing table which is not in repair list will get wrong database or wrong table error.
repair-table-list = []
# Whether new collations are enabled, as indicated by its name, this configuration entry take effect ONLY when a TiDB cluster bootstraps for the first time.
new_collations_enabled_on_first_bootstrap = true
# Don't register information of this TiDB to etcd, so this instance of TiDB won't appear in the services like dashboard.
# This option is useful when you want to embed TiDB into your service(i.e. use TiDB as a library).
# *If you want to start a TiDB service, NEVER enable this.*
skip-register-to-dashboard = false
# When enabled, usage data (for example, instance versions) will be reported to PingCAP periodically for user experience analytics.
# If this config is set to `false` on all TiDB servers, telemetry will be always disabled regardless of the value of the global variable `tidb_enable_telemetry`.
# See PingCAP privacy policy for details: https://pingcap.com/en/privacy-policy/
enable-telemetry = false
# deprecate-integer-display-length is used to be compatible with MySQL 8.0 in which the integer declared with display length will be returned with
# a warning like `Integer display width is deprecated and will be removed in a future release`.
deprecate-integer-display-length = false
# enable-enum-length-limit is used to deal with compatibility issues. When true, the enum/set element length is limited.
# According to MySQL 8.0 Refman:
# The maximum supported length of an individual SET element is M <= 255 and (M x w) <= 1020,
# where M is the element literal length and w is the number of bytes required for the maximum-length character in the character set.
# See https://dev.mysql.com/doc/refman/8.0/en/string-type-syntax.html for more details.
enable-enum-length-limit = true
# enable-global-kill is used to control global kill feature. When it is enabled, connection ID is globally unique and KILL
# command can be forwarded to the right TiDB instance to execute.
enable-global-kill = true
# disaggregated-tiflash indicates whether TiDB is in disaggregated tiflash mode, if true, MPP will runs on tiflash_compute nodes.
disaggregated-tiflash = false
# autoscaler-type indicates which type of AutoScaler will be used. Possible values are: mock, aws, gcp.
# Only meaningful when disaggregated-tiflash is true.
autoscaler-type = "aws"
# autoscaler-addr is the host of AutoScaler, Only meaningful when disaggregated-tiflash is true.
# Only meaningful when disaggregated-tiflash is true.
autoscaler-addr = "tiflash-autoscale-lb.tiflash-autoscale.svc.cluster.local:8081"
# autoscaler-cluster-id is the unique id for each TiDB cluster, which will used by AutoScaler.
# Only meaningful when disaggregated-tiflash is true.
autoscaler-cluster-id = ""
# use-autoscaler indicates whether use AutoScaler or PD for tiflash_compute nodes, only meaningful when disaggregated-tiflash is true.
use-autoscaler = false
# in-mem-slow-query-topn-num indicates the number of TopN slow queries stored in memory.
in-mem-slow-query-topn-num = 30
# in-mem-slow-query-recent-num indicates the number of recent slow queries stored in memory.
in-mem-slow-query-recent-num = 500
[log]
# Log level: debug, info, warn, error, fatal.
level = "info"
# Log format, one of json or text.
format = "text"
# Enable automatic timestamps in log output, if not set, it will be defaulted to true.
# enable-timestamp = true
# Enable annotating logs with the full stack error message, if not set, it will be defaulted to false.
# enable-error-stack = false
# Stores slow query log into separated files.
slow-query-file = "tidb-slow.log"
# Stores general log into separated files.
# If it equals to empty, the general log will be written to the server log.
#general-log-file = ""
# Make tidb panic if the write log operation hang for 15s
# timeout = 15
# File logging.
[log.file]
# Log file name.
filename = ""
# Max log file size in MB (upper limit to 4096MB).
max-size = 3000
# Max log file keep days. No clean up by default.
max-days = 0
# Maximum number of old log files to retain. No clean up by default.
max-backups = 0
# Compression function for rotated files.
# It can be empty or "gzip", empty means compression disabled.
#compression = ""
[security]
# Path of file that contains list of trusted SSL CAs for connection with mysql client.
ssl-ca = ""
# Path of file that contains X509 certificate in PEM format for connection with mysql client.
ssl-cert = ""
# Path of file that contains X509 key in PEM format for connection with mysql client.
ssl-key = ""
# Path of file that contains list of trusted SSL CAs for connection with cluster components.
cluster-ssl-ca = ""
# Path of file that contains X509 certificate in PEM format for connection with cluster components.
cluster-ssl-cert = ""
# Path of file that contains X509 key in PEM format for connection with cluster components.
cluster-ssl-key = ""
# Configurations of the encryption method to use for encrypting the spilled data files.
# Possible values are "plaintext", "aes128-ctr", if not set, it will be "plaintext" by default.
# "plaintext" means encryption is disabled.
spilled-file-encryption-method = "plaintext"
# Security Enhanced Mode (SEM) restricts the "SUPER" privilege and requires fine-grained privileges instead.
enable-sem = false
# Automatic creation of TLS certificates.
# Setting it to 'true' is recommended because it is safer and tie with the default configuration of MySQL.
# If this config is commented/missed, the value would be 'false' for the compatibility with TiDB versions that does not support it.
auto-tls = true
# Minium TLS version to use, e.g. "TLSv1.2"
tls-version = ""
# The RSA Key size for automatic generated RSA keys
rsa-key-size = 4096
[status]
# If enable status report HTTP service.
report-status = true
# TiDB status host.
status-host = "0.0.0.0"
## status-host is the HTTP address for reporting the internal status of a TiDB server, for example:
## API for prometheus: http://${status-host}:${status_port}/metrics
## API for pprof: http://${status-host}:${status_port}/debug/pprof
# TiDB status port.
status-port = 10080
# Prometheus pushgateway address, leaves it empty will disable push to pushgateway.
metrics-addr = ""
# Prometheus client push interval in second, set \"0\" to disable push to pushgateway.
#metrics-interval = 15
# Record statements qps by database name if it is enabled.
record-db-qps = false
# Record database name label if it is enabled.
record-db-label = false
[performance]
# Max CPUs to use, 0 use number of CPUs in the machine.
max-procs = 0
# Memory size quota for tidb server, 0 means unlimited
server-memory-quota = 0
# StmtCountLimit limits the max count of statement inside a transaction.
stmt-count-limit = 5000
# Set keep alive option for tcp connection.
tcp-keep-alive = true
# Set nodelay option for tcp connection.
tcp-no-delay = true
# Whether support cartesian product.
cross-join = true
# Stats lease duration, which influences the time of analyze and stats load.
stats-lease = "3s"
# Pseudo stats will be used if the ratio between the modify count and
# row count in statistics of a table is greater than it.
pseudo-estimate-ratio = 0.8
# Bind info lease duration, which influences the duration of loading bind info and handling invalid bind.
bind-info-lease = "3s"
# Whether support pushing down aggregation with distinct to cop task
distinct-agg-push-down = false
# The limitation of the size in byte for the entries in one transaction.
# If using TiKV as the storage, the entry represents a key/value pair.
# NOTE: If binlog is enabled with Kafka (e.g. arbiter cluster),
# this value should be less than 1073741824(1G) because this is the maximum size that can be handled by Kafka.
# If binlog is disabled or binlog is enabled without Kafka, this value should be less than 1099511627776(1T).
txn-total-size-limit = 104857600
# The limitation of the size in byte for each entry in one transaction.
# NOTE: Increasing this limit may cause performance problems.
txn-entry-size-limit = 6291456
# max lifetime of transaction ttl manager.
max-txn-ttl = 3600000
# The Go GC trigger factor, you can get more information about it at https://golang.org/pkg/runtime.
# If you encounter OOM when executing large query, you can decrease this value to trigger GC earlier.
# If you find the CPU used by GC is too high or GC is too frequent and impact your business you can increase this value.
gogc = 100
# Whether to use the lite mode of init stats.
lite-init-stats = true
# Whether to wait for init stats to finish before providing service during startup
force-init-stats = true
[proxy-protocol]
# PROXY protocol acceptable client networks.
# Empty string means disable PROXY protocol, * means all networks.
networks = ""
# PROXY protocol header read timeout, unit is second
header-timeout = 5
[opentracing]
# Enable opentracing.
enable = false
# Whether to enable the rpc metrics.
rpc-metrics = false
[opentracing.sampler]
# Type specifies the type of the sampler: const, probabilistic, rateLimiting, or remote
type = "const"
# Param is a value passed to the sampler.
# Valid values for Param field are:
# - for "const" sampler, 0 or 1 for always false/true respectively
# - for "probabilistic" sampler, a probability between 0 and 1
# - for "rateLimiting" sampler, the number of spans per second
# - for "remote" sampler, param is the same as for "probabilistic"
# and indicates the initial sampling rate before the actual one
# is received from the mothership
param = 1.0
# SamplingServerURL is the address of jaeger-agent's HTTP sampling server
sampling-server-url = ""
# MaxOperations is the maximum number of operations that the sampler
# will keep track of. If an operation is not tracked, a default probabilistic
# sampler will be used rather than the per operation specific sampler.
max-operations = 0
# SamplingRefreshInterval controls how often the remotely controlled sampler will poll
# jaeger-agent for the appropriate sampling strategy.
sampling-refresh-interval = 0
[opentracing.reporter]
# QueueSize controls how many spans the reporter can keep in memory before it starts dropping
# new spans. The queue is continuously drained by a background go-routine, as fast as spans
# can be sent out of process.
queue-size = 0
# BufferFlushInterval controls how often the buffer is force-flushed, even if it's not full.
# It is generally not useful, as it only matters for very low traffic services.
buffer-flush-interval = 0
# LogSpans, when true, enables LoggingReporter that runs in parallel with the main reporter
# and logs all submitted spans. Main Configuration.Logger must be initialized in the code
# for this option to have any effect.
log-spans = false
# LocalAgentHostPort instructs reporter to send spans to jaeger-agent at this address
local-agent-host-port = ""
[pd-client]
# Max time which PD client will wait for the PD server in seconds.
pd-server-timeout = 3
[tikv-client]
# Max gRPC connections that will be established with each tikv-server.
grpc-connection-count = 4
# After a duration of this time in seconds if the client doesn't see any activity it pings
# the server to see if the transport is still alive.
grpc-keepalive-time = 10
# After having pinged for keepalive check, the client waits for a duration of Timeout in seconds
# and if no activity is seen even after that the connection is closed.
grpc-keepalive-timeout = 3
# The compression type for gRPC channel: none or gzip.
grpc-compression-type = "none"
# Max time for commit command, must be twice bigger than raft election timeout.
commit-timeout = "41s"
# Max batch size in gRPC.
max-batch-size = 128
# Overload threshold of TiKV.
overload-threshold = 200
# Max batch wait time in nanosecond to avoid waiting too long. 0 means disable this feature.
max-batch-wait-time = 0
# Batch wait size, to avoid waiting too long.
batch-wait-size = 8
# If a Region has not been accessed for more than the given duration (in seconds), it
# will be reloaded from the PD.
region-cache-ttl = 600
# store-limit is used to restrain TiDB from sending request to some stores which is up to the limit.
# If a store has been up to the limit, it will return error for the successive request in same store.
# default 0 means shutting off store limit.
store-limit = 0
# store-liveness-timeout is used to control timeout for store liveness after sending request failed.
store-liveness-timeout = "1s"
# ttl-refreshed-txn-size decides whether a transaction should update its lock TTL.
# If the size(in byte) of a transaction is large than `ttl-refreshed-txn-size`, it update the lock TTL during the 2PC.
ttl-refreshed-txn-size = 33554432
# If the number of keys that one prewrite request of one region involves exceed this threshold, it will use `ResolveLock` instead of `ResolveLockLite`.
resolve-lock-lite-threshold = 16
[tikv-client.copr-cache]
# The capacity in MB of the cache. Zero means disable coprocessor cache.
capacity-mb = 1000.0
[binlog]
# enable to write binlog.
# NOTE: If binlog is enabled with Kafka (e.g. arbiter cluster),
# txn-total-size-limit should be less than 1073741824(1G) because this is the maximum size that can be handled by Kafka.
enable = false
# WriteTimeout specifies how long it will wait for writing binlog to pump.
write-timeout = "15s"
# If IgnoreError is true, when writing binlog meets error, TiDB would stop writing binlog,
# but still provide service.
ignore-error = false
# use socket file to write binlog, for compatible with kafka version tidb-binlog.
binlog-socket = ""
# the strategy for sending binlog to pump, value can be "range" or "hash" now.
strategy = "range"
[pessimistic-txn]
# max retry count for a statement in a pessimistic transaction.
max-retry-count = 256
# The max count of deadlock events that will be recorded in the information_schema.deadlocks table.
deadlock-history-capacity = 10
# Whether retryable deadlocks (in-statement deadlocks) are collected to the information_schema.deadlocks table.
deadlock-history-collect-retryable = false
# If true it means the auto-commit transactions will be in pessimistic mode.
pessimistic-auto-commit = false
# experimental section controls the features that are still experimental: their semantics,
# interfaces are subject to change, using these features in the production environment is not recommended.
[experimental]
# enable creating expression index.
allow-expression-index = false
# server level isolation read by engines and labels
[isolation-read]
# engines means allow the tidb server read data from which types of engines. options: "tikv", "tiflash", "tidb".
engines = ["tikv", "tiflash", "tidb"]
# instance scope variables
# These options are also available as a system variable for online configuration
# changes to the system variable do not persist to the cluster. You must make changes
# in this configuration file on each tidb-server.
[instance]
# tidb_general_log is used to log every query in the server in info level.
tidb_general_log = false
# tidb_pprof_sql_cpu is used to add label sql label to pprof result.
tidb_pprof_sql_cpu = false
# ddl_slow_threshold sets log DDL operations whose execution time exceeds the threshold value.
ddl_slow_threshold = 300
# tidb_expensive_query_time_threshold indicates the time threshold of expensive query.
tidb_expensive_query_time_threshold = 60
# Force the priority of all statements in a specified priority.
# The value could be "NO_PRIORITY", "LOW_PRIORITY", "HIGH_PRIORITY" or "DELAYED".
tidb_force_priority = "NO_PRIORITY"
# check mb4 value in utf8 is used to control whether to check the mb4 characters when the charset is utf8.
tidb_check_mb4_value_in_utf8 = true
# Whether to enable slow query log.
tidb_enable_slow_log = true
# Queries with execution time greater than this value will be logged. (Milliseconds)
tidb_slow_log_threshold = 300
# tidb_record_plan_in_slow_log is used to enable record query plan in slow log.
# 0 is disable. 1 is enable.
tidb_record_plan_in_slow_log = 1
# The maximum permitted number of simultaneous client connections. When the value is 0, the number of connections is unlimited.
max_connections = 0
# Run ddl worker on this tidb-server.
tidb_enable_ddl = true
3. 启动
dsd之后
进入mysql容器使用mysql client
youdata还是ydswarm_youdata):
dx mysql
执行:
mysql -h tidb -P4000 -uroot
执行以下SQL:
CREATE USER IF NOT EXISTS 'youdata'@'%' IDENTIFIED BY 'youdata';
GRANT ALL PRIVILEGES ON \*.\* TO 'youdata'@'%' WITH GRANT OPTION;
CREATE USER IF NOT EXISTS 'youdata'@'localhost' IDENTIFIED BY 'youdata';
GRANT ALL PRIVILEGES ON \*.\* TO 'youdata'@'localhost' WITH GRANT OPTION;
CREATE DATABASE IF NOT EXISTS youdata CHARACTER SET utf8mb4;
CREATE DATABASE IF NOT EXISTS youdata\_log CHARACTER SET utf8mb4;
CREATE DATABASE IF NOT EXISTS youdata\_flow CHARACTER SET utf8mb4;
ALTER USER 'root' IDENTIFIED BY 'youdata';
执行后之后登录则需要:mysql -h tidb -P4000 -uyoudata -pyoudata
此时web容器应该不会退出了。
这个之后进入web执行数据升级脚本。
4. 备份
更改备份脚本:/youdata/scripts/backup_single.sh
核心就是mysql/mysqldump语句加上--host=tidb --port=4000,另外还得去除--single-transaction。
最终脚本如下。
切记:一定要执行一遍备份脚本,并且验证backeup.log无报错,且备份SQL内容完整无丢失。
如果执行报错,因为复制粘贴出现\r这类的报错,执行如下命令:
sed -i 's/\r//' backup_single.sh
#!/bin/bash
# 单机 MySQL 备份脚本
# 使用方法:
# 1、输入命令 crontab -e 打开 cron 编辑器
# 2、在最后一行追加 0 2 * * * /bin/sh /backup_file_path/backup_single.sh
# 3、保存退出
BACKUP_PATH="/youdata/db_data_bak"
# youdata库下单独备份的表, 相当于在youdata库备份完之后再单独备份这些表
BACKUP_ADDITION_TABLES=("new_component") # 输入例如: BACKUP_ADDITION_TABLES=("new_component" "tf_task_flow")
LOG="${BACKUP_PATH}/backup.log"
DATE_POSTFIX=$(date +"%Y-%m-%d_%H-%M-%S")
BACKUP_FILE="${BACKUP_PATH}/backup-${DATE_POSTFIX}.sql"
BACKUP_COMPLEX_FILE="${BACKUP_PATH}/backup-complex-${DATE_POSTFIX}.sql"
BACKUP_AAC_FILE="${BACKUP_PATH}/backup-aac-${DATE_POSTFIX}.sql"
BACKUP_FLOW_FILE="${BACKUP_PATH}/backup-flow-${DATE_POSTFIX}.sql"
mkdir -p ${BACKUP_PATH}
echo "[${DATE_POSTFIX}] Start to backup mysql data." >>$LOG
# 从 yaml 中读取 mysql 密码
version=$(docker ps | grep "web:lts-" | head -n 1 | awk -F ' ' {'print $2'} | cut -f 2 -d "-")
version_no_point=${version/./}
yaml="/youdata/installer/docker-stack.youdata${version_no_point}.yaml"
if [ ! -f $yaml ]; then
echo "ERROR! $yaml does NOT exist!" >>$LOG
exit 1
fi
db=$(cat $yaml | grep DB | head -n 1)
db_password=$(echo $db | rev | cut -f 2- -d @ | rev | cut -f 4- -d :)
# echo "DB password: $db_password"
mysql_cid=$(docker ps | grep "mysql:stable" | cut -f 1 -d ' ')
if [ -z $mysql_cid ]; then
# 兼容 stable tag 之间的版本
mysql_cid=$(docker ps | grep "mysql" | grep -v "exporter" | cut -f 1 -d ' ')
fi
IFS=' ' read -d "" -ra cid_arr <<<$mysql_cid
if [ ${#cid_arr[@]} -gt 1 ]; then
echo "ERROR! Get multiple MySQL container ids." >>$LOG
exit 1
fi
if [ -z $mysql_cid ]; then
echo "ERROR! Can NOT get MySQL container id, please check MySQL is running." >>$LOG
exit 1
fi
backup_and_rename_tables() {
for TABLE in "${BACKUP_ADDITION_TABLES[@]}"; do
TABLE_FILE="${BACKUP_PATH}/${TABLE}-${DATE_POSTFIX}.sql"
echo "${DATE_POSTFIX} Table $TABLE backed up start" >>$LOG
# Step 1: 使用 mysqldump 备份表
docker exec $mysql_cid mysqldump --host=tidb --port=4000 --user="youdata" --password=$db_password --default-character-set=utf8mb4 --set-gtid-purged=OFF youdata ${TABLE} >$TABLE_FILE 2>>$LOG
# Step 2: 使用 sed 重命名表
sed -i "s/\`$TABLE\`/\`${TABLE}_bak\`/g" "$TABLE_FILE"
echo "${DATE_POSTFIX} Table $TABLE backed up and renamed to ${TABLE}_bak in $TABLE_FILE" >>$LOG
done
}
# 备份 youdata 库
SECONDS=0
docker exec $mysql_cid mysqldump --host=tidb --port=4000 --user="youdata" --password=$db_password --default-character-set=utf8mb4 --set-gtid-purged=OFF youdata >$BACKUP_FILE 2>>$LOG
ret=$?
duration=$SECONDS
if [ $ret == "0" ]; then
echo "Backup db youdata success. It takes $duration seconds." >>$LOG
sql_size=$(du -m $BACKUP_FILE | cut -f1)
echo "Backup db youdata SQL: ${BACKUP_FILE}: $sql_size MB." >>$LOG
backup_and_rename_tables
else
echo "Backup db youdata failed. It takes $duration seconds." >>$LOG
sql_size=$(du -m $BACKUP_FILE | cut -f1)
echo "Backup db youdata SQL: ${BACKUP_FILE}: $sql_size MB." >>$LOG
fi
# 备份 mis2datasource 库
temp_query=$(docker exec $mysql_cid mysql --host=tidb --port=4000 -uyoudata -p$db_password -e "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'mis2datasource';" 2>>$LOG)
if [ ! -z "$temp_query" ]; then
SECONDS=0
docker exec $mysql_cid mysqldump --host=tidb --port=4000 --user="youdata" --password=$db_password --default-character-set=utf8mb4 --set-gtid-purged=OFF mis2datasource >$BACKUP_COMPLEX_FILE 2>>$LOG
ret=$?
duration=$SECONDS
if [ $ret == "0" ]; then
echo "Backup db mis2datasource success. It takes $duration seconds." >>$LOG
sql_size=$(du -m $BACKUP_COMPLEX_FILE | cut -f1)
echo "Backup db mis2datasource SQL: ${BACKUP_COMPLEX_FILE}: $sql_size MB." >>$LOG
else
echo "Backup db mis2datasource failed. It takes $duration seconds." >>$LOG
sql_size=$(du -m $BACKUP_COMPLEX_FILE | cut -f1)
echo "Backup db mis2datasource SQL: ${BACKUP_COMPLEX_FILE}: $sql_size MB." >>$LOG
fi
else
echo "db mis2datasource does not exist, skip. " >>$LOG
fi
# 备份 youdata_flow 库
temp_query=$(docker exec $mysql_cid mysql --host=tidb --port=4000 -uyoudata -p$db_password -e "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'youdata_flow';" 2>>$LOG)
if [ ! -z "$temp_query" ]; then
SECONDS=0
docker exec $mysql_cid mysqldump --host=tidb --port=4000 --user="youdata" --password=$db_password --default-character-set=utf8mb4 --set-gtid-purged=OFF youdata_flow >$BACKUP_FLOW_FILE 2>>$LOG
ret=$?
duration=$SECONDS
if [ $ret == "0" ]; then
echo "Backup db youdata_flow success. It takes $duration seconds." >>$LOG
sql_size=$(du -m $BACKUP_FLOW_FILE | cut -f1)
echo "Backup db youdata_flow SQL: ${BACKUP_FLOW_FILE}: $sql_size MB." >>$LOG
else
echo "Backup db youdata_flow failed. It takes $duration seconds." >>$LOG
sql_size=$(du -m $BACKUP_FLOW_FILE | cut -f1)
echo "Backup db youdata_flow SQL: ${BACKUP_FLOW_FILE}: $sql_size MB." >>$LOG
fi
else
echo "db youdata_flow does not exist, skip. " >>$LOG
fi
# 备份 AAC auth_system 库
temp_query=$(docker exec $mysql_cid mysql --host=tidb --port=4000 -uyoudata -p$db_password -e "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'auth_system';" 2>>$LOG)
if [ ! -z "$temp_query" ]; then
SECONDS=0
docker exec $mysql_cid mysqldump --host=tidb --port=4000 --user="youdata" --password=$db_password --default-character-set=utf8mb4 --set-gtid-purged=OFF auth_system >$BACKUP_AAC_FILE 2>>$LOG
ret=$?
duration=$SECONDS
if [ $ret == "0" ]; then
echo "Backup db auth_system success. It takes $duration seconds." >>$LOG
sql_size=$(du -m $BACKUP_AAC_FILE | cut -f1)
echo "Backup db auth_system SQL: ${BACKUP_AAC_FILE}: $sql_size MB." >>$LOG
else
echo "Backup db auth_system failed. It takes $duration seconds." >>$LOG
sql_size=$(du -m $BACKUP_AAC_FILE | cut -f1)
echo "Backup db auth_system SQL: ${BACKUP_AAC_FILE}: $sql_size MB." >>$LOG
fi
else
echo "db auth_system does not exist, skip. " >>$LOG
fi
find ${BACKUP_PATH} -mtime +5 -type f -delete
exit $ret
文档反馈
以上内容对您是否有帮助?