第一步:下载
下线路径 :http://download.pingcap.org/tidb-latest-linux-amd64.tar.gz
https://github.com/pingcap/tidb/archive/refs/tags/v7.1.1.tar.gz
服务有网的话直接下载:wget http://download.pingcap.org/tidb-latest-linux-amd64.tar.gz
第二步:解压
tar -zxvf tidb-latest-linux-amd64.tar.gz
第三步:(个人下载解压出来是v5.0.1)
cd tidb-v5.0.1-linux-amd64
第四步:配置启动密令
./bin/pd-server --name=wanma \
--data-dir=data \
--client-urls="http://10.9.5.61:2379" \
--peer-urls="http://10.9.5.61:2380" \
--initial-cluster="wanma=http://10.9.5.61:2380" \
--log-file=data.log &
./bin/tikv-server --pd="10.9.5.61:2379" \
--addr="10.9.5.61:20160" \
--data-dir=tikv \
--log-file=tikv.log &
./bin/tidb-server --store=tikv \
--path="10.9.5.61:2379" \
--log-file=tidb.log &
下面解析一下上面的密令:
PD(Placement Driver)启动命令:
./bin/pd-server --name=wanma \
--data-dir=data \
--client-urls="http://10.9.5.61:2379" \
--peer-urls="http://10.9.5.61:2380" \
--initial-cluster="wanma=http://10.9.5.61:2380" \
--log-file=data.log &
--name=wanma
: 设置 PD 实例的名称为 "wanma"。--data-dir=data
: 指定数据存储的目录。--client-urls
: PD 对外提供的客户端访问地址。--peer-urls
: PD 之间通信的地址。--initial-cluster
: 初始的集群拓扑配置。--log-file=data.log
: 指定日志文件。
TiKV 启动命令:
./bin/tikv-server --pd="10.9.5.61:2379" \
--addr="10.9.5.61:20160" \
--data-dir=tikv \
--log-file=tikv.log &
--pd
: TiKV 连接的 PD 地址。--addr
: TiKV 对外提供服务的地址。--data-dir
: 数据存储的路径。--log-file=tikv.log
: 指定日志文件。
TiDB 启动命令:
./bin/tidb-server --store=tikv \
--path="10.9.5.61:2379" \
--log-file=tidb.log \
--sql-mode=NO_ENGINE_SUBSTITUTION \
--socket=/path/to/tidb.sock \
--bind-address=0.0.0.0 \
--secure-auth=0 \
--enable-auto-scaling=0 \
--skip-grant-tables=0 \
--default-authentication-plugin=mysql_native_password \
--server-id=1 \
--port=4000 \
--user=root \
--password=your_password
-----------------个人使用
./bin/tidb-server --store=tikv \
--path="10.9.5.61:2379" \
--log-file=tidb.log &
-
--store=tikv
: 指定使用 TiKV 作为存储引擎。 -
--path="10.9.5.61:2379"
: 指定连接到 PD 的地址。 -
--log-file=tidb.log
: 指定日志文件的路径。 -
--sql-mode=NO_ENGINE_SUBSTITUTION
: 设置 SQL 模式,其中NO_ENGINE_SUBSTITUTION
确保在引擎不可用时不会自动替代。 -
--socket=/path/to/tidb.sock
: 指定用于连接的 Unix 套接字路径。 -
--bind-address=0.0.0.0
: 指定绑定的 IP 地址,这里设置为允许从任何 IP 地址连接。 -
--secure-auth=0
: 禁用安全认证。 -
--enable-auto-scaling=0
: 禁用自动扩展。 -
--skip-grant-tables=0
: 禁用跳过权限验证。 -
--default-authentication-plugin=mysql_native_password
: 指定默认的身份验证插件。 -
--server-id=1
: 指定服务器 ID。 -
--port=4000
: 指定 TiDB 服务器监听的端口。 -
--user=root
: 指定连接数据库时使用的用户名。 -
--password=your_password
: 指定连接数据库时使用的密码。请将your_password
替换为实际的密码。
-----第五步:验证TIDB是否启动成功
ps -ef |grep TIDB
----第六步:连接TIDB
连接前要看下有无mysql客户端,没有的情况下:
sudo dnf search mysql
sudo dnf install mysql-server.x86_64
有点话直接执行:
mysql -h10.9.5.61 -P4000 -uroot -p
-----第七步:测试TIDB
没有设置密码就直接enter,下面开始测试:
设置密码:ALTER USER 'root'@'%' IDENTIFIED BY 'xxxxxxxxxxxxxxxxx';
刷新权限:FLUSH PRIVILEGES;
创建数据库:
CREATE DATABASE mydb;
使用mydb
USE mydb;
创建表:
CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50) NOT NULL, email VARCHAR(100) NOT NULL );
插入数据:
INSERT INTO users (username, email) VALUES ('john_doe', 'john@example.com'), ('jane_smith', 'jane@example.com'), ('alice_johnson', 'alice@example.com');
查询数据:
SELECT * FROM users;
修改数据:
UPDATE users
SET email = '123456_email@example.com'
WHERE username = 'john_doe';
删除数据:
DELETE FROM users WHERE username = 'jane_smith';
设置远程连接前:
修改密码:ALTER USER 'root'@'%' IDENTIFIED BY 'new_password';
FLUSH PRIVILEGES;
退出:exit
登入: mysql -h10.9.5.61 -P4000 -uroot -p
输入密码:xxxxxxxxxxx
设置TIDB随服务器启动而启动:
检查是否已经存在 tidb.service 文件:
systemctl list-unit-files | grep tidb
如果已经存在 tidb.service,您可以跳过创建步骤,直接进入编辑步骤。
如果不存在 tidb.service,创建服务单元文件:
sudo nano /etc/systemd/system/tidb.service
在编辑器中输入 TiDB 服务的配置信息:
[Unit]
Description=TiDB Server
After=network.target
[Service]
User=root # 替换为您要运行 TiDB 的用户名
Group=root # 替换为用户组,通常与用户名相同
WorkingDirectory=/usr/local/tidb-v5.0.1-linux-amd64 # TiDB 安装路径
ExecStart=/usr/local/tidb-v5.0.1-linux-amd64/bin/pd-server --name=wanma \
--data-dir=data \
--client-urls="http://10.9.5.61:2379" \
--peer-urls="http://10.9.5.61:2380" \
--initial-cluster="wanma=http://10.9.5.61:2380" \
--log-file=data.log && \
/usr/local/tidb-v5.0.1-linux-amd64/bin/tikv-server --pd="10.9.5.61:2379" \
--addr="10.9.5.61:20160" \
--data-dir=tikv \
--log-file=tikv.log && \
/usr/local/tidb-v5.0.1-linux-amd64/bin/tidb-server --store=tikv \
--path="10.9.5.61:2379" \
--log-file=tidb.log
Restart=always
RestartSec=3
LimitNOFILE=10000
[Install]
WantedBy=multi-user.target
内容填写写:先查出各项
用户名和用户组: 你可以通过以下命令来查看当前系统中的用户名和用户组:
id -u -n # 获取当前用户名
id -g -n # 获取当前用户组名
TiDB Server 启动路径:
which tidb-server
启用并启动 TiDB 服务:
sudo systemctl enable tidb.service
sudo systemctl start tidb.service
验证服务是否已经启动:
sudo systemctl status tidb.service
如果状态显示为活动(active),则表示 TiDB 服务已经成功启动。
解释一下上面的密令:
[Unit]
Description=TiDB Server
After=network.target
[Service]
Type=simple
User=<your_tidb_user>
Group=<your_tidb_group>
ExecStart=/path/to/tidb-server \
--store=tikv \
--path="10.9.5.61:2379" \
--log-file=/path/to/tidb.log \
--sql-mode=NO_ENGINE_SUBSTITUTION \
--socket=/path/to/tidb.sock \
--bind-address=0.0.0.0 \
--secure-auth=0 \
--enable-auto-scaling=0 \
--skip-grant-tables=0 \
--default-authentication-plugin=mysql_native_password \
--server-id=1 \
--port=4000 \
--user=root \
--password=your_password
Restart=always
[Install]
WantedBy=multi-user.target
- 替换
<your_tidb_user>
为你想要用于运行 TiDB 服务的用户名。 - 替换
<your_tidb_group>
为相应的用户组名。 - 替换
/path/to/tidb-server
为实际的 TiDB Server 启动路径。 - 替换
/path/to/tidb.log
为 TiDB 日志文件的路径。 - 替换
10.9.5.61:2379
为实际的 PD 地址和端口。 - 替换
/path/to/tidb.sock
为 TiDB 的 Unix 套接字路径。 - 替换
your_password
为你想要设置的 TiDB 数据库密码。
完成这些更改后,将整个内容添加到 tidb.service
文件中,并将文件保存。然后,你可以使用 systemctl
命令来管理 TiDB 服务,例如启动、停止和重启