Mysql集群搭建
1. 简介
文主要介绍在ubuntu系统搭建mysql数据库集群,负载均衡设置,以及集群的应用测试。
准备工作:
l ubuntu14.04.3虚拟机3台。
l mysql集群安装包mysql-cluster-gpl-7.4.11-debian7-x86_64.deb,下载地址http://cdn.mysql.com//Downloads/MySQL-Cluster-7.4/mysql-cluster-gpl-7.4.11-debian7-x86_64.deb
2. Mysql集群安装
2.1. 虚拟机配置
设置虚拟机网卡连接方式为NAT,安装必要程序包,完成之后,可设置其为HOST-ONLY让其可互相访问,并且无需外网连接。
安装必要程序包:
sudo apt-get install ssh
sudo apt-get install python-paramiko
sudo apt-get install libaio-dev
sudo apt-get install mysql-client
创建集群OS用户:adduser cluster
配置host:
192.168.70.128 mysqlmgm.hscf.com
192.168.70.131 mysqldata1.hscf.com
192.168.70.132 mysqldata2.hscf.com
2.2. 安装集群包
安装mysql集群包:
sudo dpkg -i mysql-cluster-gpl-7.4.11-debian7-x86_64.deb
.deb文件安装Mysql集群到/opt/mysql/server-5.6/目录下。由于我们需要使用cluster用户从通过ssh从管理节点访问其他节点,因此我们修改次目录的所有者sudo chown -R cluster:cluster /opt/mysql
2.3. 配置集群
2.3.1. 通过web界面进行配置
1. /opt/mysql/server-5.6/bin下,运行 ./ndb_setup.py
2. 在“Cluster Type and SSH Credentials”界面进行设置
Cluster Name: myclusterHSCF
HostLIst: mysqlmgm.hscf.com, mysqldata1.hscf.com, mysqldata2.hscf.com
Application Area: simple testing
writeload: medium
Keybased SSH (no)
username: cluster
password:******
3. 修改“mysql cluster install directory”: /opt/mysql/server-5.6/
4. 配置“Define Processes and Cluster Topology”
5. 在界面“Define Processes Parameters”检查(了解)一下参数
6. 部署和启动
2.4. 集群启动,停止脚本
启动集群:
1. 在管理服务器启动管理器:
/opt/mysql/server-5.6/bin/ndb_mgmd --ndb-nodeid=49 --config-dir=/home/cluster/MySQL_Cluster/49/ --config-file=/home/cluster/MySQL_Cluster/49/config.ini
| |
我们对比可以看到,实际上是比web上启动少了—initial这个参数
2. 分别在Data节点服务器上,启动数据节点,运行:
/opt/mysql/server-5.6/bin/ndbmtd --ndb-nodeid=1 --ndb-connectstring=192.168.70.128:1186
/opt/mysql/server-5.6/bin/ndbmtd --ndb-nodeid=2 --ndb-connectstring=192.168.70.128:1186
| |
3. 分别在Sql节点服务器上,启动Sql节点,运行:
/opt/mysql/server-5.6/bin/mysqld --defaults-file=/home/cluster/MySQL_Cluster/53/my.cnf &
/opt/mysql/server-5.6/bin/mysqld --defaults-file=/home/cluster/MySQL_Cluster/54/my.cnf &
| |
至此,整个集群已启动。
停止集群:
1.
mysqladmin -h127.0.0.1 -uroot shutdown
| |
分别在SQL节点停止SQL服务:
2. 在管理服务器上,运行命令,停掉DB和管理:
/opt/mysql/server-5.6/bin/ndb_mgm -e shutdown;
| |
3. 负载均衡搭建
3.1. haproxy搭建
由于Mysql Cluster集群本身没有提供负载均衡, 所以必须使用第3方组件来实现, 本节介绍基于 Haproxy的一个实现
1. 使用命令在管理服务器上直接安装(当前默认版本:1.4.24)
2. Haproxy配置:vi /etc/haproxy/haproxy.cfg
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
user haproxy
group haproxy
daemon
defaults
log global
mode tcp
option redispatch
option abortonclose
balance roundrobin
| |
contimeout 5000
clitimeout 50000
srvtimeout 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
listen proxy
bind 192.168.70.128:3306
mode tcp
option mysql-check user root
server db1 192.168.70.131:3306 weight 1 check inter 1s rise 3 fall 2
server db2 192.168.70.132:3306 weight 1 check inter 1s rise 3 fall 2
listen haproxy_stats
mode http
bind 192.168.70.128:8888
option httplog
stats refresh 5s
stats uri /status
stats realm Haproxy Manager
stats auth admin:handhand
| |
3. 重启haproxy服务
3. 根据配置,打开haproxy监控页面 http://192.168.70.128:8888/status
进行查看,用户名/密码:admin/handhand
至此,基本的haproxy就已经搭建好了
4. 集群测试
4.1.负载均衡测试
(1)在sql1 和sql2节点上,分别创建一张相同的表 ctest(a int), engine不是ndb的,即,非集群表
使用 java往表里写数据,如下代码:
package mysqlclusterTest;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class ConnTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
java.sql.Connection conn = null; // 数据库连接
Statement stmt = null; // 数据库表达式
ResultSet rs = null; // 结果集
try {
/*加载驱动*/
Class.forName("com.mysql.jdbc.Driver");
/*连接到数据库*/
for(int i =0; i<1000; i++){
conn = DriverManager.getConnection(
"jdbc:mysql://172.16.233.183:3306/tmp?", "ubuntu", "");
/* 获取表达式*/
stmt = (Statement) conn.createStatement();
/* 插入数据*/
stmt.executeUpdate("insert into ctest (a) values (" + i + ")");
| |
/* 执行SQL*/
rs = stmt.executeQuery("select count(1) cnt from ctest");
/* 查看里面的数据*/
while (rs.next()) {
System.out.println("count:" + rs.getString("cnt"));
//System.out.println("年龄=" + rs.getString("age"));
}
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
结果: 在sql1和sql2节点分别查看, 两张表分别插入了500条记录,一个为单,一个为双,(我们采用轮询的方式)
(2)我们把 sql2点停止, 继续使用上面java写入数据,结果:仍然可以正常工作,只是全部写入了sql1节点的表里了。
| |
4.2. ndbcluster同步测试
使用mysql-workbench连接到管理服务器192.168.70.128:3306上,创建表
CREATE TABLE clustertest2.City (
ID INT(11) NOT NULL
)
ENGINE = NDBCLUSTER
会发现在数据库192.168.70.131:3306, 192.168.70.132:3306同时也创建了了表City,并且当插入数据到任意数据库时,其他数据库也能同时查看到相应数据
| |