haproxy实战之haproxy实现mysql负载均衡

haproxy实战之haproxy实现mysql负载均衡

实验目的
haproxy + mysql实现负载均衡

1.准备机器,做好时间同步,域名主机名解析

192.168.42.151 [node2 haproxy]
192.168.42.152 [node3 mariadb]
192.168.42.153 [node4 mariadb]

2.node3,node4上安装mariadb

(1).安装mariadb

yum install mariadb-server -y

(2).更改基本配置

vim /etc/my.cnf.d/server.cnf
[mysqld] 
skip_name_resolve=1
log-bin=mysql-bin
innodb_file_per_table = 1

(3).启动mariadb

systemctl start mariadb

(4).更改密码

mysqladmin -uroot -p password "root"

(5).登录mysql

mysql -u root -p

(6).添加外部访问账号

MariaDB [(none)]> grant all privileges on *.* to 'magedu'@'192.168.42.%' identified by '123456';
MariaDB [(none)]> flush privileges;

(7)登录测试一下

mysql -u magedu -h 192.168.42.155 -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>

3.安装haproxy
node1: (1).haproxy的安装

yum install haproxy -y

(2).配置haproxy 配置文件如下:

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    tcp
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  mysql
    bind *:3306
    log   global
    mode  tcp
    default_backend             mysqlsrvs

backend mysqlsrvs
    balance     roundrobin
    server      mysql1 192.168.42.152:3306 check
    server      mysql2 192.168.42.153:3306 check

(3)启动haproxy

systemctl start haproxy
[root@node2 haproxy]# ss -tnl
State      Recv-Q Send-Q Local Address:Port  Peer Address:Port              
LISTEN     0      128               *:22               *:*                  
LISTEN     0      100       127.0.0.1:25               *:*                  
LISTEN     0      3000              *:3306             *:*                  
LISTEN     0      128              :::22              :::*                  
LISTEN     0      100             ::1:25              :::*

(4)安装mariadb 客户端工具

yum install mariadb -y

(5).测试

mysql -u maged -h 192.168.42.151 -p

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+

此时是只有4个库,我们创建一个库,并退出

MariaDB [(none)]> create database  leip;
Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| leip               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> exit;
Bye

(6).再一次连接测试

[root@node2 haproxy]# mysql -u magedu -h 192.168.42.151 -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> exit
Bye

从上面可以看到,此时我们的数据库是只有4个库了,因此我们可以判断,我们连接的是两个不同主机上的mariadb,也就是说负载均衡mysql成功了

原创文章,作者:srayban,如若转载,请注明出处:http://www.178linux.com/78829

(4)
sraybansrayban
上一篇 2017-06-29
下一篇 2017-06-29

相关推荐

  • thinkpad e420编译安装thinkfan控制风扇

    我的笔记本是win7+linuxmint双系统,在进入linuxmint长时间运行后会明显感觉发热,我发现笔记本风扇的转数过低,导致热量不能发散出去,解决方法就是安装thinkfan风扇控制软件。 1、下载软件包 https://sourceforge.net/projects/thinkfan/ 最新版本是1.0beta2 2、编译安装 编译前确保安装过c…

    Linux干货 2017-03-09
  • Nginx作为web服务器的使用配置

    概述     Nginx是一款免费开源的web服务器,同时也可以作为http、imap/pop3协议进行反代服务器,本篇介绍一些nginx作为web服务器方面的相关配置,具体包含:     1、nginx基础概念介绍     2、nginx…

    Linux干货 2016-10-27
  • 马哥教育网络班21期+第10周课程练习

    1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情) POST(Power On Self Test): 检测系统外围关键设备(如:CPU、内存、显卡、I/O、键盘鼠标等)是否正常。 加载BIOS(Basic Input and Output System): 根据在BIOS中设置的系统启动顺序来搜索用于启动系统的驱动器(硬盘、光盘、U…

    Linux干货 2016-09-08
  • centos6.9的安装

    先准备一个安装虚拟机的的软件VMware Workstation Pro和光盘centos6.9和光盘 然后打开该软件,点击创建新的虚拟机   然后会出现一个新建虚拟机向导,选择典型,点下一步 继续点下一步   把虚拟机名称改成所装的版本 然后新建一个文件夹,点击浏览,接着点击这个新建的文件夹,这个文件夹就是虚拟机所装入的位置,…

    2017-07-15
  • 马哥教育网络班N22期+第8周课程练习

    1、请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别。 网桥:是连接两个局域网的基于MAC地址数据存储转发设备,工作于数据链路层集线器:所有端口处于同一个广播域和冲突域中,带宽共享,工作于物理层二层交换机:多端口网桥,一个端口一个冲突域,默认所有端口位于同一个广播域中,可以划分vlan,隔离广播域,带宽独享三层交换机:具有路由功能的二…

    Linux干货 2016-10-19