【TIDB】TIDB 负载均衡

1.安装haproxy软件 


[tidb@mysql1 haproxy-1.8.9]$ sudo make install PREFIX=/usr/local/haproxy
install -d "/usr/local/haproxy/sbin"
install haproxy  "/usr/local/haproxy/sbin"
install -d "/usr/local/haproxy/share/man"/man1
install -m 644 doc/haproxy.1 "/usr/local/haproxy/share/man"/man1
install -d "/usr/local/haproxy/doc/haproxy"
for x in configuration management architecture peers-v2.0 cookie-options lua WURFL-device-detection proxy-protocol linux-syn-cookies network-namespaces DeviceAtlas-device-detection 51Degrees-device-detection netscaler-client-ip-insertion-protocol peers close-options SPOE intro; do \
	install -m 644 doc/$x.txt "/usr/local/haproxy/doc/haproxy" ; \
done
[tidb@mysql1 haproxy-1.8.9]$ /usr/local/haproxy/sbin/haproxy -v
HA-Proxy version 1.8.9-83616ec 2018/05/18
Copyright 2000-2018 Willy Tarreau <willy@haproxy.org>

2.配置haproxy 

[root@mysql1 log]# cat  /usr/local/haproxy/haproxy.cfg
global
	log 127.0.0.1 local0 notice
	user haproxy
	group haproxy
		daemon
		#quiet
		nbproc 40
		chroot /usr/local/haproxy
		pidfile /usr/local/haproxy/haproxy.pid
defaults
	log global
	retries 2
	timeout connect 2s
	timeout client 30000s
	timeout server 30000s
listen mytidbcluster 
	bind 0.0.0.0:3390
	mode tcp
	stats hide-version
	balance roundrobin
	server mysql1 192.168.1.11:4000 check inter 2000 rise 2 fall 3 
	server mysql2 192.168.1.12:4000 check inter 2000 rise 2 fall 3
	server mysql3 192.168.1.13:4000 check inter 2000 rise 2 fall 3
listen haproxy_stats
	mode http
	bind *:8080
	option httplog
	stats refresh 30s
	stats uri /haproxy
	stats realm xxx moritor
	stats realm Haproxy Manager
	stats auth haproxy:haproxy
	stats hide-version

3.登陆访问HAPROXY 

--测试第一个节点HAPROXY 的负载均衡。
[root@mysql1 log]# mysql -uroot -h192.168.1.11 -P 3390 -p'rootroot' -e "select @@hostname;select * from test.my_test1;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql3     |
+------------+
+------+------------+
| id   | name       |
+------+------------+
|    1 | 薛双奇1    |
|    2 | 薛双奇2    |
+------+------------+
[root@mysql1 log]# mysql -uroot -h192.168.1.11 -P 3390 -p'rootroot' -e "select @@hostname;select * from test.my_test1;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql1     |
+------------+
+------+------------+
| id   | name       |
+------+------------+
|    1 | 薛双奇1    |
|    2 | 薛双奇2    |
+------+------------+
[root@mysql1 log]# mysql -uroot -h192.168.1.11 -P 3390 -p'rootroot' -e "select @@hostname;select * from test.my_test1;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql2     |
+------------+
+------+------------+
| id   | name       |
+------+------------+
|    1 | 薛双奇1    |
|    2 | 薛双奇2    |
+------+------------+
[root@mysql1 log]# mysql -uroot -h192.168.1.11 -P 3390 -p'rootroot' -e "select @@hostname;select * from test.my_test1;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql3     |
+------------+
+------+------------+
| id   | name       |
+------+------------+
|    1 | 薛双奇1    |
|    2 | 薛双奇2    |
+------+------------+
[root@mysql1 log]# mysql -uroot -h192.168.1.11 -P 3390 -p'rootroot' -e "select @@hostname;select * from test.my_test1;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql1     |
+------------+
+------+------------+
| id   | name       |
+------+------------+
|    1 | 薛双奇1    |
|    2 | 薛双奇2    |
+------+------------+
[root@mysql1 log]# mysql -uroot -h192.168.1.11 -P 3390 -p'rootroot' -e "select @@hostname;select * from test.my_test1;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql2     |
+------------+
+------+------------+
| id   | name       |
+------+------------+
|    1 | 薛双奇1    |
|    2 | 薛双奇2    |
+------+------------+

第二节点访问的负载均衡测试

--通过第二个HAPROXY节点访问集群。
[root@mysql1 log]# mysql -uroot -h192.168.1.12 -P 3390 -p'rootroot' -e "select @@hostname;select * from test.my_test1;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql2     |
+------------+
+------+------------+
| id   | name       |
+------+------------+
|    1 | 薛双奇1    |
|    2 | 薛双奇2    |
+------+------------+
[root@mysql1 log]# mysql -uroot -h192.168.1.12 -P 3390 -p'rootroot' -e "select @@hostname;select * from test.my_test1;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql3     |
+------------+
+------+------------+
| id   | name       |
+------+------------+
|    1 | 薛双奇1    |
|    2 | 薛双奇2    |
+------+------------+
[root@mysql1 log]# mysql -uroot -h192.168.1.12 -P 3390 -p'rootroot' -e "select @@hostname;select * from test.my_test1;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql1     |
+------------+
+------+------------+
| id   | name       |
+------+------------+
|    1 | 薛双奇1    |
|    2 | 薛双奇2    |
+------+------------+
[root@mysql1 log]# mysql -uroot -h192.168.1.12 -P 3390 -p'rootroot' -e "select @@hostname;select * from test.my_test1;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql1     |
+------------+
+------+------------+
| id   | name       |
+------+------------+
|    1 | 薛双奇1    |
|    2 | 薛双奇2    |
+------+------------+
[root@mysql1 log]# mysql -uroot -h192.168.1.12 -P 3390 -p'rootroot' -e "select @@hostname;select * from test.my_test1;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql2     |
+------------+
+------+------------+
| id   | name       |
+------+------------+
|    1 | 薛双奇1    |
|    2 | 薛双奇2    |
+------+------------+
[root@mysql1 log]# mysql -uroot -h192.168.1.12 -P 3390 -p'rootroot' -e "select @@hostname;select * from test.my_test1;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql1     |
+------------+
+------+------------+
| id   | name       |
+------+------------+
|    1 | 薛双奇1    |
|    2 | 薛双奇2    |
+------+------------+
[root@mysql1 log]# mysql -uroot -h192.168.1.12 -P 3390 -p'rootroot' -e "select @@hostname;select * from test.my_test1;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql1     |
+------------+
+------+------------+
| id   | name       |
+------+------------+
|    1 | 薛双奇1    |
|    2 | 薛双奇2    |
+------+------------+
[root@mysql1 log]# mysql -uroot -h192.168.1.12 -P 3390 -p'rootroot' -e "select @@hostname;select * from test.my_test1;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql1     |
+------------+
+------+------------+
| id   | name       |
+------+------------+
|    1 | 薛双奇1    |
|    2 | 薛双奇2    |
+------+------------+
[root@mysql1 log]# mysql -uroot -h192.168.1.12 -P 3390 -p'rootroot' -e "select @@hostname;select * from test.my_test1;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql3     |
+------------+
+------+------------+
| id   | name       |
+------+------------+
|    1 | 薛双奇1    |
|    2 | 薛双奇2    |
+------+------------+

由此可见,所有节点,均可访问,实现了负载均衡的目的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值