分布式数据库TiDB的部署
转自:https://my.oschina.net/Kenyon/blog/908370
一、环境
CentOS Linux release 7.3.1611 (Core)
172.26.11.91 pd & tidb
172.26.11.92 tikv
172.26.11.93 tikv
172.26.11.94 tikv
二、安装
分别在4台服务器上上传安装包
wget http://download.pingcap.org/tidb-latest-linux-amd64.tar.gz
tar -xzf tidb-latest-linux-amd64.tar.gz
cd tidb-latest-linux-amd64
mkdir -p /data/tidb/log
ln -s /root/software/tidb/tidb-latest-linux-amd64/bin/pd-tso-bench /usr/bin
ln -s /root/software/tidb/tidb-latest-linux-amd64/bin/tikv-server /usr/bin/
ln -s /root/software/tidb/tidb-latest-linux-amd64/bin/tidb-server /usr/bin/
ln -s /root/software/tidb/tidb-latest-linux-amd64/bin/pd-server /usr/bin/
ln -s /root/software/tidb/tidb-latest-linux-amd64/bin/pd-ctl /usr/bin/
三、配置使用
1.按照顺序启动
在91上启动pd服务
pd-server --name=pd1 --多个pd以不同名字命名
--data-dir=/data/tidb/pd --pd路径
--client-urls="http://172.26.11.91:2379"
--peer-urls="http://172.26.11.91:2380"
--initial-cluster="pd1=http://172.26.11.91:2380" --多个pd以逗号分隔
--log-file=/data/tidb/log/pd.log &
在92,93,94上启动tikv
tikv-server --pd="172.26.11.91:2379" \
--addr="172.26.11.92:20160" \
--data-dir=/data/tidb/tikv \
--log-file=/data/tidb/log/tikv.log
tikv-server --pd="172.26.11.91:2379" \
--addr="172.26.11.93:20160" \
--data-dir=/data/tidb/tikv \
--log-file=/data/tidb/log/tikv.log
tikv-server --pd="172.26.11.91:2379" \
--addr="172.26.11.94:20160" \
--data-dir=/data/tidb/tikv \
--log-file=/data/tidb/log/tikv.log &
在91上启动tipd服务
tidb-server --store=tikv \ --tikv引擎允许分布式存储,其他如LevelDB等是本地存储
--path="172.26.11.91:2379" \
--log-file=/data/tidb/log/tidb.log &
2.登陆使用
[root@test05 ~]# mysql -h 172.26.11.91 -P 4000 -u root -D test
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.1-TiDB-1.0 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type \'help;\' or \'\h\' for help. Type \'\c\' to clear the current input statement.
mysql> create database db_kenyon;
Query OK, 0 rows affected (2.02 sec)
mysql> use db_kenyon;
Database changed
mysql> create table tbl_kenyon(user_code varchar(64) primary key,user_name varchar(32),ctime timestamp);
Query OK, 0 rows affected (2.03 sec)
mysql> insert into tbl_kenyon values(\'01\',\'qiaofeng\',now()),(\'02\',\'murong\',now());
Query OK, 2 rows affected (0.01 sec)
mysql>
三、高可用
tidb的数据都是保存在tikv节点上面,比如上面配置了3套tikv,每套tikv都是独立的,数据保存的方式和传统关系型不一样的是,在tidb里面或者说tikv里面是映射成kv模式存储的
把92的tikv人为挂掉,此时数据库的使用会受影响,简单的一个查询就会被挂起,直到切换成功
--切换过程
mysql> select * from tbl_kenyon;
+----+-----------+---------------------+
| id | cname | ctime |
+----+-----------+---------------------+
| 1 | qiaofeng | 2017-05-23 10:50:43 |
| 2 | murong | 2017-05-23 10:50:43 |
| 3 | saodiseng | 2017-05-23 10:50:43 |
+----+-----------+---------------------+
3 rows in set (10.24 sec)
--切换以后
mysql> select * from tbl_kenyon;
+----+-----------+---------------------+
| id | cname | ctime |
+----+-----------+---------------------+
| 1 | qiaofeng | 2017-05-23 10:50:43 |
| 2 | murong | 2017-05-23 10:50:43 |
| 3 | saodiseng | 2017-05-23 10:50:43 |
+----+-----------+---------------------+
3 rows in set (0.00 sec)
可以发现经过投票,PD已经连到94上去了(93,92上都能看到),此时读取表数据很快
2017/05/23 17:59:12.151 server.rs:153: [INFO] TiKV is ready to serve
2017/05/23 17:59:12.517 raft.rs:846: [INFO] [region 2] 3 [term: 1487] received a MsgHeartbeat message with higher term from 7 [term: 1488]
2017/05/23 17:59:12.517 raft.rs:681: [INFO] [region 2] 3 became follower at term 1488
2017/05/23 17:59:12.525 server.rs:460: [INFO] resolve store 6 address ok, addr 172.26.11.94:20160
2017/05/23 17:59:13.517 apply.rs:621: [INFO] [region 2] 3 execute admin command cmd_type: CompactLog compact_log {compact_index: 6437 compact_term: 1488} at [term: 1488, index: 6439]
2017/05/23 17:59:13.644 raftlog_gc.rs:117: [INFO] [region 2] collected 225 log entries
2017/05/23 17:59:43.517 apply.rs:621: [INFO] [region 2] 3 execute admin command cmd_type: CompactLog compact_log {compact_index: 6498 compact_term: 1488} at [term: 1488, index: 6500]
2017/05/23 17:59:43.643 raftlog_gc.rs:117: [INFO] [region 2] collected 61