本文共 7890 字,大约阅读时间需要 26 分钟。
1、服务器准备
1) MySQL节点1 10.41.1.852) MySQL节点2 10.41.1.843) ndb节点1 10.41.1.834) ndb节点2 10.41.1.825) 管理节点 10.41.1.812、测试环境5台服务器均一样,不是必须的,所以服务器均已关闭iptables和selinux,生产环境请根据实际情况自行决定systemctl stop firewalld #关闭防火墙systemctl disable firewalld #永久关闭防火墙setenforce 0 #临时关闭selinuxsed -i 's#SELINUX=enforcing#SELINUX=disable#g' /etc/selinux/config #永久关闭selinuxgetenforce #查看当前状态3、软件准备mysql-cluster-gpl-7.2.4-linux2.6-x86_64.tar.gz4、管理节点安装配置建立目录:mkdir /usr/local/mysql/bin -pmkdir /data/mysql-cluster -p准备文件cp ./mysql-cluster-gpl-7.2.4-linux2.6-x86_64/bin/ndb_mgm /usr/local/mysql/bin/cp ./mysql-cluster-gpl-7.2.4-linux2.6-x86_64/bin/ndb_mgmd /usr/local/mysql/bin/环境变量echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profilesource /etc/profile配置cd /data/mysql-cluster/touch config.inicat /data/mysql-cluster/config.ini[NDBD DEFAULT]NoOfReplicas=1 DataMemory=64M IndexMemory=16M#管理节点[NDB_MGMD]nodeid=1hostname=10.41.1.81 datadir=/data/mysql-cluster#第一个 ndbd 节点:[NDBD]nodeid=2hostname=10.41.1.83 datadir=/data/mysql-cluster/data#第二个 ndbd 节点:[NDBD]nodeid=3hostname=10.41.1.82datadir=/data/mysql-cluster/data[MySQLD]
nodeid=4hostname=10.41.1.85 [MySQLD]nodeid=5hostname=10.41.1.84[MySQLD]6、NDB节点安装(数据节点)
数据节点1: 10.41.1.83数据节点2: 10.41.1.82建立用户useradd mysql解压mysql clustertar zxf mysql-cluster-gpl-7.2.4-linux2.6-x86_64.tar.gzmv mysql-cluster-gpl-7.2.4-linux2.6-x86_64/ /usr/local/mysql/授权cd /usr/local/mysqlchown -R mysql .chgrp -R mysql .建立数据目录,并授权(用来在没使用NDB时存放的数据)mkdir /data/mysql -pchown -R mysql.mysql /data/mysql/初始化/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql/ --basedir=/usr/local/mysql授权rootchown -R root .配置\cp support-files/my-large.cnf /etc/my.cnfcp support-files/mysql.server /etc/init.d/mysqldchmod 755 /etc/init.d/mysqld环境变量echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profilesource /etc/profile建立目录(存放NDB节点的数据)mkdir /data/mysql-cluster/data -p修改/etc/my.cnf文件cat /etc/my.cnf[mysqld]datadir=/data/mysqlbasedir= /usr/local/mysqlndbcluster ndb-connectstring=10.41.1.81 [MYSQL_CLUSTER]ndb-connectstring=10.41.1.81 说明:如果希望尽可能的各环境保持一致,建议在NDB节点也和SQL节点一样安装整个带有 NDB Cluster 存储引擎的MySQL Server。(NDB节点可以不用初始化数据,自己已经测试,但是我依然会初始化)安装细节和上面的SQL节点完全一样。7、安装 MySQL 节点sql节点1: 10.41.1.85sql节点2: 10.41.1.84建立用户useradd mysql解压mysql clustertar zxf mysql-cluster-gpl-7.2.4-linux2.6-x86_64.tar.gzmv mysql-cluster-gpl-7.2.4-linux2.6-x86_64/ /usr/local/mysql/授权cd /usr/local/mysqlchown -R mysql .chgrp -R mysql .建立数据目录,并授权(用来在没使用NDB时存放的数据)mkdir /data/mysql -pchown -R mysql.mysql /data/mysql/初始化/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql/ --basedir=/usr/local/mysql授权rootchown -R root .配置\cp support-files/my-large.cnf /etc/my.cnfcp support-files/mysql.server /etc/init.d/mysqldchmod 755 /etc/init.d/mysqld环境变量echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profilesource /etc/profile建立目录(存放NDB节点的数据)mkdir /data/mysql-cluster/data -p修改/etc/my.cnf文件cat /etc/my.cnf[mysqld]datadir=/data/mysqlbasedir= /usr/local/mysqlndbcluster ndb-connectstring=10.41.1.81 [MYSQL_CLUSTER]ndb-connectstring=10.41.1.81[ndbd(NDB)] 2 node(s)
id=2 @10.41.1.83 (mysql-5.5.19 ndb-7.2.4, Nodegroup: 0, Master)id=3 @10.41.1.82 (mysql-5.5.19 ndb-7.2.4, Nodegroup: 1)[ndb_mgmd(MGM)] 1 node(s)
id=1 @10.41.1.81 (mysql-5.5.19 ndb-7.2.4)[mysqld(API)] 3 node(s)
id=4 @10.41.1.85 (mysql-5.5.19 ndb-7.2.4)id=5 @10.41.1.84 (mysql-5.5.19 ndb-7.2.4)id=6 (not connected, accepting connect from any host)10、测试1-NDB存储引擎测试(1)在任意一个SQL节点(我这里选择10.41.1.85)的test_cluster库中创建测试表test_table01(不指定引擎)和test_table02(NDB存储引擎),设置存储引擎为NDB,并插入两条测试数据:mysql> create table test_table01( id int, name varchar(20) ) engine=ndb;mysql> create table test_table02( id int, name varchar(20) ) ;mysql> show tables;+------------------------+Tables_in_test_cluster+------------------------+test_table01test_table02+------------------------+2 rows in set (0.01 sec)另一sql节点:mysql> show tables;+------------------------+Tables_in_test_cluster+------------------------+test_table02+------------------------+1 row in set (0.02 sec)说明只有ndb引擎才同步。(2)在test_table02中插入两条测试数据mysql> insert into t1 select 1,'yayun';Query OK, 1 row affected (0.11 sec)Records: 1 Duplicates: 0 Warnings: 0mysql> insert into t1 select 1,'atlas';
Query OK, 1 row affected (0.03 sec)Records: 1 Duplicates: 0 Warnings: 0两个sql节点都能查到数据。mysql> select * from test_table02;+------+-----------+id | name+------+-----------+1 | cluster012 | cluster02+------+-----------+2 rows in set (0.00 sec)显然,两个SQL节点查询的数据是一致。(3)在SQL节点10.41.1.85上把测试表test_table02引擎改为MyISAM,再次插入测试数据:mysql> alter table test_table02 engine=myisam;Query OK, 2 rows affected (0.90 sec)Records: 2 Duplicates: 0 Warnings: 0mysql> insert into test_table02 value(3,'cluster03');#10.41.1.84ERROR 1412 (HY000): Table definition has changed, please retry transaction插入数据直接报错。11、单点故障测试
管理节点,不需要特殊的配置,只需要将管理工具和配置文件放置多台服务器上即可。SQL节点10.41.1.84上的MySQL服务停止管理节点:[mysqld(API)] 3 node(s)id=4 @10.41.1.85 (mysql-5.5.19 ndb-7.2.4)id=5 (not connected, accepting connect from 10.41.1.84)id=6 (not connected, accepting connect from any host)10.41.1.85:mysql> insert into test_table02 value(4,'cluster04');Query OK, 1 row affected (0.00 sec)mysql> select from test_table02;+------+-----------+id | name+------+-----------+2 | cluster023 | cluster034 | cluster041 | cluster01+------+-----------+4 rows in set (0.00 sec)10.41.1.85服务开启:mysql> select from test_table02;+------+-----------+id | name+------+-----------+3 | cluster034 | cluster041 | cluster012 | cluster02+------+-----------+4 rows in set (0.01 sec)数据又同步过来了NDB(数据节点)
数据节点10.41.1.82上的NDB进程停止[root@node3 mysql]# ps -ef | grep ndbdroot 15969 1 0 14:37 ? 00:00:04 ndbd --initialroot 15970 15969 1 14:37 ? 00:00:47 ndbd --initialroot 16029 15801 0 15:36 pts/3 00:00:00 grep --color=auto ndbdkillall ndbd任意sql节点查询:mysql> select from test_table02;ERROR 1296 (HY000): Got error 157 'Unknown error code' from NDBCLUSTER无法查询10.41.1.81:grep 'NoOfReplicas' /data/mysql-cluster/config.iniNoOfReplicas=1 #每个数据节点的镜像数量将配置文件中的NoOfReplicas改为2,按照前面的步骤重新启动集群,无法启动,NoOfReplicas参数无法临时更改,我们开始就需要设置好,不要到后面才想到更改,那时就悲剧了。如果重新ndbd --initial,将会丢失所有数据。NoOfReplicas=2:管理节点需要--initial参数启动ndb_mgmd --initial -f /data/mysql-cluster/config.ini数据节点(NDB)启动还要用ndbd --initial,数据丢失mysql> select from test_table02;ERROR 1146 (42S02): Table 'test_cluster.test_table02' doesn't existmysql> create table test(-> id int,-> name varchar(20)-> ) engine=ndb;Query OK, 0 rows affected (0.85 sec)mysql> show tables;
+------------------------+Tables_in_test_cluster+------------------------+test+------------------------+1 row in set (0.01 sec)mysql> insert into test value(1,'one');
Query OK, 1 row affected (0.00 sec)mysql> insert into test value(2,'two');
Query OK, 1 row affected (0.00 sec)mysql> select * from test;
+------+------+id | name+------+------+1 | one2 | two+------+------+2 rows in set (0.00 sec)再次停掉数据节点10.41.1.82:两节点都可以正常数据查询12、MySQL Cluster集群的关闭关闭顺序:SQL节点->数据节点->管理节点(在MySQL Cluster环境中,NDB节点和管理节点的关闭都可以在管理节点的管理程序中完成,也可以分节点关闭,但是SQL节点却没办法。所以,在关闭整个MySQL Cluster环境或者关闭某个SQL节点的时候,首先必须到SQL节点主机上来关闭SQL节点程序。关闭方法和MySQL Server的关闭一样。)(1)SQL节点关闭/etc/init.d/mysqld stop(2)(NDB)数据节点关闭ndbd stop(3)管理节点关闭ndb_mgm> shutdown或者命令行:ndb_mgm -e shutdown总结:参数NoOfReplicas无法临时更改。selinux,iptables等相关问题。注:参考大量网络数据。转载于:https://blog.51cto.com/6300167/2347057