Home > Database > Mysql Tutorial > body text

Haproxy+mysqlcluster(MySQL集群)配置_MySQL

WBOY
Release: 2016-06-01 13:01:12
Original
1195 people have browsed it

一、准备

1、准备服务器

建立有2个节点的MySQL CLuster体系,使用6台服务器建立Haproxy+mysql cluster( MySQL 集群) 体系

节点配置说明
节点 对应的IP和端口
Haproxy负载均衡(1个) centos 6.3 1.1.1.11
管理节点(1个) centos 6.3 1.1.1.30
SQL节点 (2个) centos 6.3 1.1.1.21
1.1.1.22
数据节点 (2个) centos 6.3 1.1.1.31
1.1.1.32

二、Haproxy负载均衡安装配置

[root@HAPROXY001 ~]# yum install haproxy*

在/etc/haproxy/目录下创建haproxy.cfg配置文件

--------------------------------------------------------------------------------

[root@HAPROXY001 ~]# vim /etc/haproxy/haproxy.cfg

 

# this config needs haproxy-1.1.28 or haproxy-1.2.1

global

log 127.0.0.1 local0 info #日志相关

log 127.0.0.1 local1 notice

maxconn 4096

chroot /usr/local/haproxy

uid root

gid root

daemon

#debug

#quiet

pidfile /usr/local/haproxy/haproxy.pid

defaults

log global

mode http

#option httplog

option dontlognull

retries 3

option redispatch

maxconn 8000

contimeout 3000

clitimeout 30000

srvtimeout 30000

 

listen mysql

bind 0.0.0.0:3306 #代理端口

mode tcp #模式 TCP

option mysql-check #user root #mysql健康检查 root为mysql登录用户名

balance roundrobin #调度算法

server mysql1 1.1.1.21:3306 check port 3306#weight 1 check inter 1s rise 2 fall 2 #健康检查加上check

server mysql2 1.1.1.22:3306 check port 3306#weight 1 check inter 1s rise 2 fall 2

 

listen stats #监控

mode http

bind 0.0.0.0:8888

stats enable

stats uri /dbs

stats realm Global\ statistics

stats auth admin:hello123

--------------------------------------------------------------------------------

启动服务:/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg

 

登录浏览器查看:  输入admin hello123登录
Copy after login

wKioL1R8RuOwLCEWAAYmE9A0L_4625.jpg

 

三、mysql cluster安装

1,mysql cluster 各节点安装,以管理节点为例

[root@MYSQLMANAGER ~]# wget http://cdn.mysql.com/Downloads/MySQL-Cluster-7.3/mysql-cluster-gpl-7.3.7-linux-glibc2.5-x86_64.tar.gz

[root@MYSQLMANAGER ~]# groupadd mysql

[root@MYSQLMANAGER ~]# useradd mysql -g mysql

[root@MYSQLMANAGER ~]# tar -xvf mysql-cluster-gpl-7.3.7-linux-glibc2.5-x86_64.tar.gz

[root@MYSQLMANAGER ~]# mv mysql-cluster-gpl-7.3.7-linux-glibc2.5-x86_64 /usr/local/mysql

[root@MYSQLMANAGER ~]# cd /usr/local/

[root@MYSQLMANAGER ~]# chown -R mysql:mysql mysql/

[root@MYSQLMANAGER ~]# cd mysql/

[root@MYSQLMANAGER ~]# scripts/mysql_install_db --user=mysql

 

2,节点配置

管理节点:

[root@MYSQLMANAGER ~]# vi /var/lib/mysql-cluster/config.ini

--------------------------------------------------------------------------------

[NDBD DEFAULT]

NoOfReplicas=2 #副本数量,建议使用默认的2

MaxNoOfConcurrentOperations=10000

DataMemory=1024M #每个数据节点中给数据分配的内存

IndexMemory=500M #每个数据节点中给索引分配的内存

BackupMemory: 50M

[NDB_MGMD]

#管理节点配置项

Nodeid= 1

HostName=1.1.1.30 #管理节点IP

DataDir=/usr/local/mysql/data #管理节点日志、配置目录

ArbitrationRank: 1 #该节点的优先级别等级

[NDBD]

#数据节点配置项

Nodeid = 2 #第一个数据节点

HostName=1.1.1.31 #数据节点IP

DataDir=/usr/local/mysql/data #数据节点存放数据的目录

[NDBD]

Nodeid = 3 #第二个数据节点

HostName=1.1.1.32

DataDir=/usr/local/mysql/data

[mysqld]

#SQL节点配置项

Nodeid = 4 #第一个SQL节点

HostName=1.1.1.21

[mysqld]

Nodeid = 5 #第二个SQL节点

HostName=1.1.1.22

[mysqld]

Nodeid = 6 #第三个SQL节点

HostName=1.1.1.254

[mysqld]

#建议保留一个SQL节点配置口

--------------------------------------------------------------------------------

 

数据节点: 两数据节点配置一样

 

[root@MYSQLDB001 ~]# vim /etc/my.cnf

--------------------------------------------------------------------------------

[mysqld]

datadir=/usr/local/mysql/data

socket=/usr/local//mysql/mysql.sock

user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

#运行NDB存储引擎

ndbcluster

#指定管理节点

ndb-connectstring=1.1.1.30:1186

 

[MYSQL_CLUSTER]

ndb-connectstring=1.1.1.30:1186

[NDB_MGM]

connect-string=1.1.1.30

 

[mysqld_safe]

log-error=/usr/local/mysql/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

--------------------------------------------------------------------------------


SQL节点配置:

 

 

[root@MYSQL001 ~]# cd /usr/local/mysql/

[root@MYSQL001 ~]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld

[root@MYSQL001 ~]# chmod +x /etc/rc.d/init.d/mysqld

[root@MYSQL001 ~]# chkconfig --add mysqld

[root@MYSQL001 ~]# vi /etc/my.cnf

--------------------------------------------------------------------------------

 

[mysqld]

server-id=4

#每个服务器的id不一样

datadir=/usr/local/mysql/data

socket=/var/lib/mysql/mysql.sock

user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

log-bin = /usr/local/mysql/log/mysql-bin.log

max_connections=1000

skip-name-resolve

 

#以下为mysql 主主模式的配置文件

# 忽略mysql数据库复制

binlog-ignore-db=mysql

# 每次增长2

auto-increment-increment = 2

# 设置自动增长的字段的偏移量,即初始值为2

auto-increment-offset = 1

 

[mysqld_safe]

log-error=/usr/local/mysql/log/mysqld.log

pid-file=/usr/local/mysql/mysqld.pid

 

[MYSQLD]

ndbcluster

ndb-connectstring=1.1.1.30

[MYSQL_CLUSTER]

ndb-connectstring=1.1.1.30

[NDB_MGM]

connect-string=1.1.1.30

3,启动服务

CLUSTER启动的顺序依次为:管理节点—数据节点—SQL节点 关闭顺序相反

 

管理节点启动:

[root@MYSQLMANAGER ~]# /usr/local/mysql/bin/ndb_mgmd -f /var/lib/mysql-cluster/config.ini

添加新节点后,重新加载管理节点配置信息:

 

[root@MYSQLMANAGER ~]# /usr/local/mysql/bin/ndb_mgmd -f /etc/config.ini --initial

[root@MYSQLMANAGER ~]# /usr/local/mysql/bin/ndb_mgmd -f /etc/config.ini --reload

 

数据节点启动:

[root@MYSQLDB001 ~]# /usr/local/mysql/bin/ndbd --initial

注意:仅限第一次mysql节点启动使用–-initial,后面再启动不需要此参数

 

SQL节点启动:

[root@MYSQL001 ~]# service mysqld start

 

四、mysql cluster 测试

1,查看cluster 信息

[root@MYSQLMANAGER ~]#ndb_mgm -e show

wKiom1R9M53gIIoEAAFvaL4okmM280.jpg

2,测试SQL数据同步

从SQL节点1登录,创建数据库和表,进行简单测试。

[root@MYSQL001 ~]# mysql -uroot -p

mysql> create database test ;

mysql> use test;
Database changed

mysql> create table test1(id int,name varchar(10)) engine=ndb ;

mysql> insert into test1 values(1,'test');

mysql> select * from test1 ;

+------+---------+
| id | name |
+------+---------+
| 1 | test |
+------+---------+

从SQL节点2登录,查看效果,库,表和数据已经同步。

从SQL节点2节点插入一条数据,同样登陆SQL节点1,也能看到数据已经同步

3,测试HA服务

停止SQL节点2服务查看

[root@MYSQL002 ~]# /etc/init.d/mysqld stop

Shutting down MySQL.... [ OK ]

wKioL1R9OILyn6L2AAXs559Vej8749.jpgwKiom1R9OEfyn9AzAAF8FYaGkuk795.jpg

mysql 服务 1.1.1.11:3306 依然可以正常使用。

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!