I tried TiDB on MacBook M2!

# Table of Contents

1. Introduction
2. Environment　
3. What is TiDB?
4. What is TiUP Playground?
5. Download and Install TiUP
6. Declare the global environment variable
7. Launch the cluster (TiUP Playground)
8. Start a new session to access TiDB
9. Demonstrating TiDB Dashboard
10. Demonstrating Grafana
11. Summary
12. Reference

# Introduction

I recently had a chance to hear about TiDB, so I thought I'd give it a try.
I also had an opportunity to attend an event "TiUG KICK OFF" recently held on 26th February 2024 at WESTALL Yotsuya Building.

https://tiug.connpass.com/event/303186/

That was also a good experience to know various features about TiDB.
But this time I am going to share my experience on trying out TiDB on macOS M2.

# Environment

* macOS Ventura v13.0(Apple M2)

# What is TiDB?

TiDB, an abbreviation of Titanium Database, is an open source distributed SQL database compatible with MySQL.
It is referred to as NewSQL. Although there are some conditions, it is good to hear that it can be used as MySQL compatible.
This makes me curious to know more about TiDB.
For more information, please refer to the [official documentation](https://docs.pingcap.com/ja/tidb/stable/overview).

# What is TiUP Playground?

TiUP is a tool to download and install TiDB components.
TiUP Playground is the mechanism to build the TiDB clusters.
The basic usage of the playground component is as follows:
```
tiup playground
```
Running this command will install TiDB cluster.
Before running this command, you need to download the TiDB cluster.

# Download and Install TiUP
First download the TiUP test cluster and then launch the TiDB cluster.
You can download the TiUP from the command below.
```
curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
```

# Declare the global environment variable
After the installation, TiUP displays the absolute path of the corresponding profile file. You need to modify the following source command according to the path.
If you got the following message, then you have successfully installed TiUP:
```
Successfully set mirror to https://tiup-mirrors.pingcap.com
Detected shell: zsh
Shell profile:  /Users/testuser/.zshrc
/Users/testuser/.zshrc has been modified to add tiup to PATH
open a new terminal or source /Users/testuser/.zshrc to use it
Installed path: /Users/testuser/.tiup/bin/tiup
===============================================
Have a try:     tiup playground
===============================================
```

Now, copy the file name from the Shell profile in my case it is .zshrc after that we will declare this variable globally by using the following command.
```
source .bashrc
```

# Launch the cluster (TiUP Playground)
If you want to start a TiDB cluster of the latest version, run the following command:
```
tiup playground
```
After execution of the above code we will get a result in terminal that looks like this.

![Screenshot 2024-01-30 at 17.07.40.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/327994/296e7246-73cd-bb83-a267-9ef3bdd9a59f.jpeg)

In this result, we can clearly view the information about connecting TiDB, TiDB dashboard, and Grafana.

# Start a new session to access TiDB
If you haven't installed the MySQL yet, then use the following command to install:
```
brew update
brew install mysql
```
After successful installation, you can use the MySQL client to connect to TiDB.
```
mysql --host 127.0.0.1 --port 4000 -u root
```
After this command, we are able to get the MySQL command line in our terminal.

![Screenshot 2024-01-30 at 17.26.59.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/327994/442c09b9-2534-a274-a3c6-cea4e8919a0a.jpeg)


Now we can continue working on MySQL like creating database show database and other MySQL queries.

Let's try the show databases option.

These are list of default databases during the MySQL client installed.

![Screenshot 2024-01-30 at 17.42.04.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/327994/a1ce49c7-565a-b474-65ea-9f534f5c2e34.jpeg)

Let's create a test database called tidb_test using this SQL command.
```
create database tidb_test;
```
then change the database to tidb_test using this MySQL command.
```
use tidb_test;
```
Next is to create the table name tidb_user;
```
CREATE TABLE tidb_user(
        User_ID int NOT NULL  AUTO_INCREMENT,
        FullName varchar(255) NOT NULL,
    Email varchar(255) NOT NULL,
    Password varchar(255) NOT NULL,
    Phone  varchar(255),
    Address varchar(255),
    Gender  varchar(255),
    Dob  varchar(255),
    PRIMARY KEY (User_ID)
);
```
and insert data into it.
```
INSERT INTO tidb_user(FullName, Email, Password) VALUES("Test TiDB User","test_tidb@gmail.com","tidb_123");
```
after that we can check data of that table;
```
select * from tidb_user;
```
![Screenshot 2024-01-30 at 17.58.29.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/327994/39127a8e-8576-4cc8-ac1e-7fb44138ebe9.png)

This will confirm that MySQL is working fine.

# Demonstrating TiDB Dashboard
By running tiup playground command, we have got some information related to the TiDB dashboard.
![Screenshot 2024-01-30 at 18.14.55.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/327994/8e09e026-283b-62a7-1f16-2d8eaa373b55.png)

The host's IP address will be the IP address of the local machine. Because we are not running on the virtual machine, there will not be any different IP address from the host machine.

The dashboard looks initially like this:

![Screenshot 2024-01-30 at 18.17.24.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/327994/045ded88-49d5-509b-3bc7-77779fa86895.png)

There is a sign in option displayed. The default username is root with password empty. This will log in to the TiDB dashboard.

![Screenshot 2024-01-30 at 18.19.53.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/327994/bbef9fa0-6a22-79d1-dc91-84f811e2b66b.png)

Besides dashboard, there are different other pages, Cluster Info page, Top SQL, SQL Statements, Slow Queries and so on.

# Demonstrating Grafana
Grafana is an open source monitoring system for analyzing and visualizing metrics.
Similarly, you can access the grafana by the following link which will display the grafana login page:
```
http://127.0.0.1:3000
```
The default username and password for grafana is admin. Use this login details, and later you can set the new password.
This is how the grafana home page looks like.

![Screenshot 2024-01-30 at 18.34.47.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/327994/5c628298-09a4-ebd7-ee15-384d5f0cd336.png)

This is all about grafana monitoring.

# Summary
I think it's a good experience to use TiDB. It is faster than MySQL as it supports parallel query execution for select queries and can utilize many more CPU cores.
MySQL is limited to a single CPU core for a single select query. But in terms of using a large number of servers, I think it will be challenging to use TiDB unless you have a fairly large-scale environment.
If it's just a small DB, the cost may be prohibitive.
This time I haven't tried the db performance test. But surely I would like to try out various things in the future.

# Reference

https://docs.pingcap.com/tidb/dev/quick-start-with-tidb

https://tidbcloud.com/free-trial?__hstc=86493575.a6f6bd890e8cb7a56825a3d6bf4da768.1690080592786.1690080592786.1690080592786.1&__hssc=86493575.3.1690080592786&__hsfp=3567315901&_gl=1*cmvipz*_ga*MTIwMDA2NTYxMi4xNjkwMDMwOTEz*_ga_9FRXHHPYVY*MTY5MDA4MDU5MS4xLjEuMTY5MDA4MDkzMy4zMi4wLjA.&_ga=2.108647105.29318933.1690030913-1200065612.1690030913

https://docs.pingcap.com/ja/tidb/stable/overview