CentOS-7安裝Mysql叢集

來源:互聯網
上載者:User

標籤:blog   http   io   ar   os   使用   sp   strong   on   

安裝要求

安裝環境:CentOS-7
安裝方式:源碼編譯安裝
軟體名稱:mysql-cluster-gpl-7.3.7-linux2.6-x86_64.tar.gz
:http://mysql.mirror.kangaroot.net/Downloads/
軟體安裝位置:/usr/local/mysql
資料存放位置:/var/mysql/data
日誌存放位置:/var/mysql/logs

叢集設計

首先設計叢集的安裝分配方式,我共需要5台伺服器,伺服器分配如下:
管理節點:    192.168.1.214
sql節點1:   192.168.1.215
sql節點2:   192.168.1.216
資料節點1:  192.168.1.217
資料節點2:  192.168.1.218
裝置的串連方式如所示:
 
注意:
目前這種設計存在的問題是管理節點是單點的,214掛掉後整個叢集就會癱瘓,先不管這個問題,首先把這個簡單的MySQL叢集先搭建起來。

檢查安裝的mysql

檢查系統中已經安裝過的mysql資訊,操作如下:

[[email protected] /]# rpm -qa | grep mysql
[[email protected] /]# service mysql status

如果安裝過其他版本的mysql,請卸載,操作如下:

[[email protected] /]# /etc/init.d/mysqld stop  //關閉目前的mysql服務
[[email protected] /]# ps -ef | grep mysql  //檢驗mysql是否已經關閉
#如果沒關閉,執行kill -9 連接埠號碼
#執行刪除
rpm -e --allmatches --nodeps mysql mysql-server
rm -rf /var/lib/mysql  // 刪除mysql的安裝目錄
管理節點 管理節點安裝

安裝管理節點(192.168.15.231)

[[email protected] /]# groupadd mysql
[[email protected] /]#  useradd mysql -g mysql
[[email protected] /]# cd /usr/local
[[email protected] local]# tar -zxv -f mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz
[[email protected] local]# mv mysql-cluster-gpl-7.2.6-linux2.6-x86_64 mysql
[[email protected] local]# chown -R mysql:mysql mysql
[[email protected] local]# cd mysql
[[email protected] mysql]# scripts/mysql_install_db --user=mysql

 

管理節點配置

[[email protected] ~]#  mkdir /var/lib/mysql-cluster
[[email protected] ~]# cd /var/lib/mysql-cluster
[[email protected] mysql-cluster]# vi + /var/lib/mysql-cluster/config.ini

在config.ini 中添加以下內容:

[NDBD DEFAULT]
NoOfReplicas=1
[TCP DEFAULT]
portnumber=3306

[NDB_MGMD]
#設定管理節點伺服器
HostName=192.168.15.231
DataDir=/var/mysql/data

[NDBD]
#設定儲存節點伺服器(NDB節點)
HostName=192.168.15.234
DataDir=/var/mysql/data

[NDBD]
#第二個NDB節點
HostName=192.168.15.235
DataDir=/var/mysql/data

[MYSQLD]
#設定SQL節點伺服器
HostName=192.168.15.232
[MYSQLD]
#第二個SQL節點
HostName=192.168.15.233

管理節點啟動

[[email protected] ~]# /usr/local/mysql/bin/ndb_mgmd -f /var/lib/mysql-cluster/config.ini
[[email protected] ~]# mkdir /var/mysql/logs
[[email protected] ~]# netstat -lntpu

[[email protected] ~]#cd /usr/local/mysql/bin 

[[email protected] ~]#cp ./ndb_mgm /usr/local/bin/ 

[[email protected] ~]#cp ./ndb_mgmd /usr/local/bin/ 

看到tcp 0 0 0.0.0.0:1186開放說明啟動正常
開啟管理節點伺服器的1186連接埠

管理節點檢驗

執行以下操作:

[[email protected] /]# 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 (not connected, accepting connect from 192.168.15.234)
id=3 (not connected, accepting connect from 192.168.15.235)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @192.168.15.231  (mysql-5.5.22 ndb-7.2.6)

[mysqld(API)]   2 node(s)
id=4 (not connected, accepting connect from 192.168.15.232)
id=5 (not connected, accepting connect from 192.168.15.233)

管理節點關閉

[[email protected] /]# /usr/local/mysql/bin/ndb_mgm -e shutdown

#成功後終端列印出以下資訊
Connected to Management Server at: 192.168.15.236:1186
3 NDB Cluster node(s) have shutdown.
Disconnecting to allow management server to shutdown.

資料節點 資料節點安裝

資料節點1: 192.168.15.234
資料節點2: 192.168.15.235

[[email protected] /]# groupadd mysql
[[email protected] /]# useradd mysql -g mysql
[[email protected] /]# cd /usr/local
[[email protected] local]# tar -zxv -f mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz
[[email protected] local]# mv mysql-cluster-gpl-7.2.6-linux2.6-x86_64 mysql
[[email protected] local]# chown -R mysql:mysql mysql
[[email protected] local]# cd mysql
[[email protected] mysql]# scripts/mysql_install_db --user=mysql
[[email protected] mysql]# cp support-files/my-medium.cnf /etc/my.cnf
[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld

cp ./ndb_mgmd /usr/local/bin/ 

資料節點配置

對資料節點進行配置,執行以下操作:

[[email protected] mysql]# mkdir /var/mysql/data
[[email protected] mysql]# mkdir /var/mysql/logs
[[email protected] mysql]# vi /etc/my.cnf

向檔案追加以下內容:

[MYSQLD]
ndbcluster
ndb-connectstring=192.168.15.231
[MYSQL_CLUSTER]
ndb-connectstring=192.168.15.231
[NDB_MGM]
connect-string=192.168.15.231
資料節點啟動

啟動此處時,管理節點伺服器防火牆必須開啟1186,3306連接埠。
注意:只是在第一次啟動或在備份/恢複或配置變化後重啟ndbd時,才加–initial參數!
第一次啟動如下:

[[email protected] mysql]# /usr/local/mysql/bin/ndbd --initial
2013-01-30 13:43:53 [ndbd] INFO     -- Angel connected to ‘192.168.15.231:1186‘
2013-01-30 13:43:53 [ndbd] INFO     -- Angel allocated nodeid: 2

正常啟動方式:

[[email protected] mysql]# /usr/local/mysql/bin/ndbd
資料節點關閉

[[email protected] /]# /etc/rc.d/init.d/mysqld stop
或者
[[email protected] mysql]# /etc/init.d/mysql stop
Shutting down MySQL.. SUCCESS!

/usr/local/mysql/bin/mysqladmin -uroot shutdown

SQL節點安裝 SQL節點安裝

SQL節點和儲存節點(NDB節點)安裝相同,都執行以下操作;
sql節點1: 192.168.15.232
sql節點2: 192.168.15.233

[[email protected] /]# groupadd mysql
[[email protected] /]# useradd mysql -g mysql
[[email protected] /]# cd /usr/local
[[email protected] local]# tar -zxv -f mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz
[[email protected] local]# mv mysql-cluster-gpl-7.2.6-linux2.6-x86_64 mysql
[[email protected] local]# chown -R mysql:mysql mysql
[[email protected] local]# cd mysql
[[email protected] mysql]# scripts/mysql_install_db --user=mysql
[[email protected] mysql]# cp support-files/my-medium.cnf /etc/my.cnf
[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld
SQL節點配置

執行以下操作:

[[email protected] mysql]# mkdir /var/mysql/data     //建立儲存資料的檔案夾
[[email protected] mysql]# mkdir /var/mysql/logs     //建立儲存日誌的檔案夾
[[email protected] mysql]# vi /usr/local/mysql/my.cnf  //修改設定檔

追加以下內容:

[MYSQLD]
ndbcluster
ndb-connectstring=192.168.15.231
[MYSQL_CLUSTER]
ndb-connectstring=192.168.15.231
[NDB_MGM]
connect-string=192.168.15.231
SQL節點啟動

執行以下操作:

[[email protected] mysql]# service mysqld start
Starting MySQL.. SUCCESS!
SQL節點關閉

最直接的方式:

[[email protected] mysql]# /usr/local/mysql/bin/mysqladmin -uroot shutdown

[[email protected] /]# /etc/rc.d/init.d/mysqld stop
或者
[[email protected] mysql]# /etc/init.d/mysql stop
Shutting down MySQL.. SUCCESS!

功能測試

在管理節點(192.168.15.231)上查看服務狀態

[[email protected] ~]# /usr/local/mysql/bin/ndb_mgm

-- NDB Cluster -- Management Client --
ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)]     2 node(s)
id=2    @192.168.15.234  (mysql-5.5.22 ndb-7.2.6, Nodegroup: 0, Master)
id=3    @192.168.15.235  (mysql-5.5.22 ndb-7.2.6, Nodegroup: 1)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @192.168.15.231  (mysql-5.5.22 ndb-7.2.6)

[mysqld(API)]   2 node(s)
id=4    @192.168.15.232  (mysql-5.5.22 ndb-7.2.6)
id=5    @192.168.15.233  (mysql-5.5.22 ndb-7.2.6)

可以看到這裡的資料節點、管理節點、sql節點都是正常的。
注意

  1. 在建表的時候一定要用ENGINE=NDB或ENGINE=NDBCLUSTER指定使用NDB叢集儲存引擎,或用ALTER TABLE選項更改表的儲存引擎。
  2. NDB表必須有一個主鍵,因此建立表的時候必須定義主鍵,否則NDB儲存引擎將自動產生隱含的主鍵。
  3. Sql節點的使用者權限表仍然採用MYISAM儲存引擎儲存的,所以在一個Sql節點建立的MySql使用者只能訪問這個節點,如果要用同樣的使用者訪問別的Sql節點,需要在對應的Sql節點追加使用者。雖然在MySql Cluster7.2版本開始提供了”使用者權限共用”。
資料同步性測試

在一個資料節點上進行相關資料庫的建立,然後到另外一個資料節點上看看資料是否同步。

  1. 1步:

SQL節點1(192.168.15.232)上增加資料:

[[email protected] mysql]#  /etc/rc.d/init.d/mysqld status                //檢驗mysql是否運行
[[email protected] mysql]# /etc/rc.d/init.d/mysqld start                   //啟動mysql
[[email protected] mysql]# /usr/local/mysql/bin/mysql -u root -p
Enter password:
mysql> show databases;
mysql> create database testdb2;
mysql> use testdb2;
mysql> CREATE TABLE td_test2 (i INT) ENGINE=NDB;
//這裡必須指定資料庫表的引擎為NDBCLUSTER,與設定檔中的名稱相同
mysql> INSERT INTO td_test2() VALUES (1);
mysql> INSERT INTO td_test2() VALUES (152);
mysql> SELECT * FROM td_test2;
  1. 2步:

進入到SQL節點2(192.168.15.233)上查看資料

mysql> use testdb2;
Database changed
mysql> SELECT * FROM td_test2;
+------+
| i    |
+------+
|  126 |
|    1 |
+------+
2 rows in set (0.01 sec)

查看錶的引擎是不是NDB:

>show create table td_test2;
  1. 3步:

反向測試,SQL節點2(192.168.15.233)上增加資料:

mysql> create database bb;
mysql> use bb;
mysql> CREATE TABLE td_test3 (i INT) ENGINE=NDB;
mysql> INSERT INTO td_test3 () VALUES (98);
mysql> SELECT * FROM td_test3;

SQL節點1(192.168.15.232)上查看資料:

mysql>  use bb;
Database changed
mysql>  SELECT * FROM td_test3;
+------+
| i    |
+------+
|   98 |
+------+
1 row in set (0.00 sec)
關閉叢集

先關閉管理節點,然後關閉SQL節點和資料節點。

叢集啟動操作順序

要再次啟動叢集,按照以下順序執行:
管理節點 -> 資料節點 –> SQL節點 注意:此次啟動資料節點時不要加”–initial”參數。 

安裝及測試中的錯誤 啟動中的錯誤

錯誤提示:
Can‘t connect to local MySQL server through socket ‘/tmp/mysql.sock‘ (2)
解決辦法1(連接埠佔用) netstat -anp |grep 3306
kill -9 進程號
解決辦法1(許可權問題) [[email protected] mysql]# chown -R mysql:mysql /var/mysql 

CentOS-7安裝Mysql叢集

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.