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 20:04
下一篇 2017-06-29 22:19

相关推荐

  • Linux简要发展史

    目录 Linux简述 Linux之父 Linux标志 企鹅的来源 Tux的来源 历史 Unix GNU BSD Minix Linux 主要特性 Linux内核版本 Linux发行版 Linux简述 ·         Linux是一套自由加开放源代码的类Unix操作系统,诞生于…

    Linux干货 2016-10-18
  • Linux发行版、发行版联系与区别

    参考这篇文章<2016年最佳Linux发行版排行榜> http://mt.sohu.com/20160128/n436204298.shtml #1 最好的回归发行版:OpenSUSE OpenSUSE背后的SUSE公司是最古老的Linux企业,成立于Linus Torvalds放出Linux的一年之后。它其实早于Red Hat诞生,同时也是社区…

    Linux干货 2016-08-15
  • N28-第三周博客作业

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
    2、取出最后登录到当前系统的用户的相关信息。
    3、取出当前系统上被用户当作其默认shell的最多的那个shell。
    4、将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
    5、取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。
    6、列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。
    7、显示/var目录下一级子目录或文件的总个数。
    8、取出/etc/group文件中第三个字段数值最小的10个组的名字。
    9、将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。
    10、请总结描述用户和组管理类命令的使用方法并完成以下练习:
    (1)、创建组distro,其GID为2016;
    (2)、创建用户mandriva, 其ID号为1005;基本组为distro;
    (3)、创建用户mageia,其ID号为1100,家目录为/home/linux;
    (4)、给用户mageia添加密码,密码为mageedu;
    (5)、删除mandriva,但保留其家目录;
    (6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
    (7)、修改slackware的默认shell为/bin/tcsh;
    (8)、为用户slackware新增附加组admins;

    Linux干货 2017-12-19
  • 软件包管理器之二——YUM介绍及使用

    一、前言     之前我们介绍了RPM的应用(详细请查看http://www.178linux.com/archives/6383),发觉RPM安装软件有一个很繁琐的问题,那就是包与包之间的依赖关系,如果想正常安装和使用软件程序,就必须根据要求一步一步的解决软件之间的依赖关系。那么如果程序使用的包很多,将会很消耗使用人…

    Linux干货 2015-07-21
  • N25-第5周作业

    1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行 [root@jizo ~]# grep -E '^[[:space:]]+.*' /boot/grub/gurb.conf 2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,…

    Linux干货 2017-01-09
  • RAID简介

    RAID是什么? RAID英文名字叫Redundant Arrays of Independent Disks,也叫磁盘阵列?阵列有是什么意思?通俗的理解就是把很多的硬盘组织在一起来使用。       RAID可以根据性能来划分为两种:有无冗余(容错)能力     &n…

    Linux干货 2016-02-14