设为首页 收藏本站
查看: 2446|回复: 0

[经验分享] mysql-cluster-gpl-7.5.4双管理节点 部署及使用haproxy+keepalived做...

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-1-4 15:03:59 | 显示全部楼层 |阅读模式
mysql-cluster-gpl-7.5.4双管理节点 部署及使用haproxy+keepalived做负载均衡
环境:

1、系统: centos 7
2、部署的机器及IP

1
2
3
4
5
6
7
8
9
管理节点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、关闭系统防火墙  

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



2、关闭SElinux
1
2
sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
setenforce 0 selinux 立即生效



二、系统安装约定
1
2
3
4
源码包存放路径:/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
1
wget -P /usr/local/src http://cdn.mysql.com//Downloads/ ... er-gpl-7.5.4.tar.gz



2、下载boost_1_59_0
1
2
mkdir -p /usr/local/boost
wget -P /usr/local/boost https://nchc.dl.sourceforge.net/ ... boost_1_59_0.tar.gz



3、下载haproxy-1.7.0
1
wget -P /usr/local/src http://www.haproxy.org/download/1.7/src/haproxy-1.7.0.tar.gz



4、下载keepalived-1.3.2
1
wget -P /usr/local/src http://www.keepalived.org/software/keepalived-1.3.2.tar.gz



四、安装编译环境及编译依赖
1
2
3
4
5
6
7
8
9
10
11
12
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安装目录
1
mkdir -p /apps/program/mysqlCluster/{bin,ndbdata,tmp,etc}



2、创建MySQL 账号
1
useradd mysql -s /sbin/nologin -M



3、MySQL目录mysql 账号能读写
1
chown -R mysql:mysql /apps/program/mysqlCluster



4、编译mysql-cluster-gpl-7.5.4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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)
1
vim /apps/program/mysqlCluster/etc/config.ini



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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)
1
vim /etc/my.cnf



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/ ... ation-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)
1
vim /etc/my.cnf



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/ ... ation-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)
1
ndb_mgmd -f /apps/program/mysqlCluster/etc/config.ini --configdir=/apps/program/mysqlCluster --initial



2、初始化mysql-cluster管理节点 (192.168.1.80)
1
2
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)
1
ndbd -–initial



4、初始化mysql-cluster MySQL节点(192.168.1.83 192.168.1.84)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
拷贝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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[iyunv@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)



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[iyunv@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 数据同步
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
192.168.1.83
[iyunv@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
[iyunv@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
[iyunv@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
[iyunv@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使用

1
vim /bin/clustercheck



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/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



1
2
#是/bin/clustercheck可执行
chmod +x /bin/clustercheck



1
2
3
vim /etc/services
添加
mysqlchk 9200/tcp # mysqlchk



1
vim /etc/xinetd.d/mysqlchk



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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
}



测试健康检查
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[iyunv@184 /]# /bin/clustercheck
HTTP/1.1 200 OK
Content-Type: text/plain
Connection: close
Content-Length: 40

MYSQL Cluster Node is Runing.
[iyunv@184 /]#
#启动xinetd
systemctl start  xinetd.service
#xinetd.service 加入开机启动
systemctl enable xinetd.service
#查看9200端口是否打开
[iyunv@184 /]# netstat -anlp | grep 9200
tcp6       0      0 :::9200      :::*           LISTEN      244559/xinetd
telnet 测试端口返回是否正常
[iyunv@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 管理节点启动脚本
1
192.168.1.79



1
vim /etc/init.d/ndb_mgmd



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#! /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



1
192.168.1.80



1
vim /etc/init.d/ndb_mgmd



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#! /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



1
2
3
4
5
6
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 数据节点启动脚本
1
192.168.1.81 192.168.1.82



1
vim /etc/init.d/ndbddata



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#! /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



1
2
3
4
5
6
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)
1
2
3
4
5
6
7
8
9
安装 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}




1
2
创建haproxy 启动脚本
vim /etc/init.d/haproxy




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#! /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



1
chmod +x  /etc/init.d/haproxy # 可执行权限



创建haproxy.conf
1
vim /usr/local/haproxy/conf/haproxy.conf



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
[iyunv@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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
1
2
3
cp /usr/local/keepalived/etc/sysconfig/keepalived  /etc/sysconfig/
创建/etc/keepalived 文件夹
mkdir -p /etc/keepalived



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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
! 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)
1
vim /etc/keepalived/keepalived.conf



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
! 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
1
2
3
4
5
6
7
8
9
10
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



1
chmod +x /etc/keepalived/chk_haproxy.sh #可执行权限



测试keepalived
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
192.168.1.79
[iyunv@179 ~]# systemctl start  keepalived.service
[iyunv@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
[iyunv@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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
192.168.1.80
[iyunv@180 ~]# systemctl start  keepalived.service
[iyunv@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
[iyunv@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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
关闭 192.168.1.79 keepalived
root@179 ~]# systemctl stop  keepalived.service     
[iyunv@179 ~]# ps -ef | grep keepalived
root       94947   94907  0 12:47 pts/0    00:00:00 grep --color=auto keepalived
[iyunv@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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[iyunv@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
[iyunv@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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
测试连接MySQL
[iyunv@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>







运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-323839-1-1.html 上篇帖子: mysql 内存优化之关闭numa 下篇帖子: mariadb 二进制启动报错,Can't find messagefile '/usr/local/mysql/shar...
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表