HAProxy 负载平衡问题

HAProxy 负载平衡问题

我有以下配置用于平衡precona集群的负载。

haproxy配置文件

global
        log 127.0.0.1   local1 notice
        maxconn 4096
        user haproxy
        group haproxy
        #debug
        #quiet

defaults
        log     global
        mode    http
        option  tcplog
        option  dontlognull
        retries 3
        maxconn 2000
        timeout connect 3000
        timeout server 5000
        timeout client 5000

listen mysql-cluster
    bind 127.0.0.1:3306
    mode tcp
    balance roundrobin
    option mysql-check user root

    server db01 192.168.54.158:3306 check
    server db02 192.168.54.140:3306 check

我跟着配置 haproxy 的教程。但是我无法从 haproxy 实例连接到集群。

mysql -u root -proot -h 127.0.0.1 --port=3306 -e "show variables like 'server_id'"

抛出以下错误。

ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0

我尝试通过查看 haproxy 日志来调试该问题。但是 /var/log/syslog 中没有任何日志,或者 /var/log/ 中没有这样的文件 haproxy.log

我甚至尝试将 /etc/rsyslog.d/49-haproxy.conf 更新为以下内容并重新启动 rsyslog 服务

$ModLoad imudp
$UDPServerRun 514

local0.*                        -/var/log/haproxy-0.log
local1.*                        -/var/log/haproxy-1.log
&~

我在 ubuntu 14.04.1 LTS 上使用 haproxy 版本 1.5.15。我可以直接从 haproxy 节点连接到 mysql 集群。这里配置错了什么?如何在 haproxy 中正确启用日志记录?

答案1

 Allow mysql server for remote login -:
 /etc/mysql/my.cnf    
127.0.0.1 to 0.0.0.0 

   Note – This command run on both mysql server 

root@mysql-1# mysql -u root -p -e "INSERT INTO mysql.user (Host,User) values ('haproxy server ip ','haproxy_check'); FLUSH PRIVILEGES;"

 root@mysql-1# mysql -u root -p -e "INSERT INTO mysql.user (Host,User) values ('haproxy server ip','haproxy_root'); FLUSH PRIVILEGES;"

   A MySQL user is needed with root privileges when accessing the MySQL cluster from HAProxy. The default root user on all the servers are allowed to login only locally. While this can be fixed by granting additional privileges to the root user, it is better to have a separate user with root privileges.

注意-此命令在mysql服务器上运行

root@mysql-1# mysql -u root -p -e "GRANT ALL PRIVILEGES ON *.* TO 'haproxy_root'@'haproxy server ip' IDENTIFIED BY 'password' WITH GRANT OPTION; FLUSH PRIVILEGES"

 # sudo service mysql restart
 Then run below cmnd -:

  sudo mysql -h haproxyserverIP  -u haproxy_root -p -e "show variables like 'server_id'"

相关内容