把网上下载的集群压缩包mysql-cluster-gpl-7.6.7-linux-glibc2.12-x86_64.tar.gz解压放在/usr/local/下

[root@localhost hbk]# cd /usr/local/
[root@localhost local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql-cluster-gpl-7.6.7-linux-glibc2.12-x86_64.tar.gz  sbin  share  src
[root@localhost local]# tar -zxvf mysql-cluster-gpl-7.6.7-linux-glibc2.12-x86_64.tar.gz 

解压后重命名成mysql

[root@localhost local]# mv mysql-cluster-gpl-7.6.7-linux-glibc2.12-x86_64 mysql
[root@localhost local]# cd mysql

新增mysql用户和组

[root@localhost mysql]# groupadd mysql
[root@localhost mysql]# useradd -g mysql -s /bin/false mysql

数据库安装初始化,安装后会随机生成一个密码,请记住此密码,后台登录的时候需要用到。

[root@localhost mysql]# ./bin/mysqld --initialize

会生成一个data目录

修改权限:

[root@localhost mysql]# chown -R root .
[root@localhost mysql]#  chgrp -R mysql .
[root@localhost mysql]# chown -R mysql:mysql data
[root@localhost mysql]# cp support-files/mysql.server /etc/rc.d/init.d/
[root@localhost mysql]# chmod +x /etc/rc.d/init.d/mysql.server
[root@localhost mysql]# chkconfig --add mysql.server

编辑/etc/my.cnf文件如下:

[root@localhost mysql]# vi /etc/my.cnf

[mysqld]
#datadir=/var/lib/mysql
#socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

ndbcluster
ndb-connectstring=192.168.254.102
[mysql_cluster]
ndb-connectstring=192.168.254.102

[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid

启动mysql

[root@localhost mysql]# /etc/init.d/mysql.server start
Starting MySQL.Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
..................................... SUCCESS! 

加入到环境变量,方便以后从任意目录可以执行mysql命令

[root@localhost mysql]# export PATH=$PATH:/usr/local/mysql/bin
[root@localhost mysql]# source /etc/profile

登录mysql,修改成自己的密码,自己操作,方便以后牢记密码。

[root@localhost mysql]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.23-ndb-7.6.7-cluster-gpl

Copyright (c) 2000, 2018, 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> set password = password('huangbaokang');
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

我把我的密码改成了huangbaokang
启动ndbd服务。

[root@localhost mysql]# ./bin/ndbd --initial
2018-09-12 22:14:57 [ndbd] INFO     -- Angel connected to '192.168.254.102:1186'
2018-09-12 22:14:57 [ndbd] INFO     -- Angel allocated nodeid: 3
2018-09-12 22:14:57 [ndbd] WARNING  -- Cannot change directory to '/var/lib/mysql-cluster', error: 2
2018-09-12 22:14:57 [ndbd] ERROR    -- Couldn't start as daemon, error: 'Failed to open logfile '/var/lib/mysql-cluster/ndb_3_out.log' for write, errno: 2'

以上是没有/var/lib/mysql-cluster目录导致,新建即可。

[root@localhost mysql]# mkdir /var/lib/mysql-cluster
[root@localhost mysql]# chown root:mysql /var/lib/mysql-cluster

再次执行,没有报错信息了

[root@localhost mysql]# ./bin/ndbd --initial
2018-09-12 22:18:50 [ndbd] INFO     -- Angel connected to '192.168.254.102:1186'
2018-09-12 22:18:50 [ndbd] INFO     -- Angel allocated nodeid: 3

一般搭建集群的时候,会弄两个数据节点。跟以上步骤一样,再搭建一个这样的环境,以上能够连接成功192.168.254.102,是因为我的管理节点已经搭建好。