How to Set Up TiDB Server Auto-Start

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 如何设置tidb server自启动

| username: TiDBer_kMTvTIEv

[TiDB Usage Environment] Production Environment / Testing / PoC
System: Ubuntu 18.04
[TiDB Version]
7.5.0
[Reproduction Path] What operations were performed when the issue occurred
[Encountered Issue: Issue Phenomenon and Impact]
I have deployed the TiDB server on a local server. I only want to use the TiDB server database and do not need a cluster. How can I configure it to set the TiDB server database to start automatically on boot?

| username: 啦啦啦啦啦 | Original post link

I remember it is set to start automatically by default.
Use tiup cluster enable/disable <cluster-name> to control whether it starts automatically on boot.

| username: 普罗米修斯 | Original post link

It won’t work if you only use tidb-server without a cluster, right?

| username: TiDBer_kMTvTIEv | Original post link

How do I set up the cluster and TiDB server to start automatically?

| username: 普罗米修斯 | Original post link

The above reply mentioned tiup cluster enable <cluster-name>.

| username: 江湖故人 | Original post link

The command tiup cluster enable is used to set the cluster service to auto-start after a machine reboot. This command will execute systemctl enable <service> on the specified nodes to enable the service’s auto-start.

| username: tidb菜鸟一只 | Original post link

The tidb-server is just a stateless node, and all data is stored on tikv. It is possible to use tikv alone, but it is impossible to use tidb-server alone. The entire cluster must be started to use tidb-server, so just execute tiup cluster enable clustername.

| username: 小龙虾爱大龙虾 | Original post link

Using only the tidb-server executable to start an instance? Although this can work, there may be many bugs. It is recommended to use tiup to create a playground environment for testing. If you are only using tidb-server, then you should write a service yourself.

| username: andone | Original post link

tiup cluster enable

| username: 小龙虾爱大龙虾 | Original post link

You can start an instance by directly executing tidb-server :joy_cat:

| username: kelvin | Original post link

tiup cluster enable is correct

| username: 小龙虾爱大龙虾 | Original post link

He didn’t even install tiup, how can he use tiup? Didn’t they say there is only one tidb-server executable file? :joy_cat:

| username: TiDBer_kMTvTIEv | Original post link

I have installed tiup. My intention is for the server to start the tidb-server automatically when it boots up. It doesn’t matter whether tiup starts or not.

| username: 小龙虾爱大龙虾 | Original post link

Then just create a service file and manage it with systemctl.

| username: Jellybean | Original post link

For first-time users of TiDB who want to experience or verify its features, it is recommended to use tiup playground to start a TiDB cluster on a single machine. This can be done with just a few commands, making it very convenient and fast, requiring very few resources while still allowing you to experience the cluster features.

For more details, please visit the official website:

| username: TiDBer_kMTvTIEv | Original post link

Can the cluster created by tiup playground be set to start automatically on boot?

| username: Jellybean | Original post link

The tiup playground is a command that essentially starts a database service for you. If you want it to start automatically on boot, you can set it to execute this command automatically at startup. You can manage it automatically using systemctl.

If you want to use previous data, i.e., have persistence, you can use it with the --tag option.

  • If you wish to persist data, you can execute TiUP with the --tag parameter: tiup --tag <your-tag> playground .... For more details, refer to the TiUP Reference Manual.

If you want a new, empty cluster every time you restart, you don’t need to use the --tag parameter.

| username: 小龙虾爱大龙虾 | Original post link

You write a tidb-4000.service file and place it in the /etc/systemd/system/ directory:

[Unit]
Description=TiDB Server
After=network.target

[Service] 
Type=simple
User=tidb
Group=tidb
KillMode=control-group
Restart=always
RestartSec=5
StartLimitInterval=0
ExecStart=/home/tidb/.tiup/components/tidb/v7.2.0/tidb-server

[Install]   
WantedBy=multi-user.target

Then execute:

sudo systemctl daemon-reload
sudo systemctl enable tidb-4000.service
sudo systemctl start tidb-4000.service 
sudo systemctl status tidb-4000.service 

This should work, but directly executing the tidb-server executable will likely store data in /tmp, and it is uncertain if the previous data files can be used again upon re-execution. It is highly discouraged to use TiDB in this manner.

| username: TiDBer_小阿飞 | Original post link

The command tiup cluster enable is used to set the cluster service to auto-start after a machine reboot. This command will execute systemctl enable <service> on the specified nodes to enable the service’s auto-start.

Note:
When the entire cluster is shut down and restarted, the order in which services start is determined by the operating system’s boot sequence on the nodes. If the restart order is incorrect, in some cases, the cluster may still not be able to provide services after the restart (for example, if TiKV starts before PD, and systemd restarts TiKV multiple times without finding PD, it will give up).

Syntax:
tiup cluster enable [flags]

| username: Kongdom | Original post link

Execute systemctl enable <service> to enable the service to start automatically.