JavaScript must be enabled in order for you to see "WP Copy Data Protect" effect. However, it seems JavaScript is either disabled or not supported by your browser. To see full result of "WP Copy Data Protector", enable JavaScript by changing your browser options, then try again.

實作MySQL Cluster on CentOS 6.2 x64


假設你已經嘗試過Replication與Proxy都遇到瓶頸時,Cluster這個HA架構也是一個不錯的辦法,整體架構如上圖所示,分為Management、Data與SQL Node三種,而Management主要負責控管各個Node之間的狀態(Judgement),當某個Node出問題的時候可自動切換,且若是重要的Node出狀況時會自動停掉服務來避免資料遺失或損壞,Data則為存資料的功能,是比照Master/Slave的架構,而最後SQL是提供標準MySQL協定的前端,以提供服務用,這邊實作是以三台為例,所以廢話不多說,直接上實作吧,如下:

1) 前提已經準備好三台CentOS 6.2 x64的Server,且每台扮演腳色的規劃如下:
管理節點(ndb_mgmd):192.168.73.178
數據節點(ndbd):192.168.73.179與192.168.73.180
SQL節點(mysql):192.168.73.179與192.168.73.180

◎這邊數據與SQL節點也可分做四台不同的Server來分散負載

2) 關閉postfix並移除,因欲移除的mysql-libs可能與MySQL Cluster有衝突,也是Postfix的相依套件(三台都做)
#service postfix stop
Shutting down postfix: [ OK ]
#yum -y erase postfix mysql-libs

3) 卸載MySQL相關套件並刪除相關檔案並更新(三台都做)
#rpm -qa | grep 'mysql'
#rpm -e mysql*
#rm -rf /etc/my.cnf
#rm -rf /var/lib/mysql
#yum -y update

4) 關閉服務與啟動時不啟動防火牆(三台都做)
#service ip6tables stop
ip6tables: Flushing firewall rules: [ OK ]
ip6tables: Setting chains to policy ACCEPT: filter [ OK ]
ip6tables: Unloading modules: [ OK ]
#service iptables stop
iptables: Flushing firewall rules: [ OK ]
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Unloading modules: [ OK ]
#chkconfig --level 2345 iptables off
#chkconfig --level 2345 ip6tables off

5) 關閉SELinux,避免影影響到實作時的Cluster功能(檔案內設定啟動時,指令設定當前系統,三台都做)
#vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
#sestatus
#setenforce 0
SELinux status: enabled
SELinuxfs mount: /selinux
Current mode: permissive
Mode from config file: disabled
Policy version: 24
Policy from config file: targeted

6) 安裝並配置管理節點(ndb_mgmd:192.168.73.178)
#wget ftp://ftp.ntu.edu.tw/pub/MySQL/Downloads/MySQL-Cluster-7.2/mysql-cluster-gpl-7.2.4-linux2.6-x86_64.tar.gz
#tar zxvf mysql-cluster-gpl-7.2.4-linux2.6-x86_64.tar.gz
#mkdir -p /usr/local/mysql/bin
#mkdir -p /usr/local/mysql/ndbdata
#vi ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin
#cd mysql-cluster-gpl-7.2.4-linux2.6-x86_64
#cp bin/ndb_mgmd /usr/local/mysql/bin
#cp bin/ndb_mgm /usr/local/mysql/bin
#mkdir /var/lib/mysql-cluster
#mkdir /usr/local/mysql/cluster-conf
#vi /usr/local/mysql/cluster-conf/config.ini
# ref:mysql-cluster-gpl-7.2.4-linux2.6-x86_64/mysql-test/suite/ndb/ndb_config_config.ini
[ndbd default]
NoOfReplicas=2 #Number of Replication
DataMemory=80M #資料庫存儲可以分配的記憶體
IndexMemory=18M #索取存儲可以分配的記憶體
[ndb_mgmd]
id=1
hostname=192.168.73.178
DataDir=/var/lib/mysql-cluster
[ndbd]
id=11
HostName=192.168.73.179
DataDir=/usr/local/mysql/ndbdata
[ndbd]
id=12
HostName=192.168.73.180
DataDir=/usr/local/mysql/ndbdata
[mysqld]
id=21
HostName=192.168.73.179
[mysqld]
id=22
HostName=192.168.73.180
[mysqld]
id=23

7) 安裝SQL節點(mysql:192.168.73.179與192.168.73.180)
#scp mysql-cluster-gpl-7.2.4-linux2.6-x86_64.tar.gz 192.168.73.179:~
#ssh 192.168.73.{179,180}
#tar zxvf ./mysql-cluster-gpl-7.2.4-linux2.6-x86_64.tar.gz
#mv mysql-cluster-gpl-7.2.4-linux2.6-x86_64 /usr/local/mysql
#groupadd mysql
#useradd -g mysql mysql
#chown -R mysql.mysql /usr/local/mysql/
#cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf
#cd /usr/local/mysql
#scripts/mysql_install_db --user=mysql
#cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
#chkconfig --add mysqld
#chkconfig --level 35 mysqld on
#chkconfig --list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
#vi ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin
#vi /etc/my.cnf
# Example MySQL config file for large systems.
#
# This is for a large system with memory = 512M where the system runs mainly
# MySQL.
#
# MySQL programs look for option files in a set of
# locations which depend on the deployment platform.
# You can copy this option file to one of those
# locations. For information about these locations, see:
# http://dev.mysql.com/doc/mysql/en/option-files.html
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.
# The following options will be passed to all MySQL clients
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock
# Here follows entries for some specific programs
# The MySQL server
[mysqld]
port = 3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
ndbcluster
ndb-connectstring=192.168.73.178
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
# Replication Master Server (default)
# binary logging is required for replication
log-bin=mysql-bin
# binary logging format - mixed recommended
binlog_format=mixed
# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
# server-id = 1
# Replication Slave (comment out master section to use this)
#
# To configure this host as a replication slave, you can choose between
# two methods :
#
# 1) Use the CHANGE MASTER TO command (fully described in our manual) -
# the syntax is:
#
# CHANGE MASTER TO MASTER_HOST=, MASTER_PORT= ,
# MASTER_USER=, MASTER_PASSWORD= ;
#
# where you replace , , by quoted strings and
# by the master's port number (3306 by default).
#
# Example:
#
# CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
# MASTER_USER='joe', MASTER_PASSWORD='secret';
#
# OR
#
# 2) Set the variables below. However, in case you choose this method, then
# start replication for the first time (even unsuccessfully, for example
# if you mistyped the password in master-password and the slave fails to
# connect), the slave will create a master.info file, and any later
# change in this file to the variables' values below will be ignored and
# overridden by the content of the master.info file, unless you shutdown
# the slave server, delete master.info and restart the slaver server.
# For that reason, you may want to leave the lines below untouched
# (commented) and instead use CHANGE MASTER TO (see above)
#
# required unique id between 2 and 2^32 - 1
# (and different from the master)
# defaults to 2 if master-host is set
# but will not function as a slave if omitted
#server-id = 2
#
# The replication master for this slave - required
#master-host =
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user =
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password = #
# The port the master is listening on.
# optional - defaults to 3306
#master-port = #
# binary logging - not required for slaves, but recommended
#log-bin=mysql-bin
# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = /usr/local/mysql/data
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = /usr/local/mysql/data
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 256M
#innodb_additional_mem_pool_size = 20M
# Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size = 64M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout

8) 安裝數據節點(ndbd:192.168.73.179與192.168.73.180)
#tar zxvf mysql-cluster-gpl-7.2.4-linux2.6-x86_64.tar.gz
#mkdir -p /usr/local/mysql/bin
#mkdir -p /usr/local/mysql/ndbdata
#cp mysql-cluster-gpl-7.2.4-linux2.6-x86_64/bin/ndbd /usr/local/mysql/bin
#vi ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin
#vi /etc/my.cnf
[mysql_cluster]
ndb-connectstring=192.168.73.178

9) 啟動MySQL Cluster服務(順序:管理節點->數據節點->SQL節點)

a.在管理節點(ndb_mgmd:192.168.73.178)上執行下列指令:
#rm mysql/mysql-cluster/ndb_1_config.bin.1 <-此步非必須,若config.ini有更動則須做此步
#ndb_mgmd -f /usr/local/mysql/cluster-conf/config.ini
MySQL Cluster Management Server mysql-5.5.19 ndb-7.2.4

b.在數據節點(ndbd:192.168.73.179與192.168.73.180)上執行下列指令:(第一次啟動、備份或回復時要加上選項--initial,之後可不用加該選項)
#ndbd --initial
2012-07-06 23:25:52 [ndbd] INFO -- Angel connected to '192.168.73.178:1186'
2012-07-06 23:25:52 [ndbd] INFO -- Angel allocated nodeid: 12

c.在SQL節點(mysql:192.168.73.179與192.168.73.180)上執行下列指令:
#/etc/rc.d/init.d/mysqld start或#service mysqld start

10) 回到管理節點(ndb_mgmd:192.168.73.178)上,透過ndb_mgm來檢查Cluster的情況
#ndb_mgm -e show
Connected to Management Server at: localhost:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=11 @192.168.73.179 (mysql-5.5.19 ndb-7.2.4, Nodegroup: 0, Master)
id=12 @192.168.73.180 (mysql-5.5.19 ndb-7.2.4, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.73.178 (mysql-5.5.19 ndb-7.2.4)
[mysqld(API)] 3 node(s)
id=21 (not connected, accepting connect from 192.168.73.179)
id=22 (not connected, accepting connect from 192.168.73.180)
id=23 (not connected, accepting connect from any host)

11) 在其中一個SQL節點上建立DB並添加數據(記得要在全部都啟動完後執行,否則會出錯)
#mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.19-ndb-7.2.4-gpl-log MySQL Cluster Community Server (GPL)
Copyright (c) 2000, 2011, 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 ndbtest;
mysql>use ndbtest;
CREATE TABLE `t1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
);

之後在另一個SQL節點執行下列指令:
#mysql -u root
mysql>CREATE TABLE `t1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
);

◎之後可以切換到/usr/local/mysql/data目錄下或去資料庫下面檢查是否已經同步

12) 關閉MySQL Cluster服務(順序:SQL節點->數據節點->管理節點)
#/etc/init.d/mysqld stop或#service mysqld stop
#~/mysql-cluster-gpl-7.2.4-linux2.6-x86_64/bin/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.

◎以上就是簡單實作Mysql Cluster的範例,若要檢視其他部落客的架設過程可參考林楓小站(Source安裝)或Nekobe's Lair(RPM套件7.2.0),大致到這裡,收工!

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
This site is protected by WP-CopyRightPro