一 服務器初始化
#!/bin/bash
if [[ "$(whoami)" != "root" ]]; then
echo "please run this script as root ." >&2
exit 1
fi
#yum update
yum_update(){
yum update -y
}
#configure yum source
yum_config(){
yum install wget epel-release -y
cd /etc/yum.repos.d/ && mkdir bak && mv -f *.repo bak/
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all && yum makecache
yum -y install iotop iftop net-tools lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip sudo ntp libaio-devel wget vim ncurses-devel autoconf automake zlib-devel python-devel bash-completion htop
}
#firewalld
iptables_config(){
systemctl stop firewalld.service
systemctl disable firewalld.service
yum install iptables-services -y
systemctl enable iptables
systemctl start iptables
iptables -F
service iptables save
}
# ntp
ntp_config(){
#檢測及安裝 NTP 服務
sudo systemctl status ntpd.service
ntpstat
sudo yum install ntp ntpdate && \
sudo systemctl start ntpd.service && \
sudo systemctl enable ntpd.service
sudo systemctl stop ntpd.service && \
sudo ntpdate pool.ntp.org && \
sudo systemctl start ntpd.service
}
#system config
system_config(){
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
timedatectl set-local-rtc 1 && timedatectl set-timezone Asia/Shanghai
yum -y install chrony && systemctl start chronyd.service && systemctl enable chronyd.service
}
ulimit_config(){
echo "ulimit -SHn 102400" >> /etc/rc.local
cat >> /etc/security/limits.conf << EOF
* soft nofile 102400
* hard nofile 102400
* soft nproc 102400
* hard nproc 102400
* soft memlock unlimited
* hard memlock unlimited
EOF
}
#set sysctl
sysctl_config(){
cp /etc/sysctl.conf /etc/sysctl.conf.bak
cat > /etc/sysctl.conf << EOF
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
EOF
/sbin/sysctl -p
echo "sysctl set OK!!"
}
#install docker
install_docker() {
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum-config-manager --enable docker-ce-edge
yum-config-manager --enable docker-ce-test
yum-config-manager --disable docker-ce-edge
yum install docker-ce -y
systemctl start docker
systemctl enable docker
echo "docker install succeed!!"
}
#install_docker_compace
install_docker_compace() {
#curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
mv ./docker-compose /usr/local/bin/
chmod +x /usr/local/bin/docker-compose
docker-compose --version
echo "docker-compose install succeed!!"
}
main(){
yum_update
yum_config
iptables_config
system_config
ulimit_config
sysctl_config
#install_docker
#install_docker_compace
}
main
1.1 關閉swap分區
echo "vm.swappiness = 0">> /etc/sysctl.conf
swapoff -a && swapon -a
sysctl -p
1.2 檢測和關閉透明大頁
-
執行以下命令查看透明大頁的開啟狀態。如果返回 [always] madvise never 則表示處於啟用狀態:
cat /sys/kernel/mm/transparent_hugepage/enabled
-
執行 grubby 命令查看默認內核版本:
grubby --default-kernel
-
執行 grubby --update-kernel 命令修改內核配置,--update-kernel 后換成你查到的內核版本
grubby --args="transparent_hugepage=never" --update-kernel /boot/vmlinuz-3.10.0-1160.el7.x86_64
-
執行 grubby --info 命令查看修改后的默認內核配置
grubby --info /boot/vmlinuz-3.10.0-1160.el7.x86_64
-
執行 reboot 命令進行重啟或者修改當前的內核配置:
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
-
查看重啟或者修改后已生效的默認內核配置。如果輸出 always madvise [never] 表示透明大頁處於禁用狀態。
cat /sys/kernel/mm/transparent_hugepage/enabled
二 在 TiKV 部署目標機器上添加數據盤 EXT4 文件系統掛載參數(卷名等參數自行修改)
1.1 查看數據盤
fdisk -l
1.2 創建分區
parted -s -a optimal /dev/nvme1n1 mklabel gpt -- mkpart primary ext4 1 -1
1.3 格式化文件系統
mkfs.ext4 /dev/nvme1n1p1
1.4 查看數據盤分區 UUID
lsblk -f
1.5 編輯 /etc/fstab 文件,添加 nodelalloc 掛載參數
UUID=c51eb23b-195c-4061-92a9-3fad812cc12f /data1 ext4 defaults,nodelalloc,noatime 0 2
1.6 掛載數據盤
mkdir /data1 &&
mount -a
1.7 執行以下命令,如果文件系統為 ext4,並且掛載參數中包含 nodelalloc,則表示已生效
mount -t ext4
三 配置中控機與其他服務器互信
3.1 中控機創建普通用戶並生成密鑰
useradd tidb
su - tidb
ssh-keygen
3.2 修改sshd服務的連接數限制
sudo sed -i 's/#MaxSessions 10/MaxSessions 20/' /etc/ssh/sshd_config
sudo systemctl restart sshd
3.3 所有服務器上創建同樣的用戶,配置中控機與其他所有服務器互信
useradd tidb
echo 'tidb ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
su - tidb
mkdir -pv /home/tidb/.ssh
echo "中控機的公鑰" > /home/tidb/.ssh/authorized_keys
chmod 700 /home/tidb/.ssh
chmod 600 /home/tidb/.ssh/authorized_keys
四 中控機器下載安裝tiup
curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
source ~/.bash_profile
4.1 安裝tiup的cluster組件
tiup cluster
4.2 確認是否安裝
which tiup
4.3 更新TiUP cluster 組件至最新版本
tiup update --self && tiup update cluster
4.4 驗證當前 TiUP cluster 版本信息
tiup --binary cluster
4.5 配置集群配置文件 topo.yaml
https://github.com/pingcap/docs-cn/tree/release-4.0/config-templates
4.6 創建配置文件
cat <<'EOF' > ~/.tiup/topo.yaml
# # Global variables are applied to all deployments and used as the default value of
# # the deployments if a specific deployment value is missing.
global:
user: "tidb"
ssh_port: 11618
monitored:
node_exporter_port: 9100
blackbox_exporter_port: 9115
deploy_dir: "/tidb-deploy/monitored-9100"
data_dir: "/tidb-data/monitored-9100"
log_dir: "/tidb-deploy/monitored-9100/log"
server_configs:
tidb:
log.slow-threshold: 500
binlog.enable: false
binlog.ignore-error: false
new_collations_enabled_on_first_bootstrap: true
alter-primary-key: true
tikv:
# server.grpc-concurrency: 4
# raftstore.apply-pool-size: 2
# raftstore.store-pool-size: 2
# rocksdb.max-sub-compactions: 1
# storage.block-cache.capacity: "16GB"
# readpool.unified.max-thread-count: 12
readpool.coprocessor.use-unified-pool: true
coprocessor.split-region-on-table: true
readpool.storage.use-unified-pool: true
readpool.unified.max-thread-count: 10
server.grpc-concurrency: 6
storage.block-cache.capacity: 14G
pd:
schedule.leader-schedule-limit: 4
schedule.region-schedule-limit: 2048
schedule.replica-schedule-limit: 64
schedule.enable-cross-table-merge: true
tiflash:
# Maximum memory usage for processing a single query. Zero means unlimited.
profiles.default.max_memory_usage: 0
# Maximum memory usage for processing all concurrently running queries on the server. Zero means unlimited.
profiles.default.max_memory_usage_for_all_queries: 0
pd_servers:
- host: 172.16.0.165
ssh_port: 22
name: "pd-1"
client_port: 2379
peer_port: 2380
deploy_dir: "/pd-data/deploy/pd-2379"
data_dir: "/pd-data/data/pd-2379"
log_dir: "/pd-data/deploy/pd-2379/log"
- host: 172.16.0.250
ssh_port: 22
name: "pd-2"
client_port: 2379
peer_port: 2380
deploy_dir: "/pd-data/deploy/pd-2379"
data_dir: "/pd-data/data/pd-2379"
log_dir: "/pd-data/deploy/pd-2379/log"
- host: 172.16.0.202
ssh_port: 22
name: "pd-3"
client_port: 2379
peer_port: 2380
deploy_dir: "/pd-data/deploy/pd-2379"
data_dir: "/pd-data/data/pd-2379"
log_dir: "/pd-data/deploy/pd-2379/log"
tidb_servers:
- host: 172.16.0.161
ssh_port: 22
port: 4000
status_port: 10080
deploy_dir: "/tidb-data/deploy/tidb-4000"
log_dir: "/tidb-data/deploy/tidb-4000/log"
- host: 172.16.0.197
ssh_port: 22
port: 4000
status_port: 10080
deploy_dir: "/tidb-data/deploy/tidb-4000"
log_dir: "/tidb-data/deploy/tidb-4000/log"
- host: 172.16.0.187
ssh_port: 22
port: 4000
status_port: 10080
deploy_dir: "/tidb-data/deploy/tidb-4000"
log_dir: "/tidb-data/deploy/tidb-4000/log"
tikv_servers:
- host: 172.16.0.38
ssh_port: 22
port: 20160
status_port: 20180
deploy_dir: "/tikv-data/deploy/tikv-20160"
data_dir: "/tikv-data/data/tikv-20160"
log_dir: "/tikv-data/deploy/tikv-20160/log"
- host: 172.16.0.234
ssh_port: 22
port: 20160
status_port: 20180
deploy_dir: "/tikv-data/deploy/tikv-20160"
data_dir: "/tikv-data/data/tikv-20160"
log_dir: "/tikv-data/deploy/tikv-20160/log"
- host: 172.16.0.81
ssh_port: 22
port: 20160
status_port: 20180
deploy_dir: "/tikv-data/deploy/tikv-20160"
data_dir: "/tikv-data/data/tikv-20160"
log_dir: "/tikv-data/deploy/tikv-20160/log"
tiflash_servers:
- host: 172.16.0.66
ssh_port: 22
#tcp_port: 9000
#http_port: 8123
#flash_service_port: 3930
#flash_proxy_port: 20170
#flash_proxy_status_port: 20292
#metrics_port: 8234
deploy_dir: "/flash-data/deploy/tiflash-9000"
## The `data_dir` will be overwritten if you define `storage.main.dir` configurations in the `config` section.
data_dir: "/flash-data/data/tiflash-9000"
log_dir: "/flash-data/deploy/tiflash-9000/log"
- host: 172.16.0.88
ssh_port: 22
#tcp_port: 9000
#http_port: 8123
#flash_service_port: 3930
#flash_proxy_port: 20170
#flash_proxy_status_port: 20292
#metrics_port: 8234
deploy_dir: "/flash-data/deploy/tiflash-9000"
## The `data_dir` will be overwritten if you define `storage.main.dir` configurations in the `config` section.
data_dir: "/flash-data/data/tiflash-9000"
log_dir: "/flash-data/deploy/tiflash-9000/log"
monitoring_servers:
- host: 172.16.0.207
ssh_port: 22
port: 9090
deploy_dir: "/tidb-deploy/prometheus-9090"
data_dir: "/tidb-data/prometheus-9090"
log_dir: "/tidb-deploy/prometheus-9090/log"
grafana_servers:
- host: 172.16.0.207
port: 3000
deploy_dir: /tidb-deploy/grafana-3000
alertmanager_servers:
- host: 172.16.0.207
ssh_port: 22
web_port: 9093
cluster_port: 9094
deploy_dir: "/tidb-deploy/alertmanager-9093"
data_dir: "/tidb-data/alertmanager-9093"
log_dir: "/tidb-deploy/alertmanager-9093/log"
EOF
4.7 執行部署命令
tiup cluster deploy <cluster-name> v4.0.0 topology.yaml --user tidb [-p] [-i /home/tidb/.ssh/gcp_rsa]
4.8 啟動
tiup cluster start <cluter-name>
4.9 查看集群情況
tiup cluster list
4.10 檢查部署的 TiDB 集群情況
tiup cluster display <cluter-name>
五 訪問集群
1. 訪問數據庫
mysql -h ip -P 4000 -u root
2. 訪問grafana
http://ip:3000
默認用戶密碼都是admin
3. 訪問tidb的dashboard
http://ip:2379/dashboard
默認用戶名root
六 集群伸縮
6.1 擴展
tiup cluster scale-out
6.1.1 tidb
tidb_servers:
- host: 172.16.0.171
ssh_port: 11618
port: 4000
status_port: 10080
deploy_dir: "/tidb-data/deploy/tidb-4000"
log_dir: "/tidb-data/deploy/tidb-4000/log"
6.1.2 tikv
tikv_servers:
- host: 172.16.0.101
ssh_port: 11618
port: 20160
status_port: 20180
deploy_dir: "/tikv-data/deploy/tikv-20160"
data_dir: "/tikv-data/data/tikv-20160"
log_dir: "/tikv-data/deploy/tikv-20160/log"
6.2 縮減
tiup cluster scale-in
七 為tidb配置負載均衡
7.1 安裝haproxy
yum -y install epel-release gcc systemd-devel
yum -y install haproxy
which haproxy
7.2 編輯配置文件 方法1
cat << 'EOF' > /etc/haproxy/haproxy.cfg
global
#工作目錄
chroot /usr/local/haproxy
#日志文件,使用rsyslog服務中local5日志設備(/var/log/local5),等級info
log 127.0.0.1 local5 info
#守護進程運行
daemon
pidfile /data/haproxy/haproxy.pid
user tidb
group tidb
defaults
log global
mode http
#日志格式
option httplog
#日志中不記錄負載均衡的心跳檢測記錄
option dontlognull
#連接超時(毫秒)
timeout connect 30000
#客戶端超時(毫秒)
timeout client 3000000
#服務器超時(毫秒)
timeout server 3000000
#監控界面
listen admin_stats
#監控界面的訪問的IP和端口
bind 0.0.0.0:8888
#訪問協議
mode http
#URI相對地址
stats uri /dbs
#統計報告格式
stats realm Global\ statistics
#登陸帳戶信息
stats auth admin:abc123456
#數據庫負載均衡
listen proxy-mysql
#訪問的IP和���口
bind 0.0.0.0:3306
#網絡協議
mode tcp
#負載均衡算法(輪詢算法)
#輪詢算法:roundrobin
#權重算法:static-rr
#最少連接算法:leastconn
#請求源IP算法:source
balance roundrobin
#日志格式
option tcplog
# 在MySQL中創建一個沒有權限的haproxy用戶,密碼為空。Haproxy使用這個賬戶對MySQL數據庫心跳檢測
# create user haproxy@"172.16.%.%";
option mysql-check user haproxy
server tidb_1 <ip:4000> check weight 1 maxconn 10000 inter 600 fall 5
server tidb_2 <ip:4000> check weight 1 maxconn 10000 inter 600 fall 5
server tidb_3 <ip:4000> check weight 1 maxconn 10000 inter 600 fall 5
#使用keepalive檢測死鏈
option tcpka
EOF
7.3 編輯配置文件 方法二
cat << 'EOF' > /etc/haproxy/haproxy.cfg
global # 全局配置。
log 127.0.0.1 local2 # 定義全局的 syslog 服務器,最多可以定義兩個。
chroot /var/lib/haproxy # 更改當前目錄並為啟動進程設置超級用戶權限,從而提高安全性。
pidfile /var/run/haproxy.pid # 將 HAProxy 進程的 PID 寫入 pidfile。
maxconn 4000 # 每個 HAProxy 進程所接受的最大並發連接數。
user haproxy # 同 UID 參數。
group haproxy # 同 GID 參數,建議使用專用用戶組。
nbproc 40 # 在后台運行時創建的進程數。在啟動多個進程轉發請求時,確保該值足夠大,保證 HAProxy 不會成為瓶頸。
daemon # 讓 HAProxy 以守護進程的方式工作於后台,等同於命令行參數“-D”的功能。當然,也可以在命令行中用“-db”參數將其禁用。
stats socket /var/lib/haproxy/stats # 統計信息保存位置。
defaults # 默認配置。
log global # 日志繼承全局配置段的設置。
retries 2 # 向上游服務器嘗試連接的最大次數,超過此值便認為后端服務器不可用。
timeout connect 2s # HAProxy 與后端服務器連接超時時間。如果在同一個局域網內,可設置成較短的時間。
timeout client 30000s # 客戶端與 HAProxy 連接后,數據傳輸完畢,即非活動連接的超時時間。
timeout server 30000s # 服務器端非活動連接的超時時間。
listen admin_stats # frontend 和 backend 的組合體,此監控組的名稱可按需進行自定義。
bind 0.0.0.0:8080 # 監聽端口。
mode http # 監控運行的模式,此處為 `http` 模式。
option httplog # 開始啟用記錄 HTTP 請求的日志功能。
maxconn 10 # 最大並發連接數。
stats refresh 30s # 每隔 30 秒自動刷新監控頁面。
stats uri /haproxy # 監控頁面的 URL。
stats realm HAProxy # 監控頁面的提示信息。
stats auth <user>:<password> # 監控頁面的用戶和密碼,可設置多個用戶名。
stats hide-version # 隱藏監控頁面上的 HAProxy 版本信息。
stats admin if TRUE # 手工啟用或禁用后端服務器(HAProxy 1.4.9 及之后版本開始支持)。
listen tidb-cluster # 配置 database 負載均衡。
bind 0.0.0.0:3390 # 浮動 IP 和 監聽端口。
mode tcp # HAProxy 要使用第 4 層的傳輸層。
balance leastconn # 連接數最少的服務器優先接收連接。`leastconn` 建議用於長會話服務,例如 LDAP、SQL、TSE 等,而不是短會話協議,如 HTTP。該算法是動態的,對於啟動慢的服務器,服務器權重會在運行中作調整。
server tidb-1 <backend-ip1>:4000 check inter 2000 rise 2 fall 3 # 檢測 4000 端口,檢測頻率為每 2000 毫秒一次。如果 2 次檢測為成功,則認為服務器可用;如果 3 次檢測為失敗,則認為服務器不可用。
server tidb-2 <backend-ip2>:4000 check inter 2000 rise 2 fall 3
EOF
7.4 啟動
haproxy -D -f /etc/haproxy/haproxy.cfg
八 其他命令
# 在線修改配置文件
tiup cluster edit-config <cluster-name>
# 滾動重啟
tiup cluster reload <cluster-name> -R pd/tikv/tidb
# 重啟某個節點
tiup cluster restart <cluster-name> -N ip:port
# 修改集群名
tiup cluster rename <old-name> <new-name>
八 其他命令
# 在線修改配置文件
tiup cluster edit-config <cluster-name>
# 滾動重啟
tiup cluster reload <cluster-name> -R pd/tikv/tidb
# 重啟某個節點
tiup cluster restart <cluster-name> -N ip:port
# 修改集群名
tiup cluster rename <old-name> <new-name>
其他參數設置
innodb_lock_wait_timeout=60
interactive_timeout=300
wait_timeout=300
sql_mode ='NO_ENGINE_SUBSTITUTION,PIPES_AS_CONCAT'
transaction_isolation ='READ-COMMITTED'
去掉主鍵開關,會話級
set tidb_allow_remove_auto_inc=1;
GC查詢和設置保留一天
select VARIABLE_NAME, VARIABLE_VALUE from mysql.tidb where VARIABLE_NAME like "tikv_gc%";
update mysql.tidb set VARIABLE_VALUE="24h" where VARIABLE_NAME="tikv_gc_life_time";