搭建mysql叢集實作高可用
一:實驗目标
MySQL 叢集 概述
實戓:MySQL 叢集 搭建
二:實驗環境
rhel-server-6.5-x86_64-dvd.iso
mysql cluster 叢集各機器角色:
Mysql 管理結點:xuegod63.cn IP:192.168.1.63
mysql 資料結點:xuegod63.cn IP:192.168.1.63
mysql 資料結點:xuegod64.cn IP:192.168.1.64
msyql SQL結點:xuegod63.cn IP:192.168.1.63
msyql SQL結點:xuegod64.cn IP:192.168.1.64
MySQL Cluster(MySQL 叢集) 概述:
MySQL Cluster 是MySQL适合于分布式計算環境的高實用、高備援版本。它采用了NDB Cluster 存儲引擎,允許在1個 Cluster 中運作多個MySQL伺服器。
MySQL Cluster 是一種技術,該技術允許在無共享的系統中部署“記憶體中”資料庫的 Cluster 。通過無共享體系結構,系統能夠使用廉價的硬體,而且對軟硬體無特殊要求。此外,由于每個元件有自己的記憶體和磁盤,不存在單點故障。
mysql 叢集架構原理:
<a href="http://s2.51cto.com/wyfs02/M00/88/E6/wKioL1gARzuTXn8RAAAwAQFFvTw048.png-wh_500x0-wm_3-wmp_4-s_4187147567.png" target="_blank"></a>
注釋:
SQL節點: 給上層應用層提供sql通路。
管理節點(MGM): 管理整個叢集。 啟動,關閉叢集。 通過ndb_mgmd指令啟動叢集
存儲/資料節點: 儲存cluster中的資料。 資料節點,可以提供副本。實作資料備援。
NDB引擎: 是一種 “記憶體中”的存儲引擎 , 它具有可用性高和資料一緻性好的特點
三:實驗代碼
前提:将MySQL-Cluster-gpl-7.3.7-1.el6.x86_64.rpm-bundle.tar 上傳到xuegod63和xuegod64上。
xuegod63上執行:
1. mysql舊版本清除: 首先使用如下指令來清理之前作業系統自帶的mysql安裝:
[root@xuegod63 ~]# yum -y remove mysql #然後使用如下指令:
[root@xuegod63 ~]# rpm -qa | grep mysql* #找到2個剩餘mysql包1請按照如下的指令格式予以删除
mysql-libs-5.1.71-1.el6.i686
[root@xuegod63 ~]# rpm -e --nodeps mysql-libs
2. mysql cluster版本安裝準備:将MySQL-Cluster-gpl-7.3.4-1.el6.x86_64.rpm-bundle.tar放到某個目錄下(譬如/package) 下面,并且執行如下指令解壓:
[root@xuegod63 ~]#tar -xvf MySQL-Cluster-gpl-7.3.4-1.el6.x86_64.rpm-bundle.tar #得到如下檔案清單:
MySQL-Cluster-client-gpl-7.3.4-1.el6.x86_64.rpm MySQL-Cluster-devel-gpl-7.3.4-1.el6.x86_64.rpm
MySQL-Cluster-embedded-gpl-7.3.4-1.el6.x86_64.rpm MySQL-Cluster-server-gpl-7.3.4-1.el6.x86_64.rpm MySQL-Cluster-shared-compat-gpl-7.3.4-1.el6.x86_64.rpm MySQL-Cluster-shared-gpl-7.3.4-1.el6.x86_64.rpm MySQL-Cluster-test-gpl-7.3.4-1.el6.x86_64.rpm
3.建立檔案夾(分如下3個類來建立對應的檔案夾)
[root@xuegod63 ~]# mkdir /var/lib/mysql #資料(存儲)結點
[root@xuegod63 ~]# mkdir /var/lib/mysql-cluster #管理節點
[root@xuegod63 ~]# mkdir /var/run/mysqld #程序DIR
SQL節點:可不用 是以不需要建立
[root@xuegod63 ~]# chmod -R 777 /var/lib/mysql
[root@xuegod63 ~]# chmod -R 777 /var/lib/mysql-cluster
[root@xuegod63 ~]# chmod -R 777 /var/run/mysqld
4.安裝mysql叢集軟體包:
[root@xuegod63 ~]# rpm -ivh MySQL-Cluster-server-gpl-7.3.4-1.el6.x86_64.rpm
[root@xuegod63 ~]# rpm -ivh MySQL-Cluster-client-gpl-7.3.4-1.el6.x86_64.rpm
#特别注意,當安裝完畢MySQL-Cluster-server-gpl包後,将出現如下提示資訊,提醒我們整個cluster安裝後的初次超級賬戶密碼存在/root/.mysql_secret這個檔案當中。 ----------------------------------------------------------------------------------------------------------A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER ! You will find that password in '/root/.mysql_secret'. You must change that password on your first connect, no other statement but 'SET PASSWORD' will be accepted. See the manual for the semantics of the 'password expired' flag. Also, the account for the anonymous user has been removed. In addition, you can run: /usr/bin/mysql_secure_installation which will also give you the option of removing the test database. This is strongly recommended for production servers. ----------------------------------------------------------------------------------------------------------
5.使用如下的指令來變更權限保證可寫入:
[root@xuegod63 ~]# id mysql
uid=495(mysql) gid=489(mysql) groups=489(mysql)
[root@xuegod63 ~]# chown mysql:mysql -R /var/lib/mysql
[root@xuegod63 ~]# chown mysql:mysql -R /var/lib/mysql-cluster/
[root@xuegod63 ~]# chown mysql:mysql -R /var/run/mysqld/
xuegod64 執行
1. mysql舊版本清除: 在xuegod63和xuegod64同時執行: 首先使用如下指令來清理之前作業系統自帶的mysql安裝:
[root@xuegod63 ~]#yum -y remove mysql #然後使用如下指令:
[root@xuegod63 ~]# rpm -qa | grep mysql*
mysql-libs-5.1.52-1.el6_0.1.x86_64 #對于找到的1個剩餘mysql包,請按照如下的指令格式予以删除:
2.複制軟體包并安裝server包:
[root@xuegod63 ~]# scp MySQL-Cluster-server-gpl-7.3.7-1.el6.x86_64.rpm 192.168.1.64:/root/
[email protected]'s password:
[root@xuegod63 ~]# scp MySQL-Cluster-client-gpl-7.3.7-1.el6.x86_64.rpm 192.168.1.64:/root
xuegod63安裝mysql叢集相關的軟體包:
[root@xuegod64 ~]# rpm -ivh MySQL-Cluster-server-gpl-7.3.7-1.el6.x86_64.rpm
注意,當安裝完畢MySQL-Cluster-server-gpl包後,将出現如下提示資訊,提醒我們整個cluster安裝後的初次超級賬戶密碼存在/root/.mysql_secret這個檔案當中。 --------------------------------------------------------------------------------------- A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER ! You will find that password in '/root/.mysql_secret'. You must change that password on your first connect, no other statement but 'SET PASSWORD' will be accepted. See the manual for the semantics of the 'password expired' flag. Also, the account for the anonymous user has been removed. In addition, you can run: /usr/bin/mysql_secure_installation which will also give you the option of removing the test database. This is strongly recommended for production servers.
1)檢視mysql root使用者密碼:
[root@xuegod64 ~]# cat /root/.mysql_secret
# The random password set for the root user at Wed Apr 1 21:10:53 2015 (local time): NSblG9hMkThTgFHY
2)會生成兩個主要的指令檔案:ndb_mgmd 和ndb_mgm
[root@xuegod63 ~]# which ndb_mgm
/usr/bin/ndb_mgm
[root@xuegod63 ~]# rpm -qf /usr/bin/ndb_mgm
MySQL-Cluster-server-gpl-7.3.7-1.el6.x86_64
[root@xuegod63 ~]# which ndb_mgmd /usr/sbin/ndb_mgmd
[root@xuegod63 ~]# rpm -qf /usr/sbin/ndb_mgmd
3)還會生成使用者:mysql
3.建立檔案夾: 存放程序PID檔案: mkdir /var/run/mysqld
[root@xuegod64 ~]# id mysql
[root@xuegod63 ~]# mkdir /var/lib/mysql #不需要建立自動生成
[root@xuegod63 ~]# ll /var/lib/mysql
[root@xuegod64 ~]# mkdir /var/run/mysqld
[root@xuegod64 ~]# chown mysql:mysql -R /var/run/mysqld
注: 資料結點存放資料: /var/lib/mysql 會自動建立。
4.安裝mysql叢集用戶端指令:
[root@xuegod63 ~]# rpm -ivh MySQL-Cluster-client-gpl-7.3.7-1.el6.x86_64.rpm
注:MySQL-Cluster-server 和MySQL-Cluster-client 的關系 和msyql-server同mysql 包的關系一樣。
xuegod63: 建立管理結點上配置檔案:
[root@xuegod63 ~]# cd /var/lib/mysql-cluster
[root@xuegod63 ~]# vi config.ini #寫入以下内容
[ndbd default]
NoOfReplicas=2
DataMemory=200M
IndexMemory=100M
##### data node manger:
[ndb_mgmd]
id=1
datadir=/var/lib/mysql-cluster
HostName=192.168.1.63
###### data node options:
[ndbd]
DataDir=/var/lib/mysql
id=2
HostName=192.168.1.64
id=3
######SQL node options:
[mysqld]
id=4
id=5
參數說明:
在這個檔案裡,我們分别給五個節點配置設定了ID,這有利于更好的管理和區分各個節點。當然,要是不指定,MySQL也會動态配置設定一個
cat /var/lib/mysql-cluster/config.ini
NoOfReplicas=2 #資料寫入數量。2表示兩份
##### data node manger:#管理結點
datadir=/var/lib/mysql-cluster # 管理結點的日志
HostName=192.168.1.63 #管理結點的IP位址。本機IP
###### data node options: 存儲結點
DataDir=/var/lib/mysql #mysql資料存儲路徑
# SQL node options: #關于SQL結點
xuegod63資料節點+SQL節點配置文檔:
[root@xuegod63 /]# vim /etc/my.cnf #寫入以下内容
datadir=/var/lib/mysql #mysql資料存儲路徑
ndbcluster #啟動ndb引擎
ndb-connectstring=192.168.1.63 # 管理節點IP位址
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[mysql_cluster]
ndb-connectstring=192.168.1.63 #管理節點IP位址
----------------注:如果xuegod63 隻做 SQL節點,則配置文檔為:------------
[root@xuegod63 /]# cat !$
cat /etc/my.cnf
說明: 資料節點和SQL結點配置檔案差別 ,就多一行,資料結點有:datadir=/var/lib/mysql ,SQL節點上沒有。
-------------------------------------------------------------------------------------------------
xuegod64配置資料結點和SQL結點:生成配置檔案:
[root@xuegod63 mysql-cluster]# scp /etc/my.cnf 192.168.1.64:/etc/
[email protected]'s password: 123456
[root@xuegod64 /]# cat /etc/my.cnf #寫入以下内容。預設沒有my.cnf檔案
[mysqld] datadir=/var/lib/mysql #mysql資料存儲路徑
mySQL Cluster初次啟動指令以及使用者密碼更改調整:(請嚴格按照次序啟動)
啟動 mysql cluster
先啟動: 管理結點服務-->資料結點服務-->sql結點服務
關閉: 關閉管理結點服務,關閉管理結點服務後,nbdb資料結點服務會自動關閉-->手動把sql結點服務關了。 執行初次啟動前請先确認 将兩台機器的防火牆關閉(service iptables stop 或者 設定 防火牆端口可通,兩個端口即通訊端口1186、資料端口3306 )
1:xuegod63上啟動管理結點指令:
[root@xuegod63 ~]#ndb_mgmd -f /var/lib/mysql-cluster/config.ini # mysql cluster 背景運作程序
這裡出現警告是正常的。
報錯的原因:管理結點好。 資料結點,存儲結點。
解答:這裡講管理結點和資料結點用一台伺服器搭建,實際生産環境中是不可行的,因為一旦資料結點挂了,那麼就意味着資料節點也會挂掉的。
檢視端口号:
[root@xuegod63 ~]# netstat -antup | grep 1186
tcp 0 0 0.0.0.0:1186 0.0.0.0:* LISTEN 7057/ndb_mgmd
tcp 0 0 127.0.0.1:1186 127.0.0.1:60324 ESTABLISHED 7057/ndb_mgmd
tcp 0 0 127.0.0.1:60324 127.0.0.1:1186 ESTABLISHED 7057/ndb_mgmd
2:xuegod63和xuegod64都要啟動資料結點服務:
[root@xuegod63 ~]# ndbd --initial #啟動初始化
2015-04-01 21:57:58 [ndbd] INFO -- Angel connected to '192.168.1.63:1186'
2015-04-01 21:57:58 [ndbd] INFO -- Angel allocated nodeid: 2
[root@xuegod64 ~]# ndbd --initial #啟動初始化
2014-12-02 22:24:31 [ndbd] INFO -- Angel connected to '192.168.1.63:1186'
2014-12-02 22:24:31 [ndbd] INFO -- Angel allocated nodeid: 3
[root@xuegod63 ~]# vim /var/lib/mysql-cluster/config.ini #檢視資料結點對應的ID
id=3
3:xuegod63和xuegod64都要啟動SQL結點服務:
[root@xuegod63 ~]#mysqld_safe --defaults-file=/etc/my.cnf &
[root@xuegod64 ~]#mysqld_safe --defaults-file=/etc/my.cnf &
4:檢視mysql 叢集狀态:
[root@xuegod63 ~]# ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: localhost:1186
[ndbd(NDB)]2 node(s)
[email protected] (mysql-5.6.21 ndb-7.3.7, starting, Nodegroup: 0)
id=3 (not connected, accepting connect from 192.168.1.64)
[ndb_mgmd(MGM)]1 node(s)
[email protected] (mysql-5.6.21 ndb-7.3.7)
[mysqld(API)]2 node(s)
id=4 (not connected, accepting connect from 192.168.1.63)
id=5 (not connected, accepting connect from 192.168.1.64)
測試叢集,資料同步:
1:xuegod63 修改root密碼:
[root@xuegod63 /]# cat /root/.mysql_secret
# The random password set for the root user at Tue Dec 2 21:25:59 2014 (local time): rWG1av6XjzT4ghfd
[root@xuegod63 /]# mysql -uroot -prWG1av6XjzT4ghfd
mysql> show databases;
ERROR 1820 (HY000): You must SET PASSWORD before executing this statement
mysql> set password=password('123456');
[root@xuegod63 /]# mysql -uroot -p123456
2:xuegod64 修改root密碼:
# The random password set for the root user at Tue Dec 2 21:31:11 2014 (local time): 2nOsamIw5wkBNhDm
[root@xuegod64 ~]# mysql -uroot -p2nOsamIw5wkBNhDm
3:插入資料并測試資料同步
1)xuegod64插入資料
[root@xuegod64 ~]# mysql -uroot -p123456
mysql> create database mk;
+--------------------+ | Database |
+--------------------+
| information_schema |
| mk |
| mysql |
| ndb_3_fs |
| ndbinfo |
| performance_schema |
| test |
2)xuegod63同步:
[root@xuegod63 ~]# mysql -uroot -p123456
關閉伺服器
注:關閉: 關閉管理節點服務時資料結點服務會自動關閉 –-> 手動關閉SQL結點服務
1:關閉管理節點服務 ,時資料結點服務自動關閉
[root@xuegod63 ~]# ndb_mgm
ndb_mgm> shutdown
Node 2: Cluster shutdown initiated
Node 3: Cluster shutdown initiated
3 NDB Cluster node(s) have shutdown.
Disconnecting to allow management server to shutdown.
Node 2: Node shutdown completed.
ndb_mgm> exit
ps -axu | grep ndbd #檢視不到,說明資料節點已經被關 :
2:手動關閉SQL結點服務
[root@xuegod63 ~]# ps -axu | grep mysql
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root 7620 0.0 0.0 106280 640 pts/0 S 22:25 0:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/etc/my.cnf
mysql 7746 4.6 37.9 1692684 442112 pts/0 Sl 22:25 0:39 /usr/sbin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid
root 7917 0.0 0.2 125856 2372 pts/1 S+ 22:34 0:00 mysql -uroot -px xxxx
root 8014 0.0 0.0 103300 852 pts/0 S+ 22:39 0:00 grep mysql
[root@xuegod63 /]# kill -9 7620 #關閉主就OK了
[root@xuegod63 /]# kill -9 7746
[root@xuegod64 ~]# ps -axu | grep mysql
root 5572 0.0 0.0 106280 640 pts/0 S 22:25 0:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/etc/my.cnf
mysql 5770 4.6 37.9 1692684 442112 pts/0 Sl 22:25 0:39 /usr/sbin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid
[root@xuegod64 ~]# kill -9 7620
實驗總結:
msyql叢集啟動:
[root@xuegod63 /]# ndb_mgmd -f /var/lib/mysql-cluster/config.ini
MySQL Cluster Management Server mysql-5.6.21 ndb-7.3.7
[root@xuegod63 /]# ndbd
2014-12-02 22:41:59 [ndbd] INFO -- Angel connected to '192.168.1.63:1186'
2014-12-02 22:41:59 [ndbd] INFO -- Angel allocated nodeid: 2
[root@xuegod64 /]# ndbd
[root@xuegod63 /]# mysqld_safe --defaults-file=/etc/my.cnf &
[root@xuegod64 /]# mysqld_safe --defaults-file=/etc/my.cnf &
本文轉自 于學康 51CTO部落格,原文連結:http://blog.51cto.com/blxueyuan/1861806,如需轉載請自行聯系原作者