环境:

1、系统: centos 7

2、部署的机器及IP

管理节点1:192.168.1.79  
管理节点2:192.168.1.80
数据节点1:192.168.1.81
数据节点2:192.168.1.82
MySQL节点1:192.168.1.83
MySQL节点2:192.168.1.84
haproxy+keepalived 192.168.1.79
haproxy+keepalived 192.168.1.80
vip ip 192.168.7.222/21

一、linux 系统限制配置

1、关闭系统防火墙  

systemctl stop firewalld.service 关闭防火墙
systemctl disable firewalld.service  禁用防火墙

2、关闭SElinux

sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
setenforce 0 selinux 立即生效

二、系统安装约定

源码包存放路径:/usr/local/src
mysql-cluster安装目录 /apps/program/mysqlCluster
mysql-cluster数据目录 /apps/program/mysqlCluster/ndbdata
haproxy+keepalived 安装目录/usr/local/软件名字

三、下在源码安装包

1、下载mysql-cluster-gpl-7.5.4

wget -P /usr/local/src http://cdn.mysql.com//Downloads/MySQL-Cluster-7.5/mysql-cluster-gpl-7.5.4.tar.gz

2、下载boost_1_59_0

mkdir -p /usr/local/boost
wget -P /usr/local/boost https://nchc.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz

3、下载haproxy-1.7.0

 wget -P /usr/local/src http://www.haproxy.org/download/1.7/src/haproxy-1.7.0.tar.gz

4、下载keepalived-1.3.2

wget -P /usr/local/src http://www.keepalived.org/software/keepalived-1.3.2.tar.gz

四、安装编译环境及编译依赖

yum -y install epel-release
卸载旧版mysql或mariadb
yum -y remove mariadb* mysql*
安装编译环境及依赖
yum -y install  make git gcc-c++  bison-devel ncurses-devel perl \
perl-devel ncurses-devel bison cmake java-1.8.0-openjdk-devel \
java-1.8.0-openjdk 
安装keepalived 依赖 (192.168.1.79 192.168.1.80)
yum -y install libnl-devel openssl-devel libnfnetlink-devel ipvsadm \
popt-devel libnfnetlink kernel-devel popt-static iptraf autoconf automake
安装MySQL健康检查支持工具 (192.168.1.83 192.168.1.84)
yum -y install xinetd

五、编译安装 mysql-cluster-gpl-7.5.4

1、创建MySQL安装目录

mkdir -p /apps/program/mysqlCluster/{bin,ndbdata,tmp,etc}

2、创建MySQL 账号

useradd mysql -s /sbin/nologin -M

3、MySQL目录mysql 账号能读写

chown -R mysql:mysql /apps/program/mysqlCluster

4、编译mysql-cluster-gpl-7.5.4

a、解压mysql-cluster-gpl-7.5.4.tar.gz
tar -zvf tar -zxvf /usr/local/src/mysql-cluster-gpl-7.5.4.tar.gz -C /usr/local/src/
cd /usr/local/src/mysql-cluster-gpl-7.5.4
b、编译mysql-cluster
cmake -DCMAKE_INSTALL_PREFIX=/apps/program/mysqlCluster \
-DMYSQL_UNIX_ADDR=/apps/program/mysqlCluster/tmp/mysql-cluster.sock \
-DMYSQL_DATADIR=/apps/program/mysqlCluster/ndbdata \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS:STRING=utf8,gbk \
-DEXTRA_CHARSETS=all \
-DWITH_EMBEDDED_SERVER=0 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_NDBCLUSTER_STORAGE_ENGINE=1 \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_USER=mysql \
-DWITH_DEBUG=0 \
-DWITH_NDB_JAVA=OFF\
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/boost \
-DENABLE_DOWNLOADS=1

make -j4 && make install 

c、配置MySQL环境变量
echo export  PATH=$PATH:/apps/program/mysqlCluster/bin >>/etc/profile
. /etc/profile

六、创建mysql-cluster 配置文件

1、创建mysql-cluster管理节点配置文件config.ini (192.168.1.79 192.168.1.80)

vim /apps/program/mysqlCluster/etc/config.ini
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA

# config.ini -- MySQL Cluster configuration file (options case-insensitive)
[ndb_mgmd default]
datadir=/apps/program/mysqlCluster/ndbdata
[ndbd default]
NoOfReplicas = 2
DataMemory = 4096M
IndexMemory = 200M
FragmentLogFileSize=256M
TimeBetweenLocalCheckpoints=6
MaxNoOfConcurrentTransactions=50000
MaxNoOfConcurrentOperations=100000
datadir=/apps/program/mysqlCluster/ndbdata

[ndb_mgmd]
NodeId = 1
HostName=192.168.1.79
datadir=/apps/program/mysqlCluster/ndbdata

[ndb_mgmd]
NodeId = 2
HostName=192.168.1.80
datadir=/apps/program/mysqlCluster/ndbdata

[ndbd]
NodeId = 11
HostName=192.168.1.81
datadir=/apps/program/mysqlCluster/ndbdata
MaxBufferedEpochs=100
TimeBetweenEpochsTimeout=4000

[ndbd]
NodeId=12
HostName=192.168.1.82
datadir=/apps/program/mysqlCluster/ndbdata
MaxBufferedEpochs=100
TimeBetweenEpochsTimeout=4000

[mysqld]
NodeId = 21
HostName=192.168.1.83

[mysqld]
NodeId=22
HostName=192.168.1.84
[mysqld]
[mysqld]
[mysqld]

2、创建mysql-cluster 数据节点配置文件my.cnf (192.168.1.81 192.168.1.82)

vim /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 
user=mysql
max_allowed_packet=1024M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
ndbcluster
ndb-connectstring=192.168.1.79,192.168.1.80
[mysql_cluster]
ndb-connectstring=192.168.1.79,192.168.1.80

3、创建mysql-cluster MySQL节点配置文件my.cnf (192.168.1.83 192.168.1.84)

vim /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 
user=mysql
max_allowed_packet=1024M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
basedir =/apps/program/mysqlCluster
datadir=/apps/program/mysqlCluster/ndbdata
socket=/apps/program/mysqlCluster/tmp/mysql-cluster.sock
port=3306
pid-file=/apps/program/mysqlCluster/tmp/mysql-cluster.pid
log-error=/apps/program/mysqlCluster/tmp/mysql-cluster.log
character_set_server=utf8
ndbcluster
ndb-connectstring = 192.168.1.79,192.168.1.80
[mysql_cluster]
ndb-connectstring = 192.168.1.79,192.168.1.80
[NDB_MGM]
connect-string=192.168.1.79,192.168.1.80
[client]
port=3306
socket=/apps/program/mysqlCluster/tmp/mysql-cluster.sock
default-character-set = utf8

七、初始化mysql-cluster集群

1、初始化mysql-cluster管理节点 (192.168.1.79)

ndb_mgmd -f /apps/program/mysqlCluster/etc/config.ini --configdir=/apps/program/mysqlCluster --initial

2、初始化mysql-cluster管理节点 (192.168.1.80)

ndb_mgmd -c 192.168.1.79 --ndb-nodeid=2 --configdir=/apps/program/mysqlCluster
注意:ndb-nodeid 与config.ini配置文件ID要一致

3、初始化mysql-cluster 数据节点 (192.168.1.81 192.168.1.82)

ndbd -–initial

4、初始化mysql-cluster MySQL节点(192.168.1.83 192.168.1.84)

拷贝MySQL启动脚本到/etc/init.d
cp -rf /apps/program/mysqlCluster/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
初始化数据库
rm -rf /apps/program/mysqlCluster/ndbdata/*
/apps/program/mysqlCluster/bin/mysqld --collation-server=utf8_general_ci --character-set-server=utf8   \
--user=mysql --basedir=/apps/program/mysqlCluster --datadir=/apps/program/mysqlCluster/ndbdata \
--secure-file-priv=/apps/program/mysqlCluster/test/ --initialize

记录MySQL初始化密码
日志最后一行
2017-01-03T07:57:06.920754Z 1 [Note] A temporary password is generated for root@localhost: SpOZkZg266<P  (192.168.1.83)
2017-01-03T07:57:06.920754Z 1 [Note] A temporary password is generated for root@localhost: :lry?>gkq7&Y  (192.168.1.84)
启动数据MySQL
service mysqld start
修改MySQL密码
192.168.1.83 84一样操作
mysql -u root -p
输入SpOZkZg266<P
mysql> SET PASSWORD = PASSWORD('123456');  FLUSH PRIVILEGES;
将外部访问root密码设置123456
mysql> GRANT ALL PRIVILEGES ON *.* TO'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;  FLUSH PRIVILEGES;
创建MySQL健康检查账号 192.168.1.83 192.168.1.84都要创建
GRANT PROCESS ON *.* TO 'mysqlcheckuser'@'localhost' IDENTIFIED BY 'mysqlcheckpassword!';FLUSH PRIVILEGES;
chkconfig mysqld on        #开机启动
service mysql start        #启动
service mysql stop         #停止
service mysql restart      #重启

5、查看集群状态:任意节点输入:ndb_mgm 

[root@179 ~]# ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: localhost:1186
Cluster Configuration
---------------------
[ndbd(NDB)]     2 node(s)
id=11   @192.168.1.81  (mysql-5.7.16 ndb-7.5.4, Nodegroup: 0)
id=12   @192.168.1.82  (mysql-5.7.16 ndb-7.5.4, Nodegroup: 0, *)

[ndb_mgmd(MGM)] 2 node(s)
id=1    @192.168.1.79  (mysql-5.7.16 ndb-7.5.4)
id=2    @192.168.1.80  (mysql-5.7.16 ndb-7.5.4)

[mysqld(API)]   5 node(s)
id=21 (not connected, accepting connect from 192.168.1.83)
id=22 (not connected, accepting connect from 192.168.1.84)
id=23   @192.168.1.83  (mysql-5.7.16 ndb-7.5.4)
id=24   @192.168.1.84  (mysql-5.7.16 ndb-7.5.4)
id=25 (not connected, accepting connect from any host)
[root@180 ~]# ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: localhost:1186
Cluster Configuration
---------------------
[ndbd(NDB)]     2 node(s)
id=11   @192.168.1.81  (mysql-5.7.16 ndb-7.5.4, Nodegroup: 0)
id=12   @192.168.1.82  (mysql-5.7.16 ndb-7.5.4, Nodegroup: 0, *)

[ndb_mgmd(MGM)] 2 node(s)
id=1    @192.168.1.79  (mysql-5.7.16 ndb-7.5.4)
id=2    @192.168.1.80  (mysql-5.7.16 ndb-7.5.4)

[mysqld(API)]   5 node(s)
id=21 (not connected, accepting connect from 192.168.1.83)
id=22 (not connected, accepting connect from 192.168.1.84)
id=23   @192.168.1.83  (mysql-5.7.16 ndb-7.5.4)
id=24   @192.168.1.84  (mysql-5.7.16 ndb-7.5.4)
id=25 (not connected, accepting connect from any host)

6、测试mysql-cluster 数据同步 

192.168.1.83
[root@183 ~]# mysql -u root -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 430
Server version: 5.7.16-ndb-7.5.4 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> create database test;
Query OK, 1 row affected (0.07 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| ndbinfo            |
| performance_schema |            
| sys                |
| test               |
+--------------------+
7 rows in set (0.00 sec)

mysql> 
192.168.1.84
[root@184 ~]# mysql -u root -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 52787
Server version: 5.7.16-ndb-7.5.4 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| ndbinfo            |
| performance_schema |           
| sys                |
| test               |
+--------------------+
7 rows in set (0.01 sec)

创建test表 一定要ndbcluster 引擎
192.168.1.83
[root@183 ~]# mysql -u root -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 565
Server version: 5.7.16-ndb-7.5.4 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> use test;
Database changed
mysql> CREATE TABLE `test` ( `id`  int(8) NULL , `test`  varchar(255) NULL  ) ENGINE=ndbcluster;
Query OK, 0 rows affected (0.21 sec)

mysql> show tables ;
+----------------+
| Tables_in_test |
+----------------+
| test           |
+----------------+
1 row in set (0.00 sec)

192.168.1.84
[root@184 ~]# mysql -u root -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 52956
Server version: 5.7.16-ndb-7.5.4 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>  show tables ;
+----------------+
| Tables_in_test |
+----------------+
| test           |
+----------------+
1 row in set (0.01 sec)

mysql>

7、创建MySQL 健康检查脚本haproxy使用

vim /bin/clustercheck
#!/bin/bash 
#
# Script to make a proxy (ie HAProxy) capable of monitoring mysql Cluster nodes properly
#
# Authors:
#
# Grant privileges required:
# GRANT PROCESS ON *.* TO 'mysqlcheckuser'@'localhost' IDENTIFIED BY 'mysqlcheckpassword!';FLUSH PRIVILEGES ;

MYSQL_USERNAME="${1-mysqlcheckuser}"
MYSQL_PASSWORD="${2-mysqlcheckpassword!}"
AVAILABLE_WHEN_DONOR=${3:-0}
ERR_FILE="${4:-/dev/null}"
# Perform the query to check the ping
#
MYSQLCHECK=($(/apps/program/mysqlCluster/bin/mysqladmin -u $MYSQL_USERNAME -p$MYSQL_PASSWORD ping  \
    2>${ERR_FILE} |  grep -w "alive"  \
    |  awk '{print $3}'))

        if [[ "${MYSQLCHECK}" == "alive" ]];then

    # mysql Cluster node local state is 'runing' => return HTTP 200
    # Shell return-code is 0
    echo -en "HTTP/1.1 200 OK\r\n" 
    echo -en "Content-Type: text/plain\r\n" 
    echo -en "Connection: close\r\n" 
    echo -en "Content-Length: 40\r\n" 
    echo -en "\r\n" 
    echo -en "MYSQL Cluster Node is Runing.\r\n" 
    sleep 0.1
    exit 0
else
    # mysql Cluster node local state is not 'runing' => return HTTP 503
    # Shell return-code is 1
    echo -en "HTTP/1.1 503 Service Unavailable\r\n" 
    echo -en "Content-Type: text/plain\r\n" 
    echo -en "Connection: close\r\n" 
    echo -en "Content-Length: 57\r\n" 
    echo -en "\r\n" 
    echo -en "MYSQL Cluster  is not  Runing. \r\n" 
    sleep 0.1
    exit 1
fi
#是/bin/clustercheck可执行
chmod +x /bin/clustercheck
vim /etc/services
添加
mysqlchk 9200/tcp # mysqlchk
vim /etc/xinetd.d/mysqlchk
# default: on 
# description: mysqlchk 
service mysqlchk
{
# this is a config for xinetd, place it in /etc/xinetd.d/
        disable = no
        flags           = REUSE
        socket_type     = stream
        type            = UNLISTED
        port            = 9200
        wait            = no
        user            = root
        server          = /usr/bin/clustercheck
        log_on_failure  += USERID
        only_from       = 0.0.0.0/0
        per_source      = UNLIMITED
}

测试健康检查

[root@184 /]# /bin/clustercheck
HTTP/1.1 200 OK
Content-Type: text/plain
Connection: close
Content-Length: 40

MYSQL Cluster Node is Runing.
[root@184 /]# 
#启动xinetd
systemctl start  xinetd.service
#xinetd.service 加入开机启动
systemctl enable xinetd.service
#查看9200端口是否打开
[root@184 /]# netstat -anlp | grep 9200
tcp6       0      0 :::9200      :::*           LISTEN      244559/xinetd
telnet 测试端口返回是否正常
[root@184 /]# telnet 192.168.1.84 9200 
Trying 192.168.1.84...
Connected to 192.168.1.84.
Escape character is '^]'.
HTTP/1.1 200 OK
Content-Type: text/plain
Connection: close
Content-Length: 40

MYSQL Cluster Node is Runing.
Connection closed by foreign host.

8、创建mysql-cluster 管理节点启动脚本

192.168.1.79
vim /etc/init.d/ndb_mgmd
#! /bin/bash
# chkconfig: - 85 15
# description: ndb_mgmd is a World Wide ndb_mgmd server. It is used to serve
PROGDIR=/apps/program/mysqlCluster
PROGNAME=ndb_mgmd
DAEMON=/apps/program/mysqlCluster/bin/$PROGNAME
CONFIG=/apps/program/mysqlCluster/etc/config.ini
PIDFILE=/apps/program/mysqlCluster/ndbdata/ndb_1.pid
DESC="ndb_mgmd daemon"
test -x $DAEMON || exit 0
case "$1" in
start)

  if [ $(pidof $PROGNAME | wc -l) -gt 0 ]
  then
  echo "$PROGNAME is running..."
   else
   echo -n "Starting $DESC: $PROGNAME"
     $DAEMON -f $CONFIG --configdir=$PROGDIR
  echo "."
  fi
        ;;
stop)
   if [ $(pidof $PROGNAME | wc -l) -le 0 ]
   then
   echo "$PROGNAME is not running..."
else
   echo -n "Stopping $DESC: $PROGNAME"
    pidof $PROGNAME | xargs kill
    echo "."
fi
      ;;
reload)
if [ $(pidof $PROGNAME | wc -l) -gt 0 ]
then
    pidof $PROGNAME | xargs kill
fi
echo -n "reloading $DESC: $PROGNAME"
rm -rf /apps/program/mysqlCluster/ndbdata/ndb_1_config.bin.1
$DAEMON -f $CONFIG --configdir=$PROGDIR --initial
;;
restart)
 ${0}  stop
 ${0}  start
    ;;
*)
 echo "Usage: /etc/init.d/$PROGNAME {start|stop|restart|reload}" >&2
 exit 1
 ;;
esac
exit 0
192.168.1.80
vim /etc/init.d/ndb_mgmd
#! /bin/bash
# chkconfig: - 85 15
# description: ndb_mgmd is a World Wide ndb_mgmd server. It is used to serve
PROGDIR=/apps/program/mysqlCluster
PROGNAME=ndb_mgmd
DAEMON=/apps/program/mysqlCluster/bin/$PROGNAME
CONFIG=/apps/program/mysqlCluster/etc/config.ini
PIDFILE=/apps/program/mysqlCluster/ndbdata/ndb_2.pid
DESC="ndb_mgmd daemon"
test -x $DAEMON || exit 0
case "$1" in
start)

  if [ $(pidof $PROGNAME | wc -l) -gt 0 ]
  then
  echo "$PROGNAME is running..."
   else
   echo -n "Starting $DESC: $PROGNAME"
 $DAEMON -c 192.168.1.79 --ndb-nodeid=2 --configdir=$PROGDIR
  echo "."
  fi
        ;;
stop)
    if [ $(pidof $PROGNAME | wc -l) -le 0 ]
   then
   echo "$PROGNAME is not running..."
else
   echo -n "Stopping $DESC: $PROGNAME"
    pidof $PROGNAME | xargs kill
    echo "."
fi
      ;;
reload)
echo -n "reloading $DESC: $PROGNAME"
if [ $(pidof $PROGNAME | wc -l) -gt 0 ]
then
    pidof $PROGNAME | xargs kill
    
fi
rm -rf /apps/program/mysqlCluster/ndb_2_config.bin.1
 $DAEMON -c 192.168.1.79 --ndb-nodeid=2 --configdir=$PROGDIR
;;
restart)
  ${0} stop
  ${0} start
    ;;
*)
 echo "Usage: /etc/init.d/$PROGNAME {start|stop|restart|reload}" >&2
 exit 1
 ;;
esac
exit 0
chmod +x  /etc/init.d/ndb_mgmd # 可执行权限
service ndb_mgmd start   #启动
service ndb_mgmd stop    #关闭
service ndb_mgmd restart #重启
service ndb_mgmd reload  #初始化及刷新配置
chkconfig ndb_mgmd on        #开机启动

9、创建mysql-cluster 数据节点启动脚本

192.168.1.81 192.168.1.82
vim /etc/init.d/ndbddata
#! /bin/bash
# chkconfig: - 85 15
# description: ndbd is a World Wide ndbd server. It is used to serve
PROGDIR=/apps/program/mysqlCluster
PROGNAME=ndbd
DAEMON=/apps/program/mysqlCluster/bin/$PROGNAME
PIDFILE=/apps/program/mysqlCluster/ndbdata/ndb_11.pid
DESC="ndbd daemon"
test -x $DAEMON || exit 0
case "$1" in
start)

  if [ $(pidof $PROGNAME | wc -l) -gt 0 ]
  then
  echo "$PROGNAME is running..."
   else
   echo -n "Starting $DESC: $PROGNAME"
     $DAEMON
  echo "."
  fi
        ;;
stop)

   if [ $(pidof $PROGNAME | wc -l) -le 0 ]
   then
   echo "$PROGNAME is not running..."
else
   echo -n "Stopping $DESC: $PROGNAME"
    pidof $PROGNAME | xargs kill
    echo "."
fi
  ;;
reload)
if [ $(pidof $PROGNAME | wc -l) -gt 0 ]
then
 pidof $PROGNAME | xargs kill
 fi
 echo -n "reloading $DESC: $PROGNAME"
$DAEMON --initial
;;
restart)
  ${0} stop
  ${0} start
    ;;
*)
 echo "Usage: /etc/init.d/$PROGNAME {start|stop|restart|reload}" >&2
 exit 1
 ;;
esac
exit 0
chmod +x  /etc/init.d/ndbddata # 可执行权限
service ndbddata start   #启动
service ndbddata stop    #关闭
service ndbddata restart #重启
service ndbddata reload  #初始化数据
chkconfig ndbddata on        #开机启动

八、编译安装编译安装 haproxy (192.168.1.79,192.168.1.80)

安装 haproxy 日志记录支持
yum -y install rsyslog
解压 haproxy-1.7.0.tar.gz 
tar -xvf /usr/local/src/haproxy-1.7.0.tar.gz -C /usr/local/src/
cd /usr/local/src/haproxy-1.7.0
make TARGET=linux31 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
创建文件夹haproxy
mkdir -pv /usr/local/haproxy/{conf,run,log}

创建haproxy 启动脚本
vim /etc/init.d/haproxy
#! /bin/bash
# chkconfig: - 85 15
# description: haproxy is a World Wide Web server. It is used to serve
PROGDIR=/usr/local/haproxy
PROGNAME=haproxy
DAEMON=$PROGDIR/sbin/$PROGNAME
CONFIG=$PROGDIR/conf/$PROGNAME.conf
PIDFILE=$PROGDIR/run/$PROGNAME.pid
DESC="HAProxy daemon"
SCRIPTNAME=$PROGDIR/init.d/$PROGNAME
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
start()
 {
   if [ $(pidof haproxy | wc -l) -gt 0 ]
   then
  echo "$PROGNAME is Runing"
   else
    echo -n "Starting $DESC: $PROGNAME"
   $DAEMON -f $CONFIG
   echo "."
fi
        }
stop()
 {

   if [ $(pidof haproxy | wc -l) -le 0 ]
   then
  echo "$PROGNAME  is not running."
   else
  echo -n "Stopping $DESC: $PROGNAME"
    cat $PIDFILE | xargs kill
    echo "."
fi
}

reload()
 { echo -n "reloading $DESC: $PROGNAME"
   $DAEMON -f $CONFIG -p $PIDFILE -sf $(cat $PIDFILE)
}
case "$1" in
  start)
  start
  ;;
 stop)
  stop
   ;;
restart)
        stop
        start
        ;;
reload)
 reload
   ;;
*)
 echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
 exit 1
 ;;
esac
exit 0
chmod +x  /etc/init.d/haproxy # 可执行权限

创建haproxy.conf

vim /usr/local/haproxy/conf/haproxy.conf
global
                log 127.0.0.1   local0
                log 127.0.0.1  local1 notice
                #log loghost    local0 info
                maxconn 50000
                chroot  /usr/local/haproxy
                uid 99
                gid 99

                daemon
                nbproc 2
                pidfile  /usr/local/haproxy/run/haproxy.pid
                #debug
                #quiet

 defaults
                log     global
                mode    tcp
                option  tcplog
                option  dontlognull
                option  forwardfor
                option  redispatch
                retries 3
                timeout connect     3000
                timeout client      50000
                timeout server 50000




frontend admin_stat
         bind *:8888
         mode http
         default_backend stats-back

frontend cluster-front
         bind    *:3306
         mode    tcp
         default_backend mysql-cluster

backend mysql-cluster
        mode    tcp
        balance roundrobin
        option httpchk
        server  mysql1 192.168.1.83:3306 check port 9200 inter 12000 rise 3 fall 3
        server  mysql2 192.168.1.84:3306 check port 9200 inter 12000 rise 3 fall 3

backend stats-back
        mode http
        balance roundrobin
        stats uri /admin?stats
        stats auth admin:admin
        stats realm Haproxy\ Statistics
Haproxy rsyslog 日志配置
vim /etc/rsyslog.conf 
#$ModLoad imudp      修改 $ModLoad imudp 
#$UDPServerRun 514  修改 $UDPServerRun 514
#添加 Haproxy  日志记录路径
local0.* /usr/local/haproxy/log/haproxy.log
重启rsyslog
systemctl restart  rsyslog.service
启动Haproxy 
service haproxy start
加入开机启动
chkconfig haproxy on
查看haproxy  是否启动
ps -ef | grep haproxy
[root@179 ~]# ps -ef | grep haproxy 
nobody     90266       1  0 1月01 ?       00:00:32 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.conf
nobody     90267       1  0 1月01 ?       00:00:32 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.conf 
haproxy 访问端口8888 账号密码 admin:admin
http://192.168.1.79:8888/admin?stats
http://192.168.1.80:8888/admin?stats

九、编译安装keepalived

tar -xvf /usr/local/src/keepalived-1.3.2.tar.gz -C /usr/local/src
cd /usr/local/src/keepalived-1.3.2
./configure --prefix=/usr/local/keepalived
vmware 虚拟机安装 修改./lib/config.h
HAVE_DECL_RTA_ENCAP 1 修改为HAVE_DECL_RTA_ENCAP 0 如果不修改编译会报错
make && make install
拷贝keepalived 可执行文件到/usr/sbin/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
拷贝启动脚本
cp -rf /usr/local/src/keepalived-1.3.2/keepalived/keepalived.service /usr/lib/systemd/system/keepalived.service

systemctl start  keepalived.service #启动
systemctl restart  keepalived.service #重启
systemctl stop keepalived.service  #关闭
systemctl enable keepalived.service # 开机启动

配置keepalived

cp /usr/local/keepalived/etc/sysconfig/keepalived  /etc/sysconfig/
创建/etc/keepalived 文件夹
mkdir -p /etc/keepalived

创建/etc/keepalived/keepalived.conf (192.168.1.79)

vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
        admin@test.com    #发生故障时,接受信息的email地址
   }
   notification_email_from admin@test.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_haproxy {                           #自定义的监控脚本
    script "/etc/keepalived/check_haproxy.sh"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state BACKUP                                         #让VIP不抢回
    nopreempt
    interface eth0                                       #监听接口
    virtual_router_id 51
    priority 100                                            #优先级,backup机器上的优先级要小与这个值
    advert_int 1                                          #检查间隔
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {                                        #自定义的监控脚本
        chk_haproxy
    }
    virtual_ipaddress {                              #VIP地址,可以设多个
        192.168.7.222/21
    }
    notify_backup "/etc/init.d/haproxy restart"
    notify_fault "/etc/init.d/haproxy stop"
}

创建/etc/keepalived/keepalived.conf (192.168.1.80)

vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
        admin@qkagame.com    #发生故障时,接受信息的email地址
   }
   notification_email_from admin@qkagame.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_haproxy {                           #自定义的监控脚本
    script "/etc/keepalived/check_haproxy.sh"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state BACKUP                                         #让VIP不抢回
    nopreempt
    interface eth0                                       #监听接口
    virtual_router_id 51
    priority 99                                            #优先级,backup机器上的优先级要小与这个值
    advert_int 1                                          #检查间隔
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {                                        #自定义的监控脚本
        chk_haproxy
    }
    virtual_ipaddress {                              #VIP地址,可以设多个
        192.168.7.222/21
    }
    notify_backup "/etc/init.d/haproxy restart"
    notify_fault "/etc/init.d/haproxy stop"
}

创建/etc/keepalived/chk_haproxy.sh

vim /etc/keepalived/chk_haproxy.sh
 
#!/bin/bash
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ];then
    /etc/init.d/haproxy start
fi
sleep 2
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then
    /etc/init.d/keepalived stop
fi
chmod +x /etc/keepalived/chk_haproxy.sh #可执行权限

测试keepalived 

192.168.1.79 
[root@179 ~]# systemctl start  keepalived.service 
[root@179 ~]# ps -ef | grep keepalived
root       83672       1  0  2016 ?        00:00:13 /usr/sbin/keepalived -D
root       83673   83672  0  2016 ?        00:00:16 /usr/sbin/keepalived -D
root       83674   83672  0  2016 ?        00:01:58 /usr/sbin/keepalived -D
root       94902   94862  0 12:45 pts/0    00:00:00 grep --color=auto keepalived
[root@179 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:d1:e4:49 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.79/21 brd 192.168.7.255 scope global dynamic eth0
       valid_lft 45839sec preferred_lft 45839sec
    inet 192.168.7.222/21 scope global secondary eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fed1:e449/64 scope link 
       valid_lft forever preferred_lft forever
192.168.1.80
[root@180 ~]# systemctl start  keepalived.service
[root@180 ~]#  ps -ef | grep keepalived
root       86190       1  0  2016 ?        00:00:13 /usr/sbin/keepalived -D
root       86191   86190  0  2016 ?        00:00:15 /usr/sbin/keepalived -D
root       86192   86190  0  2016 ?        00:00:48 /usr/sbin/keepalived -D
root       95740   95701  0 12:46 pts/0    00:00:00 grep --color=auto keepalived
[root@180 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:35:5a:f0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.80/21 brd 192.168.7.255 scope global dynamic eth0
       valid_lft 45803sec preferred_lft 45803sec
    inet6 fe80::20c:29ff:fe35:5af0/64 scope link 
       valid_lft forever preferred_lft forever
关闭 192.168.1.79 keepalived
root@179 ~]# systemctl stop  keepalived.service     
[root@179 ~]# ps -ef | grep keepalived
root       94947   94907  0 12:47 pts/0    00:00:00 grep --color=auto keepalived
[root@179 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:d1:e4:49 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.79/21 brd 192.168.7.255 scope global dynamic eth0
       valid_lft 45726sec preferred_lft 45726sec
    inet6 fe80::20c:29ff:fed1:e449/64 scope link 
       valid_lft forever preferred_lft forever
[root@180 ~]#  ps -ef | grep keepalived
root       86190       1  0  2016 ?        00:00:13 /usr/sbin/keepalived -D
root       86191   86190  0  2016 ?        00:00:15 /usr/sbin/keepalived -D
root       86192   86190  0  2016 ?        00:00:48 /usr/sbin/keepalived -D
root       95777   95747  0 12:48 pts/0    00:00:00 grep --color=auto keepalived
[root@180 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:35:5a:f0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.80/21 brd 192.168.7.255 scope global dynamic eth0
       valid_lft 45712sec preferred_lft 45712sec
    inet 192.168.7.222/21 scope global secondary eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe35:5af0/64 scope link 
       valid_lft forever preferred_lft forever
测试连接MySQL
[root@180 ~]# mysql -u root -p123456 -h192.168.7.222
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 54284
Server version: 5.7.16-ndb-7.5.4 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql>