tidb安裝

1.準(zhǔn)備機(jī)器

組件 IP 操作系統(tǒng)版本
TIKV 192.168.3.81,192.168.3.82,192.168.3.83 CentOS 7.5
PD 192.168.3.84 CentOS 7.5
TIDB 192.168.3.84 CentOS 7.5
中控機(jī) 192.168.3.85 CentOS 7.5

2.在中控機(jī)安裝系統(tǒng)依賴(lài)包

yum -y install epel-release git curl sshpass
yum -y install python-pip

3.在中控機(jī)上創(chuàng)建tidb用戶(hù),并生成ssh key

以root用戶(hù)登錄中控機(jī),執(zhí)行以下命令創(chuàng)建tidb用戶(hù)
useradd -m -d /home/tidb tidb
設(shè)置tidb用戶(hù)密碼
passwd tidb
配置tidb用戶(hù) sudo 免密碼,將tidb ALL=(ALL) NOPASSWD: ALL添加到文件末尾即可。

vim /etc/sudoers
tidb ALL=(ALL) NOPASSWD: ALL

生成 ssh key: 執(zhí)行 su 命令從 root 用戶(hù)切換到 tidb 用戶(hù)下。
su - tidb

創(chuàng)建 tidb 用戶(hù) ssh key, 提示 Enter passphrase 時(shí)直接回車(chē)即可。執(zhí)行成功后,ssh 私鑰文件為 /home/tidb/.ssh/id_rsa, ssh 公鑰文件為 /home/tidb/.ssh/id_rsa.pub。

 ~]$ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/tidb/.ssh/id_rsa): 
Created directory '/home/tidb/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/tidb/.ssh/id_rsa.
Your public key has been saved in /home/tidb/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:eCHuCQdBeRSql3DlJK27AKjbpGb0HuXcvkNXE/iVjok tidb@tidb05_192.168.3.85
The key's randomart image is:
+---[RSA 2048]----+
|   .++=.  .   .  |
|    .Bo  . . o   |
|. . +oo . o *    |
|o  +.+ o E * .   |
|... +o+ S . .    |
|..o.=+.+ .       |
|.=.o ++..        |
|oo..o ..         |
|o ..   oo        |
+----[SHA256]-----+

4.在中控機(jī)器上下載 TiDB-Ansible

以 tidb 用戶(hù)登錄中控機(jī)并進(jìn)入 /home/tidb 目錄。
使用以下命令從 Github TiDB-Ansible 項(xiàng)目上下載 TiDB-Ansible 相應(yīng)分支,默認(rèn)的文件夾名稱(chēng)為tidb-ansible。

  • 下載 2.0 版本:

git clone -b release-2.0 https://github.com/pingcap/tidb-ansible.git

注:請(qǐng)務(wù)必按文檔操作,將 tidb-ansible 下載到 /home/tidb 目錄下,權(quán)限為 tidb 用戶(hù),不要下載到 /root 下,否則會(huì)遇到權(quán)限問(wèn)題。

5.在中控機(jī)器上安裝 Ansible 及其依賴(lài)

以tidb用戶(hù)登錄中控機(jī),請(qǐng)務(wù)必按以下方式通過(guò) pip 安裝 Ansible 及其相關(guān)依賴(lài)的指定版本,否則會(huì)有兼容問(wèn)題。安裝完成后,可通過(guò)ansible --versoin查看 Ansible 版本。目前 release-2.0、release-2.1 及 master 版本兼容 Ansible 2.4 及 Ansible 2.5 版本,Ansible 及相關(guān)依賴(lài)版本記錄在 tidb-ansible/requirements.txt 文件中。

 cd /home/tidb/tidb-ansible
 sudo pip install -r ./requirements.txt
You should consider upgrading via the 'pip install --upgrade pip' command.
[tidb@tidb05_192.168.3.85 ~/tidb-ansible]$ ansible --version
ansible 2.6.15
  config file = /home/tidb/tidb-ansible/ansible.cfg
  configured module search path = [u'/home/tidb/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible
  python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]

6.在中控機(jī)上配置部署機(jī)器 ssh 互信及 sudo 規(guī)則

以 tidb 用戶(hù)登錄中控機(jī),將你的部署目標(biāo)機(jī)器 IP 添加到 hosts.ini 文件 [servers] 區(qū)塊下。

[tidb@tidb05_192.168.3.85 ~/tidb-ansible]$cd /home/tidb/tidb-ansible/
[tidb@tidb05_192.168.3.85 ~/tidb-ansible]$cat hosts.ini 
[servers]
192.168.3.81
192.168.3.82
192.168.3.83
192.168.3.84

[all:vars]
username = tidb
ntp_server = pool.ntp.org

執(zhí)行以下命令,按提示輸入部署目標(biāo)機(jī)器root用戶(hù)密碼。該步驟將在部署目標(biāo)機(jī)器上創(chuàng)建tidb用戶(hù),并配置 sudo 規(guī)則,配置中控機(jī)與部署目標(biāo)機(jī)器之間的 ssh 互信。
ansible-playbook -i hosts.ini create_users.yml -u root -k

7.在部署目標(biāo)機(jī)器上安裝 NTP 服務(wù)

該步驟將在部署目標(biāo)機(jī)器上使用系統(tǒng)自帶軟件源聯(lián)網(wǎng)安裝并啟動(dòng) NTP 服務(wù),服務(wù)使用安裝包默認(rèn)的 NTP server 列表,見(jiàn)配置文件 /etc/ntp.conf 中 server 參數(shù),如果使用默認(rèn)的 NTP server,你的機(jī)器需要連接外網(wǎng)。 為了讓 NTP 盡快開(kāi)始同步,啟動(dòng) NTP 服務(wù)前,系統(tǒng)會(huì) ntpdate hosts.ini 文件中的 ntp_server 一次,默認(rèn)為 pool.ntp.org,也可替換為你的 NTP server。

cd /home/tidb/tidb-ansible
ansible-playbook -i hosts.ini deploy_ntp.yml -u tidb -b

8.在部署目標(biāo)機(jī)器上配置 CPUfreq 調(diào)節(jié)器模式

為了讓 CPU 發(fā)揮最大性能,請(qǐng)將 CPUfreq 調(diào)節(jié)器模式設(shè)置為performanc模式。

你可以通過(guò) cpupower 命令查看系統(tǒng)支持的調(diào)節(jié)器模式:

[tidb@centos7_05 tidb-ansible]$ cpupower frequency-info --governors
analyzing CPU 0:
  available cpufreq governors: performance powersave

本例中系統(tǒng)支持設(shè)置 performance 和 powersave 模式。如果返回 “Not Available”,表示當(dāng)前系統(tǒng)不支持配置 CPUfreq,跳過(guò)該步驟即可。

你可以通過(guò)cpupower 命令查看系統(tǒng)當(dāng)前的 CPUfreq 調(diào)節(jié)器模式:

本例中當(dāng)前配置是 powersave 模式,你可以通過(guò)以下命令設(shè)置為 performance 模式。
你也可以通過(guò)以下命令在部署目標(biāo)機(jī)器上批量設(shè)置:

$ ansible -i hosts.ini all -m shell -a "cpupower frequency-set --governor performance" -u tidb -b

9.在部署目標(biāo)機(jī)器上添加數(shù)據(jù)盤(pán) ext4 文件系統(tǒng)掛載參數(shù)

部署目標(biāo)機(jī)器數(shù)據(jù)盤(pán)請(qǐng)格式化成 ext4 文件系統(tǒng),掛載時(shí)請(qǐng)?zhí)砑?nodelalloc 和 noatime 掛載參數(shù)。nodelalloc是必選參數(shù),否則 Ansible 安裝時(shí)檢測(cè)無(wú)法通過(guò),noatime 是可選建議參數(shù)。

下面以 /dev/sdb 數(shù)據(jù)盤(pán)為例:

查看數(shù)據(jù)盤(pán)

fdisk -l
Disk /dev/sdb: 21.5 GB

創(chuàng)建分區(qū)表

parted -s -a optimal /dev/sdb mklabel gpt -- mkpart primary ext4 1 -1

格式化文件系統(tǒng)

mkfs.ext4 /dev/sdb

查看數(shù)據(jù)盤(pán)分區(qū) UUID,本例中 sdb 的 UUID 為 b42e4005-afdc-4abe-822c-3d766f01b7f5。

[root@tidb01_192.168.3.81 ~]#lsblk -f
NAME            FSTYPE      LABEL           UUID                                   MOUNTPOINT
sda                                                                                
├─sda1          xfs                         97c182dd-9521-4ab9-99f0-a485dfa9559d   /boot
└─sda2          LVM2_member                 faVDCu-Eej4-EGru-Zvt0-c6BK-6MaU-gY8cgk 
├─centos-root xfs                         e9acfba6-573a-4b6a-9aac-2af2cb69f28d   /
├─centos-swap swap                        ccd05e39-541c-4919-80f8-238e89119e82   [SWAP]
└─centos-home xfs                         7bca643f-5eec-4a9f-b6e1-39af9a435e13   /home
sdb             ext4                        b42e4005-afdc-4abe-822c-3d766f01b7f5   
sr0             iso9660     CentOS 7 x86_64 2018-05-03-20-55-23-00        

編輯 /etc/fstab 文件,添加 nodelalloc 掛載參數(shù)

vi /etc/fstab
UUID=b42e4005-afdc-4abe-822c-3d766f01b7f5 /data ext4 defaults,nodelalloc,noatime 0 2

掛載數(shù)據(jù)盤(pán)

mkdir /data
mount -a

執(zhí)行以下命令,如果文件系統(tǒng)為 ext4,并且掛載參數(shù)中包含 nodelalloc 表示生效:

[root@tidb01_192.168.3.81 ~]#mount -t ext4
/dev/sdb on /data type ext4 (rw,noatime,nodelalloc,data=ordered)

10.分配機(jī)器資源,編輯 inventory.ini 文件

以 tidb 用戶(hù)登錄中控機(jī),inventory.ini 文件路徑為 /home/tidb/tidb-ansible/inventory.ini。
標(biāo)準(zhǔn) TiDB 集群需要 6 臺(tái)機(jī)器:

  • 2 個(gè) TiDB 節(jié)點(diǎn)
  • 3 個(gè) PD 節(jié)點(diǎn)
  • 3 個(gè) TiKV 節(jié)點(diǎn),第一臺(tái) TiDB 機(jī)器同時(shí)用作監(jiān)控機(jī)

默認(rèn)情況下,單臺(tái)機(jī)器上只需部署一個(gè) TiKV 實(shí)例。如果你的 TiKV 部署機(jī)器 CPU 及內(nèi)存配置是部署建議的兩倍或以上,并且擁有兩塊 SSD 硬盤(pán)或單塊容量超 2T 的 SSD 硬盤(pán),
可以考慮部署兩實(shí)例,但不建議部署兩個(gè)以上實(shí)例。

單機(jī)單 TiKV 實(shí)例集群拓?fù)?/p>

Name HOST IP Services
tidb01_192.168.3.81 192.168.3.81 TiKV1
tidb02_192.168.3.82 192.168.3.82 TiKV2
tidb03_192.168.3.83 192.168.3.83 TiKV3
tidb04_192.168.3.84 192.168.3.84 tidb/pd
[tidb_servers]
192.168.3.84

[tikv_servers]
192.168.3.81
192.168.3.82
192.168.3.83

[pd_servers]
192.168.3.84

[monitoring_servers]
192.168.3.84

[grafana_servers]
192.168.3.84

[monitored_servers]
192.168.3.81
192.168.3.82
192.168.3.83
192.168.3.84

11.部署目錄調(diào)整

部署目錄通過(guò) deploy_dir 變量控制,默認(rèn)全局變量已設(shè)置為 /home/tidb/deploy,對(duì)所有服務(wù)生效。如數(shù)據(jù)盤(pán)掛載目錄為 /data,可設(shè)置為 /data/deploy,樣例如下:

vim /home/tidb/tidb-ansible/inventory.ini
## Global variables
[all:vars]
deploy_dir = /data/deploy

12.部署任務(wù)

ansible-playbook 執(zhí)行 Playbook 時(shí)默認(rèn)并發(fā)為 5,部署目標(biāo)機(jī)器較多時(shí)可添加 -f 參數(shù)指定并發(fā),如 ansible-playbook deploy.yml -f 10

①確認(rèn) tidb-ansible/inventory.ini 文件中 ansible_user = tidb,本例使用 tidb 用戶(hù)作為服務(wù)運(yùn)行用戶(hù),配置如下:

## Connection
# ssh via normal user
ansible_user = tidb

ansible_user 不要設(shè)置成 root 用戶(hù),tidb-ansible 限制了服務(wù)以普通用戶(hù)運(yùn)行。

執(zhí)行以下命令如果所有 server 返回 tidb 表示 ssh 互信配置成功。

[tidb@tidb05_192.168.3.85 ~/tidb-ansible]$ansible -i inventory.ini all -m shell -a 'whoami'
192.168.3.81 | SUCCESS | rc=0 >>
tidb

192.168.3.84 | SUCCESS | rc=0 >>
tidb

192.168.3.82 | SUCCESS | rc=0 >>
tidb

192.168.3.83 | SUCCESS | rc=0 >>
tidb

執(zhí)行以下命令如果所有 server 返回 root 表示 tidb 用戶(hù) sudo 免密碼配置成功。

[tidb@tidb05_192.168.3.85 ~/tidb-ansible]$ansible -i inventory.ini all -m shell -a 'whoami' -b
192.168.3.83 | SUCCESS | rc=0 >>
root

192.168.3.81 | SUCCESS | rc=0 >>
root

192.168.3.82 | SUCCESS | rc=0 >>
root

192.168.3.84 | SUCCESS | rc=0 >>
root

②執(zhí)行 local_prepare.yml playbook,聯(lián)網(wǎng)下載 TiDB binary 到中控機(jī):
ansible-playbook local_prepare.yml

③初始化系統(tǒng)環(huán)境,修改內(nèi)核參數(shù)
ansible-playbook bootstrap.yml

報(bào)錯(cuò)

TASK [check_system_optional : Preflight check - Check TiDB server's CPU] *************************************
fatal: [192.168.3.84]: FAILED! => {"changed": false, "msg": "This machine does not have sufficient CPU to run TiDB, at least 8 cores."}

NO MORE HOSTS LEFT *******************************************************************************************
        to retry, use: --limit @/home/tidb/tidb-ansible/retry_files/bootstrap.retry

PLAY RECAP ***************************************************************************************************
192.168.3.81               : ok=29   changed=11   unreachable=0    failed=0   
192.168.3.82               : ok=29   changed=11   unreachable=0    failed=0   
192.168.3.83               : ok=29   changed=11   unreachable=0    failed=0   
192.168.3.84               : ok=29   changed=11   unreachable=0    failed=1   
localhost                  : ok=1    changed=0    unreachable=0    failed=0   


ERROR MESSAGE SUMMARY ****************************************************************************************
[192.168.3.84]: Ansible FAILED! => playbook: bootstrap.yml; TASK: check_system_optional : Preflight check - Check TiDB server's CPU; message: {"changed": false, "msg": "This machine does not have sufficient CPU to run TiDB, at least 8 cores."}

Ask for help:
Contact us: support@pingcap.com
It seems that you encounter some problems. You can send an email to the above email address, attached with the tidb-ansible/inventory.ini and tidb-ansible/log/ansible.log files and the error message, or new issue on https://github.com/pingcap/tidb-ansible/issues. We'll try our best to help you deploy a TiDB cluster. Thanks. :-)

vim bootstrap.yml 修改該文件 并注釋掉以下的內(nèi)容

- name: check system
  hosts: all
  any_errors_fatal: true
  roles:
    - check_system_static
   # - { role: check_system_optional, when: not dev_mode|default(false) }

如果是非SSD 測(cè)試的話(huà) ,最好將如下的內(nèi)容注釋掉

- name: tikv_servers machine benchmark
  hosts: tikv_servers
  gather_facts: false
  roles:
   # - { role: machine_benchmark, when: not dev_mode|default(false) }

④部署 TiDB 集群軟件
ansible-playbook deploy.yml
報(bào)錯(cuò)

TASK [check_system_dynamic : Preflight check - NTP service] ***********************************************************************************************************
fatal: [192.168.3.81]: FAILED! => {"changed": false, "msg": "Make sure NTP service is running and ntpstat is synchronised to NTP server. See https://github.com/pingcap/docs/blob/master/op-guide/ansible-deployment.md#how-to-check-whether-the-ntp-service-is-normal ."}
fatal: [192.168.3.82]: FAILED! => {"changed": false, "msg": "Make sure NTP service is running and ntpstat is synchronised to NTP server. See https://github.com/pingcap/docs/blob/master/op-guide/ansible-deployment.md#how-to-check-whether-the-ntp-service-is-normal ."}
fatal: [192.168.3.83]: FAILED! => {"changed": false, "msg": "Make sure NTP service is running and ntpstat is synchronised to NTP server. See https://github.com/pingcap/docs/blob/master/op-guide/ansible-deployment.md#how-to-check-whether-the-ntp-service-is-normal ."}
fatal: [192.168.3.84]: FAILED! => {"changed": false, "msg": "Make sure NTP service is running and ntpstat is synchronised to NTP server. See https://github.com/pingcap/docs/blob/master/op-guide/ansible-deployment.md#how-to-check-whether-the-ntp-service-is-normal ."}

NO MORE HOSTS LEFT ****************************************************************************************************************************************************
        to retry, use: --limit @/home/tidb/tidb-ansible/retry_files/deploy.retry

PLAY RECAP ************************************************************************************************************************************************************
192.168.3.81               : ok=4    changed=0    unreachable=0    failed=1   
192.168.3.82               : ok=3    changed=0    unreachable=0    failed=1   
192.168.3.83               : ok=3    changed=0    unreachable=0    failed=1   
192.168.3.84               : ok=3    changed=0    unreachable=0    failed=1   
localhost                  : ok=1    changed=0    unreachable=0    failed=0   


ERROR MESSAGE SUMMARY *************************************************************************************************************************************************
[192.168.3.81]: Ansible FAILED! => playbook: deploy.yml; TASK: check_system_dynamic : Preflight check - NTP service; message: {"changed": false, "msg": "Make sure NTP service is running and ntpstat is synchronised to NTP server. See https://github.com/pingcap/docs/blob/master/op-guide/ansible-deployment.md#how-to-check-whether-the-ntp-service-is-normal ."}

[192.168.3.82]: Ansible FAILED! => playbook: deploy.yml; TASK: check_system_dynamic : Preflight check - NTP service; message: {"changed": false, "msg": "Make sure NTP service is running and ntpstat is synchronised to NTP server. See https://github.com/pingcap/docs/blob/master/op-guide/ansible-deployment.md#how-to-check-whether-the-ntp-service-is-normal ."}

[192.168.3.83]: Ansible FAILED! => playbook: deploy.yml; TASK: check_system_dynamic : Preflight check - NTP service; message: {"changed": false, "msg": "Make sure NTP service is running and ntpstat is synchronised to NTP server. See https://github.com/pingcap/docs/blob/master/op-guide/ansible-deployment.md#how-to-check-whether-the-ntp-service-is-normal ."}

[192.168.3.84]: Ansible FAILED! => playbook: deploy.yml; TASK: check_system_dynamic : Preflight check - NTP service; message: {"changed": false, "msg": "Make sure NTP service is running and ntpstat is synchronised to NTP server. See https://github.com/pingcap/docs/blob/master/op-guide/ansible-deployment.md#how-to-check-whether-the-ntp-service-is-normal ."}

Ask for help:
Contact us: support@pingcap.com
It seems that you encounter some problems. You can send an email to the above email address, attached with the tidb-ansible/inventory.ini and tidb-ansible/log/ansible.log files and the error message, or new issue on https://github.com/pingcap/tidb-ansible/issues. We'll try our best to help you deploy a TiDB cluster. Thanks. :-)

使用以下命令可使 NTP 服務(wù)盡快開(kāi)始同步

sudo systemctl stop ntpd.service
sudo ntpdate pool.ntp.org
sudo systemctl start ntpd.service
ntpstat

⑤啟動(dòng) TiDB 集群
ansible-playbook start.yml

13測(cè)試集群

測(cè)試連接 TiDB 集群,推薦在 TiDB 前配置負(fù)載均衡來(lái)對(duì)外統(tǒng)一提供 SQL 接口。

使用 MySQL 客戶(hù)端連接測(cè)試,TCP 4000 端口是 TiDB 服務(wù)默認(rèn)端口。

mysql -u root -h 192.168.3.84 -P 4000

通過(guò)瀏覽器訪問(wèn)監(jiān)控平臺(tái)。

地址:http://192.168.3.84:3000 默認(rèn)帳號(hào)密碼是:admin/admin

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容