1. 硬件环境检查
在安装tidb数据库之前,必须对服务器的硬件环境进行全面检查,确保满足tidb 6.5的最低要求。更多学习教程www.fgedu.net.cn
# free -h
total used free shared buff/cache available
Mem: 64G 2.2G 60G 9.1M 1.8G 61G
Swap: 32G 0B 32G
# 检查磁盘空间
# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 32G 0 32G 0% /dev
tmpfs 32G 0 32G 0% /dev/shm
tmpfs 32G 9.1M 32G 1% /run
tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/sda1 50G 18G 33G 35% /
/dev/sdb1 500G 25G 475G 5% /data
# 检查CPU核心数
# nproc
32
# 检查系统架构
# uname -m
x86_64
2. 操作系统检查
tidb 6.5支持Red Hat Enterprise Linux 7.0及以上版本。本文以RHEL 9.0为例。学习交流加群风哥微信: itpux-com
# cat /etc/redhat-release
Red Hat Enterprise Linux release 9.0 (Plow)
# 检查内核版本
# uname -r
5.14.0-70.22.1.el9_0.x86_64
# 检查SELinux状态
# getenforce
Disabled
# 关闭SELinux(如未关闭)
# vi /etc/selinux/config
SELINUX=disabled
# 检查防火墙状态
# systemctl status firewalld
# 开放tidb相关端口
# firewall-cmd –permanent –add-port=4000/tcp
# firewall-cmd –permanent –add-port=2379/tcp
# firewall-cmd –permanent –add-port=2380/tcp
# firewall-cmd –permanent –add-port=10080/tcp
# firewall-cmd –permanent –add-port=10081/tcp
# firewall-cmd –reload
3. 安装前准备
安装tidb数据库前,需要进行一系列准备工作,包括创建用户、目录结构和配置系统参数。
# groupadd tidb
# useradd -g tidb tidb
# passwd tidb
Changing password for user tidb.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
# 创建安装目录
# mkdir -p /data/tidb
# mkdir -p /data/tidb/data
# mkdir -p /backup/tidb
# chown -R tidb:tidb /data/tidb
# chown -R tidb:tidb /backup/tidb
# chmod -R 755 /data/tidb
# vi /etc/sysctl.conf
# 添加以下参数
fs.file-max = 6815744
net.core.somaxconn = 32768
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_probes = 9
net.ipv4.tcp_keepalive_intvl = 30
vm.swappiness = 0
vm.overcommit_memory = 1
# 使参数生效
# sysctl -p
# vi /etc/security/limits.conf
# 添加以下内容
tidb soft nproc 65535
tidb hard nproc 65535
tidb soft nofile 65535
tidb hard nofile 65535
tidb soft core unlimited
tidb hard core unlimited
4. tidb软件安装
tidb 6.5软件安装使用tiup工具进行部署。
# curl –proto ‘=https’ –tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
# 重新加载环境变量
# source ~/.bashrc
# 检查tiup版本
# tiup –version
# 安装tidb集群组件
# tiup cluster
# 准备集群配置文件
# vi /data/tidb/cluster.yaml
# 集群配置文件内容
global:
user: “tidb”
ssh_port: 22
deploy_dir: “/data/tidb/deploy”
data_dir: “/data/tidb/data”
server_configs:
tidb:
log.slow-threshold: 300
tikv:
readpool.storage.use-unified-pool: true
readpool.coprocessor.use-unified-pool: true
pd:
replication.enable-placement-rules: true
pd_servers:
– host: 192.168.1.51
– host: 192.168.1.52
– host: 192.168.1.53
tidb_servers:
– host: 192.168.1.51
– host: 192.168.1.52
– host: 192.168.1.53
tikv_servers:
– host: 192.168.1.51
– host: 192.168.1.52
– host: 192.168.1.53
monitoring_servers:
– host: 192.168.1.51
grafana_servers:
– host: 192.168.1.51
alertmanager_servers:
– host: 192.168.1.51
# tiup cluster deploy tidb-cluster v6.5.0 /data/tidb/cluster.yaml –user root
# 启动tidb集群
# tiup cluster start tidb-cluster
# 查看集群状态
# tiup cluster status tidb-cluster
5. 数据库配置
安装完成后,需要创建和配置数据库实例。
# mysql -h 192.168.1.51 -P 4000 -u root
# 创建数据库
mysql> CREATE DATABASE fgedudb;
# 创建用户并授权
mysql> CREATE USER ‘fgedu’@’%’ IDENTIFIED BY ‘password’;
mysql> GRANT ALL PRIVILEGES ON fgedudb.* TO ‘fgedu’@’%’;
mysql> FLUSH PRIVILEGES;
# vi /data/tidb/deploy/tidb-4000/tidb.toml
# 修改以下参数
[performance]
max-procs = 8
[log]
slow-query-file = “slow-query.log”
slow-threshold = 300
# 重启tidb服务
# tiup cluster restart tidb-cluster -R tidb
6. 测试验证
数据库创建完成后,需要进行测试验证,确保数据库正常运行。学习交流加群风哥QQ113257174
# mysql -h 192.168.1.51 -P 4000 -u fgedu -p fgedudb
# 创建测试表
mysql> CREATE TABLE fgedu_employees (
mysql> emp_id INT PRIMARY KEY,
mysql> emp_name VARCHAR(50),
mysql> salary DECIMAL(10,2),
mysql> hire_date DATE
mysql> );
# 插入测试数据
mysql> INSERT INTO fgedu_employees VALUES (1, ‘张三’, 5000.00, CURRENT_DATE);
mysql> INSERT INTO fgedu_employees VALUES (2, ‘李四’, 6000.00, CURRENT_DATE);
mysql> COMMIT;
# 查询测试数据
mysql> SELECT * FROM fgedu_employees;
+——–+———-+———+————+
| emp_id | emp_name | salary | hire_date |
+——–+———-+———+————+
| 1 | 张三 | 5000.00 | 2026-03-31 |
| 2 | 李四 | 6000.00 | 2026-03-31 |
+——–+———-+———+————+
7. 备份配置
为确保数据安全,需要配置数据库备份策略。
# tiup install br
# 创建全量备份脚本
# vi /data/tidb/scripts/full_backup.sh
#!/bin/bash
BACKUP_DIR=/backup/tidb/full
DATE=$(date +%Y%m%d%H%M%S)
mkdir -p $BACKUP_DIR
# 执行全量备份
tiup br backup full –pd “192.168.1.51:2379” –storage “local://$BACKUP_DIR” –backupts “$DATE”
# 赋予执行权限
# chmod +x /data/tidb/scripts/full_backup.sh
# 配置定时任务
# crontab -e
# 添加每日凌晨2点执行全量备份
0 2 * * * /data/tidb/scripts/full_backup.sh
8. 升级迁移
本节介绍tidb数据库的升级和迁移方法。更多学习教程公众号风哥教程itpux_com
8.1 数据库升级
# tiup cluster display tidb-cluster
# 升级tidb集群
# tiup cluster upgrade tidb-cluster v6.5.1
# 验证升级结果
# tiup cluster display tidb-cluster
# 连接数据库验证版本
# mysql -h 192.168.1.51 -P 4000 -u root -e “SELECT version();”
+——————–+
| version() |
+——————–+
| 5.7.25-TiDB-v6.5.1 |
+——————–+
8.2 数据库迁移
# tiup install dumpling
# tiup dumpling -h 192.168.1.51 -P 4000 -u root -t 8 -o /backup/tidb/migration
# 使用lightning工具导入数据到目标集群
# tiup install lightning
# tiup lightning –backend tidb –host 192.168.2.51 –port 4000 –user root –password password –db fgedudb /backup/tidb/migration
# 验证迁移结果
# mysql -h 192.168.2.51 -P 4000 -u fgedu -p fgedudb -e “SELECT COUNT(*) FROM fgedu_employees;”
+———-+
| count(*) |
+———-+
| 2 |
+———-+
9. 性能优化
为提高tidb数据库性能,需要进行一系列参数优化。
# vi /data/tidb/deploy/tidb-4000/tidb.toml
# 优化参数
[performance]
max-procs = 8
txn-total-size-limit = 1073741824
[log]
slow-query-file = “slow-query.log”
slow-threshold = 300
# 配置tikv参数
# vi /data/tidb/deploy/tikv-20160/tikv.toml
# 优化参数
[readpool.storage]
use-unified-pool = true
[readpool.coprocessor]
use-unified-pool = true
[storage]
reserve-space = “0”
# 配置pd参数
# vi /data/tidb/deploy/pd-2379/pd.toml
# 优化参数
[replication]
enable-placement-rules = true
max-replicas = 3
# 重启集群使参数生效
# tiup cluster restart tidb-cluster
# tiup cluster edit-config tidb-cluster
10. 故障排查
本节介绍tidb数据库常见故障的排查方法。from:fengge
10.1 集群无法启动
# tiup cluster status tidb-cluster
# 查看组件日志
# tiup cluster log tidb-cluster -R tidb
# tiup cluster log tidb-cluster -R tikv
# tiup cluster log tidb-cluster -R pd
10.2 连接失败
# ping 192.168.1.51
# 检查端口是否开放
# netstat -tuln | grep 4000
# 检查tidb服务状态
# tiup cluster status tidb-cluster -R tidb
10.3 性能问题
# 访问 http://192.168.1.51:3000
# 查看慢查询
# tiup cluster log tidb-cluster -R tidb –file slow-query.log
# 分析执行计划
# mysql -h 192.168.1.51 -P 4000 -u fgedu -p fgedudb
mysql> EXPLAIN SELECT * FROM fgedu_employees WHERE salary > 5000;
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
