1. 程式人生 > >2-18-搭建mysql集群實現高可用

2-18-搭建mysql集群實現高可用

random see 客戶 red manual 安裝mysql har 區分 後臺運行


1 環境清理以及安裝

1.1 mysql舊版本清除

準備5臺虛擬機,分配如下

mysql管理結點:xuegod1.cn IP:192.168.10.31 (安裝server、client)

mysql數據結點:xuegod2.cn IP:192.168.10.32 (安裝server、client)

mysql數據結點:xuegod3.cn IP:192.168.10.33 (安裝server、client)

msyqlSQL結點:xuegod4.cn IP:192.168.10.34 (安裝server、client)

msyqlSQL結點:xuegod5.cn IP:192.168.10.35 (安裝server、client)

首先使用如下命令來清理之前操作系統自帶的mysql安裝:

[[email protected] ~]# yum remove -y mysql

然後使用如下命令:

[[email protected] ~]# rpm -qa|grep mysql

mysql-libs-5.1.73-7.el6.x86_64

對於找到的2個剩余mysql包,請按照如下的命令格式予以刪除:

[[email protected] ~]# rpm -e --nodeps mysql-libs-5.1.73-7.el6.x86_64

最後刪掉下列文件:

01. rm -rf /var/lib/mysql/*

02. rm -rf /etc/my.cnf

03. rm -rf /etc/init.d/mysqld

1.2 mysql cluster版本安裝及準備工作

1 將MySQL-Cluster-gpl-7.3.4-1.el6.x86_64.rpm-bundle.tar放到某個目錄下(譬如/package) 下面,並且執行如下命令解壓:

tar -xvfMySQL-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

也可以在PC端解壓,只上傳server和client的rpm包

通過scp命令講server和client rpm包傳到每一臺服務器

[[email protected] ~]# scp MySQL-Cluster-server-gpl-7.4.11-1.el6.x86_64.rpm MySQL-Cluster-client-gpl-7.4.11-1.el6.x86_64.rpm 192.168.10.32:/root/

2 每一臺主機都要安裝mysql集群軟件包(server\client)批量安裝

開啟所有服務器會話,在xshell右下角選擇全部會話,然後在下面的框裏面輸入命令回車

tar -xvf MySQL-Cluster-gpl-7.3.4-1.el6.x86_64.rpm-bundle.tar

批量安裝mysql集群軟件包(server\client)

yum -y install MySQL-Cluster-server-gpl-7.4.11-1.el6.x86_64.rpm

rpm -ivh MySQL-Cluster-client-gpl-7.4.11-1.el6.x86_64.rpm

會生成兩個主要的命令文件:ndb_mgmd 和ndb_mgm

[[email protected] ~]# whichndb_mgm

/usr/bin/ndb_mgm

[[email protected] ~]# rpm -qf/usr/bin/ndb_mgm

MySQL-Cluster-server-gpl-7.3.7-1.el6.x86_64

[[email protected] ~]# whichndb_mgmd

/usr/sbin/ndb_mgmd

[[email protected] ~]# rpm -qf/usr/sbin/ndb_mgmd

MySQL-Cluster-server-gpl-7.3.7-1.el6.x86_64

還會生成用戶:mysql

[[email protected] ~]# id mysql

uid=495(mysql)gid=489(mysql) groups=489(mysql)

1.3 創建文件夾

分如下三個雷來創建對應的文件夾

管理節點創建:

[[email protected] ~]# mkdir /var/lib/mysql-cluster /var/run/mysqld/

數據節點存放數據:

[[email protected] ~]# mkdir /var/run/mysqld

[[email protected] ~]# mkdir /var/run/mysqld

SQL節點: 可不用文件授權 (進程PID目錄)

[[email protected] ~]# mkdir /var/run/mysqld

[[email protected] ~]# mkdir /var/run/mysqld

1.4 授權

管理節點授權:

[[email protected] ~]# chown mysql.mysql -R /var/lib/mysql-cluster/ /var/run/mysqld/

數據節點授權:

[[email protected] ~]# chown mysql.mysql -R /var/lib/mysql/ /var/run/mysqld/

[[email protected] ~]# chown mysql.mysql -R /var/lib/mysql/ /var/run/mysqld/

SQL節點授權:

[[email protected] ~]# chown mysql.mysql -R /var/lib/mysql/ /var/run/mysqld/

[[email protected] ~]# chown mysql.mysql -R /var/lib/mysql/ /var/run/mysqld/

1.5 查看mysql root用戶密碼

註意:當安裝完畢MySQL-Cluster-server-gpl包後,將出現如下

提示信息,提醒我們整個cluster安裝後的初次超級賬戶密碼存在/root/.mysql_secret這個文件當中。

[[email protected] ~]# cat /root/.mysql_secret

# The random password set for the root user at Wed May 10 16:57:54 2017 (local time): 4plZLpSfizLjKSBm

2 搭建mysql集群

2.1 配置各主機

2.1.1 xuegod1創建管理節點配置文件

[[email protected] ~]vim /var/lib/mysql-cluster/config.ini #寫入以下內容

[ndbddefault]

NoOfReplicas=2 #數據寫入數量。2表示兩份

DataMemory=200M #配置數據存儲可使用的內存

IndexMemory=100M #索引給100M

[ndb_mgmd]

id=1

datadir=/var/lib/mysql-cluster #管理結點的日誌

HostName=192.168.10.31 #管理結點的IP地址。本機IP

######data node options: #存儲結點

[ndbd]

HostName=192.168.10.32

DataDir=/var/lib/mysql #mysql數據存儲路徑

id=2

[ndbd]

HostName=192.168.10.33

DataDir=/var/lib/mysql #mysql數據存儲路徑

id=3

# SQLnode options: #關於SQL結點

[mysqld]

HostName=192.168.10.34

id=4

[mysqld]

HostName=192.168.10.35

id=5

在這個文件裏,我們分別給五個節點分配了ID,這有利於更好的管理和區分各個節點。當然,要是不指定,MySQL也會動態分配一個

2.1.2 xuegod2數據節點

[[email protected] /]# vim /etc/my.cnf #xuegod3配置一樣

[mysqld]

datadir=/var/lib/mysql #mysql數據存儲路徑

ndbcluster #啟動ndb引擎

ndb-connectstring=192.168.10.31 # 管理節點IP地址

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

[mysql_cluster]

ndb-connectstring=192.168.10.31 #管理節點IP地址

2.1.3 SQL節點配置

[[email protected] ~]# vim /etc/my.cnf #xuegod5配置一樣

[mysqld]

ndbcluster #啟動ndb引擎

ndb-connectstring=192.168.10.31 #管理節點IP地址

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

[mysql_cluster]

ndb-connectstring=192.168.10.31 #管理節點IP都中

說明:數據節點和SQL節點配置文件區別,就多一行,數據節點有: datadir=/var/lib/mysql #mysql數據的存儲路徑,SQL節點上沒有。

2.2 MySQL Cluster啟動

初次啟動命令以及用戶密碼更改調整:(嚴格按照次序啟動)

先啟動:管理節點服務-->數據節點服務-->SQL節點服務

關閉:關閉管理節點服務,關閉管理節點服務後,nbdb數據節點服務會自動關閉-->手動把sql節點服務管理。

執行初次啟動前請先確認 將兩臺機器的防火墻關閉(service iptables stop 或者 設定 防火墻端口可通,兩個端口即通訊端口1186、數據端口3306 )

2.2.1 xuegod1上啟動管理節點命令

[[email protected] ~]# ndb_mgmd -f /var/lib/mysql-cluster/config.ini # mysql cluster 後臺運行進程

#盡量不要把管理結點、 數據結點、存儲結點 配置在同一臺機子上,否則一個掛了,就全掛了。

查看端口號:

[[email protected] ~]# netstat -anput|grep 1186

tcp 0 0 0.0.0.0:1186 0.0.0.0:* LISTEN 2380/ndb_mgmd

tcp 0 0 127.0.0.1:1186 127.0.0.1:49343 ESTABLISHED 2380/ndb_mgmd

tcp 0 0 192.168.10.31:1186 192.168.10.33:48587 ESTABLISHED 2380/ndb_mgmd

tcp 0 0 192.168.10.31:1186 192.168.10.32:35434 ESTABLISHED 2380/ndb_mgmd

tcp 0 0 192.168.10.31:1186 192.168.10.33:48586 ESTABLISHED 2380/ndb_mgmd

tcp 0 0 192.168.10.31:1186 192.168.10.32:35435 ESTABLISHED 2380/ndb_mgmd

tcp 0 0 127.0.0.1:49343 127.0.0.1:1186 ESTABLISHED 2380/ndb_mgmd

2.2.2 xuegod2和xuegod3啟動數據節點服務

[[email protected] ~]# ndbd --initial

2017-05-10 09:34:21 [ndbd] INFO -- Angel connected to ‘192.168.10.31:1186‘

2017-05-10 09:34:21 [ndbd] INFO -- Angel allocated nodeid: 2

[[email protected] ~]# ndbd --initial

2017-05-10 05:54:38 [ndbd] INFO -- Angel connected to ‘192.168.10.31:1186‘

2017-05-10 05:54:38 [ndbd] INFO -- Angel allocated nodeid: 3

2.2.3 xuegod4和xuegod5啟動SQL結點服務

[[email protected] ~]# mysqld_safe --defaults-file=/etc/my.cnf &

[[email protected] ~]# mysqld_safe --defaults-file=/etc/my.cnf &

查看mysql集群狀態:

[[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 @192.168.10.32 (mysql-5.6.29 ndb-7.4.11, Nodegroup: 0, *)

id=3 @192.168.10.33 (mysql-5.6.29 ndb-7.4.11, Nodegroup: 0)

[ndb_mgmd(MGM)] 1 node(s)

id=1 @192.168.10.31 (mysql-5.6.29 ndb-7.4.11)

[mysqld(API)] 2 node(s)

id=4 @192.168.10.34 (mysql-5.6.29 ndb-7.4.11)

id=5 @192.168.10.35 (mysql-5.6.29 ndb-7.4.11)

2.3 數據同步

因為默認密碼比較坑人,我們就需要在此之前改一下兩臺機器mysql的密碼。

2.3.1 xuegod5修改mysql root密碼

[[email protected] ~]# cat /root/.mysql_secret

# The random password set for the root user at Wed May 24 02:31:53 2017 (local time): Imzb3KPGbjHYxIAl

[[email protected] ~]# mysql -uroot -pImzb3KPGbjHYxIAl

mysql> show database;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘database‘ at line 1

mysql> set password=password(‘123456‘);

Query OK, 0 rows affected (0.01 sec)

mysql> exit;

Bye

[[email protected] ~]# mysql -uroot -p123456 #測試登錄

mysql> grant all privileges on *.* to cluster@"%" identified by "123456"; #授權

Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

2.3.2 xuegod4修改mysql root密碼

[[email protected] ~]# cat /root/.mysql_secret

# The random password set for the root user at Wed May 24 03:08:52 2017 (local time): 4r4jBIWfcedp753c

[[email protected] ~]# mysql -uroot -p4r4jBIWfcedp753c

mysql> set password=password(‘123456‘);

Query OK, 0 rows affected (0.00 sec)

mysql> exit

Bye

[[email protected] ~]# mysql -uroot -p123456

mysql> grant all privileges on *.* to cluster@"%" identified by "123456";

Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec

3 測試

模擬外部機器的一個客戶端插入數據

[[email protected] ~]# mysql -ucluster -p123456 -h 192.168.10.34

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| ndbinfo |

| performance_schema |

| test |

+--------------------+

5 rows in set (0.00 sec)

mysql> create database db;

Query OK, 1 row affected (0.12 sec)

mysql> use db;

Database changed

mysql> create table test(id int) engine=ndb;

Query OK, 0 rows affected (0.34 sec)

mysql> insert into test values(1000);

Query OK, 1 row affected (0.02 sec)

mysql> select * from test;

+------+

| id |

+------+

| 1000 |

+------+

1 row in set (0.01 sec)

登錄另一臺sql節點查看

[[email protected] ~]# mysql -ucluster -p123456 -h 192.168.10.35

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| db |

| mysql |

| ndbinfo |

| performance_schema |

| test |

+--------------------+

6 rows in set (0.02 sec)

mysql> use db;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> select * from test;

+------+

| id |

+------+

| 1000 |

+------+

1 row in set (0.01 sec)

停掉一個節點測試:

[[email protected] ~]# mysqladmin -uroot -p123456 shutdown

ndb_mgm> show

Cluster Configuration

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

[ndbd(NDB)] 2 node(s)

id=2 @192.168.10.32 (mysql-5.6.29 ndb-7.4.11, Nodegroup: 0, *)

id=3 @192.168.10.33 (mysql-5.6.29 ndb-7.4.11, Nodegroup: 0)

[ndb_mgmd(MGM)] 1 node(s)

id=1 @192.168.10.31 (mysql-5.6.29 ndb-7.4.11)

[mysqld(API)] 2 node(s)

id=4 @192.168.10.34 (mysql-5.6.29 ndb-7.4.11)

id=5 (not connected, accepting connect from 192.168.10.35)

再啟動節點

[[email protected] ~]# mysqld_safe --defaults-file=/etc/my.cnf &

ndb_mgm> show

Cluster Configuration

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

[ndbd(NDB)] 2 node(s)

id=2 @192.168.10.32 (mysql-5.6.29 ndb-7.4.11, Nodegroup: 0, *)

id=3 @192.168.10.33 (mysql-5.6.29 ndb-7.4.11, Nodegroup: 0)

[ndb_mgmd(MGM)] 1 node(s)

id=1 @192.168.10.31 (mysql-5.6.29 ndb-7.4.11)

[mysqld(API)] 2 node(s)

id=4 @192.168.10.34 (mysql-5.6.29 ndb-7.4.11)

id=5 @192.168.10.35 (mysql-5.6.29 ndb-7.4.11)

4 關閉服務

關閉mysql集群順序: 關閉管理節點服務-》 關閉管理節點時,數據結點服務自動關閉 –》 需要手動關閉SQL結點服務

關閉管理節點:

[[email protected] ~]# ndb_mgm

-- NDB Cluster -- Management Client --

ndb_mgm> shutdown

Connected to Management Server at: localhost:1186

Node 3: Cluster shutdown initiated

Node 2: Cluster shutdown initiated

Node 3: Node shutdown completed.

Node 2: Node shutdown completed.

3 NDB Cluster node(s) have shutdown.

Disconnecting to allow management server to shutdown.

ndb_mgm> exit

[[email protected] ~]# ps -aux|grep ndbd #查看不到,說明數據節點已經被關

Warning: bad syntax, perhaps a bogus ‘-‘? See /usr/share/doc/procps-3.2.8/FAQ

root 2476 0.0 0.0 103316 836 pts/1 S+ 22:06 0:00 grep ndbd

手動關閉SQL節點服務

[[email protected] ~]# mysqladmin -uroot -p123456 shutdown

[[email protected] ~]# mysqladmin -uroot -p123456 shutdown

或方法二kill掉

[[email protected] ~]# ps -axu|grep mysql

Warning: bad syntax, perhaps a bogus ‘-‘? See /usr/share/doc/procps-3.2.8/FAQ

root 4142 0.0 0.1 106244 1392 pts/0 S 08:22 0:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/etc/my.cnf

mysql 4253 2.8 44.6 951048 448728 pts/0 Sl 08:22 0:00 /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 4293 0.0 0.0 103316 836 pts/2 S+ 08:23 0:00 grep mysql

[[email protected] ~]# kill -9 4142

[[email protected] ~]# kill -9 4253

[[email protected] ~]# ps -aux|grep mysql

Warning: bad syntax, perhaps a bogus ‘-‘? See /usr/share/doc/procps-3.2.8/FAQ

root 4306 0.0 0.1 106244 1392 pts/2 S 07:46 0:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/etc/my.cnf

mysql 4417 0.5 44.8 1279896 450652 pts/2 Sl 07:46 0:00 /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 4446 0.0 0.0 103316 836 pts/1 S+ 07:47 0:00 grep mysql

[[email protected] ~]# kill -9 4306

[[email protected] ~]# kill -9 4417

5 總結

再次啟動,mysql集群啟動

[[email protected] ~]# ndb_mgmd -f /var/lib/mysql-cluster/config.ini

[[email protected] ~]# ndbd

[[email protected] ~]# ndbd

[[email protected] ~]# mysqld_safe --defaults-file=/etc/my.cnf &

[[email protected]~]# mysqld_safe --defaults-file=/etc/my.cnf &

查看mysql集群狀態:

[[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 @192.168.10.32 (mysql-5.6.29 ndb-7.4.11, Nodegroup: 0, *)

id=3 @192.168.10.33 (mysql-5.6.29 ndb-7.4.11, Nodegroup: 0)

[ndb_mgmd(MGM)] 1 node(s)

id=1 @192.168.10.31 (mysql-5.6.29 ndb-7.4.11)

[mysqld(API)] 2 node(s)

id=4 @192.168.10.34 (mysql-5.6.29 ndb-7.4.11)

id=5 @192.168.10.35 (mysql-5.6.29 ndb-7.4.11)

ndb_mgm>

2-18-搭建mysql集群實現高可用