Server:
sql1.com – 10.199.3.36 - MySQL cluster node 1
sql2.com – 10.199.3.37 - MySQL cluster node 2
loaddb1.com – 10.199.3.38 - Load Balancer 1 & MySQL cluster management server
loaddb2.com – 10.199.3.39 - Load Balancer 2
sql.com – 10.199.3.35 - MySQL cluster with Vitural IP
Recommended:
2 MySQL cluster nodes + 1 cluster management server + 2 Load Balancers = 5 servers.
Requirements:
OS: CentOS
Packages: gcc g++ gcc-g++ gdbm gdbm-devel openssl openssl-devel stunnel krb5-devel bzip2 bzip2-devel.
Download gói MySQL mysql-max-5.0.27-linux-i686.tar.gz (42MB)
$ mkdir /var/src/tar
$ wget
http://mysql.cdpa.nsysu.edu.tw/Downl...ux-i686.tar.gz
Giải nén.
$ cd /var/src
$ tar -xzf ./tar/mysql-max-5.0.27-linux-i686.tar.gz
Server: loaddb1.com: 10.199.3.38 Load Balancer 1 / MySQL cluster management server
Trước tiên, chúng ta cần cài đặt cluster management server (ndb_mgmd) và cluster management client (ndb_mgm - dùng để theo dõi những gì diễn ra trên hệ thống cluster).
$ cd mysql-max-5.0.27-linux-i686/
$ mv bin/ndb_mgm /usr/bin
$ mv bin/ndb_mgmd /usr/bin
$ chmod 755 /usr/bin/ndb_mg*
Tiếp theo, chúng ta tạo file cấu hình cho hệ thống cluster /var/lib/mysql-cluster/config.ini
$ mkdir /var/lib/mysql-cluster
$ cd /var/lib/mysql-cluster
$ vi config.ini
HTML Code:
[NDBD DEFAULT]
NoOfReplicas=2
[MYSQLD DEFAULT]
[NDB_MGMD DEFAULT]
[TCP DEFAULT]
# Section for the cluster management node
[NDB_MGMD]
# IP address of the management node (this system)
HostName=10.199.3.38 #Va IP la 10.199.3.39 voi loaddb2.com
# Section for the storage nodes
[NDBD]
# IP address of the first storage node
HostName=10.199.3.35
DataDir= /var/lib/mysql-cluster
[NDBD]
# IP address of the second storage node
HostName=10.199.3.36
DataDir=/var/lib/mysql-cluster
# one [MYSQLD] per storage node
[MYSQLD]
[MYSQLD]
Sau đó thêm script này vào phần bootup
$ echo 'ndb_mgmd -f /var/lib/mysql-cluster/config.ini' > /etc/init.d/ndb_mgmd
$ chmod 755 /etc/init.d/ndb_mgmd
$ ndb_mgmd -f /var/lib/mysql-cluster/config.ini
Server: sql1.vng.com: 10.199.3.36 MySQL cluster node 1 & sql2.com: 10.199.3.37 MySQL cluster node 2
$ groupadd mysql
$ useradd -g mysql mysql
$ cp -Rf /var/src/mysql-max-5.0.27-linux-i686 /usr/local/mysql
$ cd /usr/local/mysql
$ scripts/mysql_install_db --user=mysql
$ chown -R root:mysql .
$ chown -R mysql data
$ cp support-files/mysql.server /etc/init.d/
$ chmod 755 /etc/init.d/mysql.server
$ chkconfig mysql.server on
$ cd /usr/local/mysql/bin
$ mv * /usr/bin
$ cd ../
$ rm -fr /usr/local/mysql/bin
$ ln -s /usr/bin /usr/local/mysql/bin
Tạo file config cho MySQL:
$ vi /etc/my.cnf
HTML Code:
[mysqld]
ndbcluster
# IP address of the cluster management node
ndb-connectstring=10.199.3.38
[mysql_cluster]
# IP address of the cluster management node
ndb-connectstring=10.199.3.38
$ mkdir /var/lib/mysql-cluster
$ cd /var/lib/mysql-cluster
$ ndbd --initial
$ /etc/init.d/mysql.server start
$ echo 'ndbd' > /etc/init.d/ndbd
$ chmod 755 /etc/init.d/ndbd
$ mysqladmin -u root password passwordroot
$ ndbd
Load Balancers with Ultra Monkey
Lưu ý là cả 2 server loaddb1.com và db2.com phải hỗ trợ PVS (IP Virtual Server) trong kernels. IPVS implements transport-layer load balancing inside the Linux kernel.
Chúng ta bắt đầu bằng việc kích họat IPVS
$ modprobe ip_vs_dh ;\
modprobe ip_vs_ftp ;\
modprobe ip_vs ;\
modprobe ip_vs_lblc ;\
modprobe ip_vs_lblcr ;\
modprobe ip_vs_lc ;\
modprobe ip_vs_nq ;\
modprobe ip_vs_rr ;\
modprobe ip_vs_sed ;\
modprobe ip_vs_sh ;\
modprobe ip_vs_wlc ;\
modprobe ip_vs_wrr
Để kích họat trong lúc bootup ta phải sửa và thêm trong /etc/modules
$ vi /etc/modules
ip_vs_dh
ip_vs_ftp
ip_vs
ip_vs_lblc
ip_vs_lblcr
ip_vs_lc
ip_vs_nq
ip_vs_rr
ip_vs_sed
ip_vs_sh
ip_vs_wlc
ip_vs_wrr
Tiếp đến chúng ta cài đặt một số gói phần mềm cần cho việc cài đặt.
$yum install libdbi-dbd-mysql.i386 libdbi.i386 libdbi-devel.i386 mysqlclient10 curl libidn perl-Crypt-SSLeay.i386 perl-Digest-HMAC.i386 perl-HTML-Parser.i386 perl-Net-DNS.i386 perl-XML-Parser.i386 perl-libwww-perl.i386 perl-DBI.i386 perl-DBI.i386 perl-CGI.i386 perl-DBD-MySQL.i386 heartbeat mysqlclient10-devel.i386 heartbeat-ldirectord.i386 heartbeat-ldirectord-1.2.3.cvs.20050927-1.centos4.i386 ipvsadm.i386
Kiểm trả các gói sau, nếu chưa có thì download:
$ cd /var/src
$ wget
http://www.ultramonkey.org/download/...m.1.noarch.rpm
$ wget
http://www.ultramonkey.org/download/...m.1.noarch.rpm
$ wget
http://www.ultramonkey.org/download/...m.1.noarch.rpm
$ wget
http://www.ultramonkey.org/download/...m.1.noarch.rpm
$ wget
http://www.ultramonkey.org/download/....um.1.i386.rpm
$ wget
http://www.ultramonkey.org/download/...m.1.noarch.rpm
$ wget
http://www.ultramonkey.org/download/...m.1.noarch.rpm
$ wget
http://www.ultramonkey.org/download/...m.1.noarch.rpm
$ wget
http://www.ultramonkey.org/download/...m.1.noarch.rpm
Cài đặt:
$ rpm –Uvh /var/src/*.rpm
The libdbd-mysql-perl package we've just installed does not work with MySQL 5 (we use MySQL 5 on our MySQL cluster...), so we install the newest DBD::mysql Perl package:
$ cd /tmp
$ wget
http://www.archive.hk/cpan/authors/i...-3.0002.tar.gz
$ tar xvfz DBD-mysql-3.0002.tar.gz
$ cd DBD-mysql-3.0002
$ perl Makefile.PL
$ make
$ make install
We must enable packet forwarding:
$vi /etc/sysctl.conf
# Enables packet forwarding
net.ipv4.ip_forward = 1
$ sysctl -p
Configure heartbeat
$ vi /etc/ha.d/ha.cf
logfacility local0
bcast eth0
mcast eth0 225.0.0.1 694 1 0
auto_failback off
node loaddb1
node loaddb2
respawn hacluster /usr/lib/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster
$ vi /etc/ha.d/haresources
loaddb1 \ #or loaddb2
ldirectord::ldirectord.cf \
LVSSyncDaemonSwap::master \
IPaddr2::10.199.3.35/24/eth0/10.199.3.255
$ vi /etc/ha.d/authkeys
auth 3
3 md5 shitkuku
$ chmod 600 /etc/ha.d/authkeys
Configure ldirectord
$ vi /etc/ha.d/ldirectord.cf
# Global Directives
checktimeout=10
checkinterval=2
autoreload=no
logfile="local0"
quiescent=yes
virtual = 10.199.3.35:3306
service = mysql
real = 10.199.3.36:3306 gate
real = 10.199.3.37:3306 gate
checktype = negotiate
login = "ldirector"
passwd = "ldirectorpassword"
database = "ldirectordb"
request = "SELECT * FROM connectioncheck"
scheduler = wrr
$ chkconfig heartbeat on
$ chkconfig ldirectord off
$ chmod 755 /etc/init.d/heartbeat
Create A Database Called ldirector
Chúng ta tạo user với quyền đây đủ cho ldirectord trên sql1.com:
$ mysql -u root -p
mysql> GRANT ALL ON ldirectordb.* TO 'ldirector'@'%' IDENTIFIED BY 'ldirectorpassword';
mysql> FLUSH PRIVILEGES;
mysql> CREATE DATABASE ldirectordb;
mysql> USE ldirectordb;
mysql> CREATE TABLE connectioncheck (i INT) ENGINE=NDBCLUSTER;
mysql> INSERT INTO connectioncheck () VALUES (1);
quit;
Và trên sql2.com
mysql -u root -p
mysql> GRANT ALL ON ldirectordb.* TO 'ldirector'@'%' IDENTIFIED BY 'ldirectorpassword';
mysql> FLUSH PRIVILEGES;
mysql> CREATE DATABASE ldirectordb;
mysql> quit;
$ /etc/init.d/ldirectord stop
$ /etc/init.d/heartbeat start
Cài đặt LVS cho sql1.com và sql2.com
$ vi /etc/sysctl.conf
# When an arp request is received on eth0, only respond if that address is
# configured on eth0. In particular, do not respond if the address is
# configured on lo
net.ipv4.conf.eth0.arp_ignore = 1
# Ditto for eth1, add for all ARPing interfaces
#net.ipv4.conf.eth1.arp_ignore = 1
# Enable configuration of arp_announce option
net.ipv4.conf.all.arp_announce = 2
# When making an ARP request sent through eth0 Always use an address that
# is configured on eth0 as the source address of the ARP request. If this
# is not set, and packets are being sent out eth0 for an address that is on
# lo, and an arp request is required, then the address on lo will be used.
# As the source IP address of arp requests is entered into the ARP cache on
# the destination, it has the effect of announcing this address. This is
# not desirable in this case as adresses on lo on the real-servers should
# be announced only by the linux-director.
net.ipv4.conf.eth0.arp_announce = 2
# Ditto for eth1, add for all ARPing interfaces
#net.ipv4.conf.eth1.arp_announce = 2
$ cd /var/src
$ wget
http://www.ultramonkey.org/download/...m.1.noarch.rpm
$ rpm -Uvh arptables-noarp-addr-0.99.2-1.rh.el.um.1.noarch.rpm
$ arptables-noarp-addr 10.199.3.35 start
$ /etc/init.d/arptables_jf save
$ chkconfig --level 2345 arptables_jf on
$ /etc/init.d/arptables_jf start
$ vi /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
IPADDR=10.199.3.35
NETMASK=255.255.255.255
NETWORK=10.199.3.0
BROADCAST=10.199.3.255
ONBOOT=yes
NAME=loopback
$ ifup lo:0
All done. Good luck! ^^