2014年1月8日 星期三

[研究] 4主機 MySQL Cluster 7.3.3 架設(CentOS 6.5 x64)

[研究] 4主機 MySQL Cluster 7.3.3 架設(CentOS 6.5 x64)

2014-01-08

上次安裝測試的MySQL Cluster 7.2.x 似乎是修改自 MySQL 5.5,這次安裝測試的 MySQL Cluster 7.3.3 是修改自 MySQL 5.6,一些套件整併了,某些舊套件不存在了

參考文件
Chapter 17. MySQL Cluster NDB 7.3
http://dev.mysql.com/doc/refman/5.6/en/mysql-cluster.html
http://dev.mysql.com/doc/refman/5.6/en/mysql-cluster-install-linux-rpm.html

---------------------------------------------------------------------------

一、概念說明

MySQL Cluster(叢集,大陸稱為 "集群" 或 "群集") 架構大致如下,此為官方網站的圖片,網址
http://dev.mysql.com/doc/refman/5.5/en/images/cluster-components-1.png



NDB 是 Network DataBase (網路資料庫)

MySQL Cluster中主機分三類

1. 管理主機 : 服務程式(Deamon) 稱為 ndb_mgmd (NDB Management Daemon的意思)
              管理工具稱為 ndb_mgm (NDB Management 的意思),預設使用 Port 1186。
2. Data Node: 實際存放資料的主機 (Storage),服務程式稱為 ndbd (NDB Daemon的意思)。
3. SQL Node : 提供存取資料庫內容,服務程式稱為 mysqld (MySQL Deamon的意思)。

---------------------------------------------------------------------------

二、環境

Windows 2008 R2 + VMware Workstation 9.0.1 架設4台 VM,都安裝 CentOS 6.5 x64

centos1  eth0:192.168.128.101 (MGM Node)
centos2  eth0:192.168.128.102 (SQL Node)
centos2  eth0:192.168.128.103 (Data Node)
centos2  eth0:192.168.128.104 (Data Node)

CentOS 6.5 x64 在 600MB,無法用圖形模式安裝,也不會安裝 X Window
CentOS 6.5 x64 在 640MB,可用圖形模式安裝,會安裝 X Window
CentOS 6.5 x86 在 512MB,無法用圖形模式安裝,也不會安裝 X Window

VM 種類選 Red Hat Enterprise Linux 6 64-bit,記憶體設 640 MB
( 其實不管選 Red Hat Enterprise Linux 6 或 Red Hat Enterprise Linux 6 64-bit,VMware Workstation 9.0.1 建議的 RAM 是 2048 MB,但是小弟電腦總共只有 4GB )

---------------------------------------------------------------------------

三、套件下載

套件下載 (免費註冊登入後才能下載)
http://dev.mysql.com/downloads/cluster/#downloads

可以直要下載 MySQL-Cluster-gpl-7.3.3-1.el6.x86_64.rpm-bundle.tar,解開後就有所有需要的 rpm
或你也可以個別去下載 rpm

[root@localhost ~]# wget http://dev.mysql.com/get/Downloads/MySQL-Cluster-7.3/MySQL-Cluster-gpl-7.3.3-1.el6.x86_64.rpm-bundle.tar

[root@localhost ~]# tar xvf MySQL-Cluster-gpl-7.3.3-1.el6.x86_64.rpm-bundle.tar 
MySQL-Cluster-shared-gpl-7.3.3-1.el6.x86_64.rpm
MySQL-Cluster-test-gpl-7.3.3-1.el6.x86_64.rpm
MySQL-Cluster-devel-gpl-7.3.3-1.el6.x86_64.rpm
MySQL-Cluster-shared-compat-gpl-7.3.3-1.el6.x86_64.rpm
MySQL-Cluster-server-gpl-7.3.3-1.el6.x86_64.rpm
MySQL-Cluster-client-gpl-7.3.3-1.el6.x86_64.rpm
MySQL-Cluster-embedded-gpl-7.3.3-1.el6.x86_64.rpm
[root@localhost ~]#

因為要登入後才能下載,下載速度請別太低,一旦登入時間 time out,下載會自動中斷

小弟下載的環境限制每個下載 session 只有 500 Kbits/s,下載最大的檔案很容易發生下載被中斷的情況
MySQL-Cluster-server-gpl-7.3.3-1.el6.x86_64.rpm

下載後,可用 WinSCP 丟到 VM 中;或用 UltraISO 做成 .iso 檔案,掛載到 VMware 光碟機,然後用 mount 掛載到 Linux 上的 /media 目錄使用

---------------------------------------------------------------------------

四、安裝

為了省麻煩,防火牆先關閉,請把 SELinux 也關閉

要立刻關閉 SELinux (但 reboot 後仍會開啟)可執行 /usr/sbin/setenforce 0

要永久關閉,請修改 /etc/selinux/config
把 SELINUX=enforcing 改為 SELINUX=disabled
此修改不會立刻生效,要 reboot 才有效

CentOS 用 yum 安裝的 MySQL 是不能架設 Cluster 的,必須移除,
然後去 MySQL 官方網站註冊和下載 MySQL Cluster 使用的套件
(rpm 安裝和 tar.gz 安裝的很多路徑不同,如果使用非 rpm 請自己另外研究)

/usr/sbin/setenforce 0 sed -i -e "s@SELINUX=enforcing@#SELINUX=enforcing@" /etc/selinux/config
sed -i -e "s@SELINUX=permissive@#SELINUX=permissive@" /etc/selinux/config
sed -i -e "/SELINUX=/aSELINUX=disabled" /etc/selinux/config
service iptables stop
yum -y remove mysql*

各Node基本需要的套件如下

MGM Node (centos1)
rpm -Uhv MySQL-Cluster-server-gpl-7.3.3-1.el6.x86_64.rpm

SQL Node (centos2)
rpm -Uhv MySQL-Cluster-server-gpl-7.3.3-1.el6.x86_64.rpm
rpm -Uhv MySQL-Cluster-client-gpl-7.3.3-1.el6.x86_64.rpm

Data Node (centos3, centos4)
rpm -Uhv MySQL-Cluster-server-gpl-7.3.3-1.el6.x86_64.rpm

centos1 上安裝情況

[root@centos1 ~]# rpm -Uhv MySQL-Cluster-server-gpl-7.3.3-1.el6.x86_64.rpm
Preparing...                ########################################### [100%]
   1:MySQL-Cluster-server-gp########################################### [100%]

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

Please report any problems with the /usr/bin/mysqlbug script!

[root@localhost ~]#

---------------------------------------------------------------------------

五、設定

參考

17.2.4. Initial Configuration of MySQL Cluster
http://dev.mysql.com/doc/refman/5.6/en/mysql-cluster-install-configuration.html

17.3.2.1. MySQL Cluster Configuration: Basic Example
http://dev.mysql.com/doc/refman/5.6/en/mysql-cluster-config-example.html

(1) 設定 Data Nodes 和 SQL Nodes

centos2, centos3, centos4 主機上

vi /etc/my.cnf

內容

[mysqld]
# Options for mysqld process:
ndbcluster                      # run NDB storage engine

[mysql_cluster]
# Options for MySQL Cluster processes:
ndb-connectstring=192.168.128.101  # location of management server

(2) 設定 MGM Node

centos1 主機上

mkdir /var/lib/mysql-cluster
cd /var/lib/mysql-cluster
vi config.ini

內容

[ndbd default]
# Options affecting ndbd processes on all data nodes:
NoOfReplicas=2    # Number of replicas
DataMemory=80M    # How much memory to allocate for data storage
IndexMemory=18M   # How much memory to allocate for index storage
                  # For DataMemory and IndexMemory, we have used the
                  # default values. Since the "world" database takes up
                  # only about 500KB, this should be more than enough for
                  # this example Cluster setup.

[tcp default]
# TCP/IP options:
#portnumber=2202   # This the default; however, you can use any
                  # port that is free for all the hosts in the cluster
                  # Note: It is recommended that you do not specify the port
                  # number at all and simply allow the default value to be used
                  # instead

[ndb_mgmd]
# Management process options:
hostname=192.168.128.101           # Hostname or IP address of MGM node
datadir=/var/lib/mysql-cluster  # Directory for MGM node log files

[ndbd]
# Options for data node "A":
                                # (one [ndbd] section per data node)
hostname=192.168.128.103           # Hostname or IP address
datadir=/usr/local/mysql/data   # Directory for this data node's data files

[ndbd]
# Options for data node "B":
hostname=192.168.128.104           # Hostname or IP address
datadir=/usr/local/mysql/data   # Directory for this data node's data files

[mysqld]
# SQL node options:
hostname=192.168.128.102           # Hostname or IP address
                                # (additional mysqld connections can be
                                # specified for this node for various
# purposes such as running ndb_restore)

---------------------------------------------------------------------------

六、啟動

17.2.5. Initial Startup of MySQL Cluster
http://dev.mysql.com/doc/refman/5.6/en/mysql-cluster-install-first-start.html

啟動 MySQL Cluster 順序: 首先啟動管理節點服務器(ndb_mgmd),然後啟動存儲節點服務器(ndbd),最後才啟動SQL節點服務器(service mysql start)

(1) MGM 主機上

centos1 主機上

[root@localhost mysql-cluster]# ndb_mgmd -f /var/lib/mysql-cluster/config.ini  --initial
MySQL Cluster Management Server mysql-5.6.14 ndb-7.3.3
[root@localhost mysql-cluster]#

如果 config.ini 檔案有修改過,要加上 --initial 參數,否則可以不用加上。

(2) 啟動 Data Node (兩台都要做)

centos3 主機上

[root@centos3 ~]# mkdir -p  /usr/local/mysql/data/
[root@centos3 ~]# ndbd
2014-01-08 20:41:51 [ndbd] INFO     -- Angel connected to '192.168.128.101:1186'
2014-01-08 20:41:51 [ndbd] INFO     -- Angel allocated nodeid: 2
[root@centos3 ~]#

centos4 主機上

[root@centos4 ~]# mkdir -p  /usr/local/mysql/data/
[root@centos4 ~]# ndbd
2014-01-08 20:41:56 [ndbd] INFO     -- Angel connected to '192.168.128.101:1186'
2014-01-08 20:41:56 [ndbd] INFO     -- Angel allocated nodeid: 3
[root@centos4 ~]#


(3) 啟動 SQL Node

centos2 主機上

[root@centos2 ~]# service mysql start
Starting MySQL................................ SUCCESS!
[root@centos2 ~]#
#

(4) 檢查目前狀況

到 MGM 主機 (centos1) 上檢查目前狀況

[root@centos1 ~]# ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: localhost:1186
Cluster Configuration
---------------------
[ndbd(NDB)]     2 node(s)
id=2    @192.168.128.103  (mysql-5.6.14 ndb-7.3.3, Nodegroup: 0, *)
id=3    @192.168.128.104  (mysql-5.6.14 ndb-7.3.3, Nodegroup: 0)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @192.168.128.101  (mysql-5.6.14 ndb-7.3.3)

[mysqld(API)]   1 node(s)
id=4 (not connected, accepting connect from 192.168.128.102)

ndb_mgm>
[root@centos1 ~]#

按下 Ctrl-C 跳出



[root@centos1 ~]# ndb_mgm -e show
Connected to Management Server at: localhost:1186
Cluster Configuration
---------------------
[ndbd(NDB)]     2 node(s)
id=2    @192.168.128.103  (mysql-5.6.14 ndb-7.3.3, Nodegroup: 0, *)
id=3    @192.168.128.104  (mysql-5.6.14 ndb-7.3.3, Nodegroup: 0)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @192.168.128.101  (mysql-5.6.14 ndb-7.3.3)

[mysqld(API)]   1 node(s)
id=4 (not connected, accepting connect from 192.168.128.102)

[root@centos1 ~]#


停止 MySQL Cluster

執行 ndb_mgm -e shuwdown 會把所有 MGM Node 和所有 Data Node 的 ndb_mgmd 和 ndbd 都停止掉。

[root@centos1 ~]# ndb_mgm -e SHUTDOWN
Connected to Management Server at: localhost:1186
3 NDB Cluster node(s) have shutdown.
Disconnecting to allow management server to shutdown.
[root@centos1 ~]#


七.測試

資料庫的備份和還原請參考這篇

[研究] 2主機 MySQL Cluster 7.3.3-1 架設
http://shaurong.blogspot.tw/2013/02/2-mysql-cluster-7210-1.html

八、防火牆

centos1 上 (接受 centos2 的連線)

chkconfig iptables on
service iptables start
chkconfig  iptables on
iptables -A INPUT -s 192.168.128.102 -p tcp --dport 1186 -j ACCEPT
iptables-save


在 centos2 執行 (接受 centos1 的連線)

chkconfig iptables on
service iptables start
chkconfig  iptables on
iptables -A INPUT -s 192.168.128.101 -p tcp --dport 1186 -j ACCEPT
iptables-save

其中 iptables-save 命令會把防火牆規則存到 /etc/sysconfig/iptables

---------------------------------------------------------------------------

附錄1:設定主機名稱

在 Server A 上執行 uname -n 或 hostname 可查詢目前的主機名稱是甚麼
[root@server1 ~]# uname -n
server1

[root@server1 ~]# hostname
server1
如果想設定主機名稱為mgm,方法如下
[root@server1 ~]# hostname  mgm

另外 vim /etc/hosts,增加
192.168.128.101 mgm
如果想要 hostname 在 reboot 後設定仍有效,必須修改 /etc/sysconfig/network 的內容

(完)

相關

[研究] 4主機 MySQL Cluster 7.3.3 架設(CentOS 6.5 x64)
http://shaurong.blogspot.com/2014/01/4-mysql-cluster-7.html

[研究] MySQL 5.6.15 安裝(rpm)(CentOS 6.5 x64)
http://shaurong.blogspot.com/2014/01/mysql-5615-rpmcentos-65-x64.html

[研究] MySQL Community 5.6.15 版安裝(yum) (CentOS 6.5 x64)
http://shaurong.blogspot.com/2014/01/mysql-community-5615-tgz-centos-65-x64.html

[研究] 4主機 MySQL Cluster 7.2.10.1 架設
http://shaurong.blogspot.tw/2013/02/4-mysql-cluster-7210-1.html

[研究] 2主機 MySQL Cluster 7.2.10-1 架設
http://shaurong.blogspot.com/2013/02/2-mysql-cluster-7210-1.html

[研究] 4主機 MySQL Cluster 7.0.9 架設
http://forum.icst.org.tw/phpbb/viewtopic.php?f=10&t=17903

[研究] 雙主機 MySQL Cluster 7.0.9架設
http://forum.icst.org.tw/phpbb/viewtopic.php?f=10&t=17904

[研究] MariaDB 5.5 安裝(yum)(CentOS 6.3 x64)
http://shaurong.blogspot.com/2013/02/mariadb-55-yum-centos-63-x64.html

[研究] CentOS 5.4 x86 上 MySQL 資料庫同步(Replication)
http://forum.icst.org.tw/phpbb/viewtopic.php?t=17824

[研究] mariadb-5.1.42 (tar.gz)(瑪利亞資料庫, MySQL) 安裝 (CentOS 5.4)
http://forum.icst.org.tw/phpbb/viewtopic.php?t=17988

[研究] mariadb-5.2.4-Linux-i686.tar.gz 安裝 (CentOS 5.5 x86)
http://forum.icst.org.tw/phpbb/viewtopic.php?t=19434

[研究] mysql-5.5.14.tar.gz 安裝(失敗)(CentOS 6.0 x86)
http://forum.icst.org.tw/phpbb/viewtopic.php?t=20252

[研究] mysql-5.5.8.tar.gz 安裝(CentOS 5.5 x86)
http://forum.icst.org.tw/phpbb/viewtopic.php?t=19214

[研究] mysql-5.5.8.tar.gz 安裝(Fedora 14 x86)
http://forum.icst.org.tw/phpbb/viewtopic.php?t=19215

[研究] 每天自動備份 MySQL 方法
http://shaurong.blogspot.com/2014/01/mysql.html
http://forum.icst.org.tw/phpbb/viewtopic.php?t=14972

[研究] 雙主機 MySQL Cluster 7.0.9 架設
http://forum.icst.org.tw/phpbb/viewtopic.php?t=17904


8 則留言:

  1. 您好,請問照著這篇完成以後
    回去測試,在MGM主機上卻會說-bash: mysql: command not found
    是否還要在安裝mysql client呢

    回覆刪除
    回覆
    1. 您可以試試,請問您用的 CentOS 版本?MySQL Cluster 版本?

      刪除
    2. CentOs6.8
      MySQL Cluster 版本如版主7.3.3-1

      刪除
  2. 浮雲大您好,先前的問題克服了
    不過在實做的時候
    建立db的時候另外一台sql也能同步
    但是建立table的時候另外一台卻無法同步

    想請問浮雲大有遇過類似狀況,或是知道有何解嗎

    回覆刪除
  3. [研究] 4主機 MySQL Cluster 7.5.9 架設(CentOS 7.4 x64)
    http://shaurong.blogspot.tw/2018/02/4-mysql-cluster-759-centos-74-x64.html

    回覆刪除
    回覆
    1. 好的!感謝浮雲大,我會再研究看看

      刪除
  4. 感謝浮雲大,之後問題都排除!

    回覆刪除