環境
rhel6.5x64
192.168.20.11 mysql11
192.168.29.12 mysql12
拓撲
環境
關閉iptables
關閉selinux
yum -y install ncurses-devel gcc gcc-c++make autoconf automake zlib zlib-devel openssl openssl-devel pcre-devel
開始安裝配置mysql11
[root@mysql11 ~]# tar -xf MySQL-Cluster-gpl-7.3.7-1.el6.x86_64.rpm-bundle.tar [root@mysql11 ~]# scpMySQL-Cluster-gpl-7.3.7-1.el6.x86_64.rpm-bundle.tar [email protected]:/root [root@mysql11 ~]# yum -y remove mysql* [root@mysql11 ~]# mkdir /var/lib/mysql [root@mysql11 ~]# mkdir /var/lib/mysql-cluster [root@mysql11 ~]# mkdir /var/run/mysqld [root@mysql11 ~]# rpm -ivhMySQL-Cluster-server-gpl-7.3.7-1.el6.x86_64.rpm [root@mysql11 ~]# id mysql uid=498(mysql) gid=498(mysql)groups=498(mysql) [root@mysql11 ~]# chown mysql:mysql -R/var/lib/mysql [root@mysql11 ~]# chown mysql:mysql -R/var/lib/mysql-cluster [root@mysql11 ~]# chown mysql:mysql -R/var/run/mysqld/
配置mysql12
[root@mysql12 ~]# yum remove mysql -y [root@mysql12 ~]# rpm -qa | grep mysql* mysql-libs-5.1.71-1.el6.x86_64 [root@mysql12 ~]# rpm -e --nodepsmysql-libs-5.1.71-1.el6.x86_64
其實我覺得/var/lib/mysql /var/lib/mysql-cluster/var/run/mysqld這三個路徑會自動生成
來試試
[root@mysql12 ~]# rpm -ivhMySQL-Cluster-server-gpl-7.3.7-1.el6.x86_64.rpm
看看,這裏有了
[root@mysql12 ~]# ls /var/lib/mysql ibdata1 ib_logfile1 ndbinfo RPM_UPGRADE_HISTORY test ib_logfile0 mysql performance_schema RPM_UPGRADE_MARKER-LAST [root@mysql12 ~]# ls/var/lib/mysql-cluster/ [root@mysql12 ~]# mkdir/var/lib/mysql-cluster mkdir: cannot create directory`/var/lib/mysql-cluster': File exists [root@mysql12 ~]# id mysql uid=498(mysql) gid=498(mysql)groups=498(mysql) [root@mysql12 ~]# ls /var/run/mysqld [root@mysql12 ~]# mkdir /var/run/mysqld mkdir: cannot create directory`/var/run/mysqld': File exists [root@mysql12 ~]# /var/run/mysqld -bash: /var/run/mysqld: is a directory [root@mysql12 ~]# ll -d /var/run/mysqld drwxr-xr-x 2 root root 4096 Aug 27 23:29/var/run/mysqld [root@mysql12 ~]# ll -d/var/lib/mysql-cluster drwxr-xr-x 2 root root 4096 Aug 27 23:29/var/lib/mysql-cluster [root@mysql12 ~]# ll -d /var/lib/mysql drwxr-xr-x 6 mysql mysql 4096 Aug 27 23:53/var/lib/mysql [root@mysql12 ~]#
改成mysql用戶所有吧
[root@mysql12 ~]# chown mysql:mysql -R/var/lib/mysql [root@mysql12 ~]# chown mysql:mysql -R/var/lib/mysql-cluster [root@mysql12 ~]# chown mysql:mysql -R/var/run/mysqld/
配置mysql11成爲管理節點
主要是兩個命令:ndb_mgmd 和 ndb_mgm
[root@mysql11 ~]# which ndb_mgm /usr/bin/ndb_mgm [root@mysql11 ~]# rpm -qf /usr/bin/ndb_mgm MySQL-Cluster-server-gpl-7.3.7-1.el6.x86_64 [root@mysql11 ~]# which ndb_mgmd /usr/sbin/ndb_mgmd [root@mysql11 ~]# rpm -qf/usr/sbin/ndb_mgmd MySQL-Cluster-server-gpl-7.3.7-1.el6.x86_64
生成配置文件
[root@mysql11 ~]# cd /var/lib/mysql-cluster [root@mysql11 mysql-cluster]# vimconfig.ini
#配置文件的內容:
[ndbd default]
NoOfReplicas=2 #數據寫入數量。2 表示兩份,這樣當其中一個存儲節點壞了,另一個還可以正常使用。
DataMemory=200M #配置數據存儲可以使用的內存
IndexMemory=100M #配置緩存索引可以使用的內存
[ndb_mgmd]
id=1
datadir=/var/lib/mysql-cluster # 指定存放管理結點的日誌
HostName=192.168.20.11 #管理結點的 IP 地址。本機 IP
###### data node options: #存儲結點
[ndbd]
HostName=192.168.20.11
DataDir=/var/lib/mysql #mysql 數據存儲路徑
id=2
[ndbd]
HostName=192.168.20.12
DataDir=/var/lib/mysql
id=3
# SQL node options: #關於 SQL 結點
[mysqld]
HostName=192.168.20.11 # 指定可以連接 ndbd 引擎的 sql 結點。
id=4
[mysqld]
HostName=192.168.20.12 # 指定可以連接 ndbd 引擎的 sql 結點。
id=5
mysql11數據節點+SQL 節點配置文檔:
配置的內容要手動寫呢!
[root@mysql11 ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql #mysql數據存儲路徑[單獨的SQL節點不需要這行]
ndbcluster #啓動 ndb 引擎
ndb-connectstring=192.168.20.11 # 管理節點 IP 地址
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[mysql_cluster]
ndb-connectstring=192.168.20.11 # 管理節點 IP 地址
再裝個客戶端吧
[root@mysql11 ~]# rpm -ivhMySQL-Cluster-client-gpl-7.3.7-1.el6.x86_64.rpm
mysql12數據節點+SQL 節點配置文檔
客戶端裝上
[root@mysql12 ~]# rpm -ivhMySQL-Cluster-client-gpl-7.3.7-1.el6.x86_64.rpm
把mysql11上 的配置文件弄過來吧
[root@mysql12 ~]# scp [email protected]:/etc/my.cnf /etc/my.cnf
SQL Cluster 初次啓動命令以及用戶密碼更改調整:(請嚴格按照次序啓動)
啓動 mysql cluster
先啓動順序:管理結點服務 需要手動啓動 ndbd 數據結點服務-》sql 結點服務
關閉順序: 關閉管理結點服務 關閉管理結點服務後,nbdb 數據結點服務會自動關閉-》手動把 sql結點服務關了。
執行初次啓動前請先確認將兩臺機器的防火牆關閉(service iptables stop 或者設定防火牆端口可通,兩個端口即:通訊端口 1186、數據端口 3306 )
啓動管理結點命令:
[root@mysql11 ~]# ndb_mgmd -f/var/lib/mysql-cluster/config.ini MySQL Cluster Management Server mysql-5.6.21ndb-7.3.7 2016-08-28 00:28:34 [MgmtSrvr] INFO -- The default config directory'/usr/mysql-cluster' does not exist. Trying to create it... 2016-08-28 00:28:34 [MgmtSrvr] INFO -- Sucessfully created config directory 2016-08-28 00:28:34 [MgmtSrvr] WARNING -- at line 7: [MGM] id is deprecated, useNodeId instead 2016-08-28 00:28:34 [MgmtSrvr] WARNING -- at line 16: [DB] id is deprecated, useNodeId instead 2016-08-28 00:28:34 [MgmtSrvr] WARNING -- at line 21: [DB] id is deprecated, use NodeIdinstead 2016-08-28 00:28:34 [MgmtSrvr] WARNING -- at line 26: [API] id is deprecated, useNodeId instead 2016-08-28 00:28:34 [MgmtSrvr] WARNING -- at line 30: [API] id is deprecated, useNodeId instead 2016-08-28 00:28:34 [MgmtSrvr] WARNING -- at line 30: Cluster configuration warning: arbitrator with id 1 and db node with id 2 on same host 192.168.20.11 Running arbitrator on the same host as a database node may cause complete cluster shutdown in case of host failure. 2016-08-28 00:28:34 [MgmtSrvr] WARNING -- at line 7: [MGM] id is deprecated, useNodeId instead 2016-08-28 00:28:34 [MgmtSrvr] WARNING -- at line 16: [DB] id is deprecated, useNodeId instead 2016-08-28 00:28:34 [MgmtSrvr] WARNING -- at line 21: [DB] id is deprecated, use NodeIdinstead 2016-08-28 00:28:34 [MgmtSrvr] WARNING -- at line 26: [API] id is deprecated, useNodeId instead 2016-08-28 00:28:34 [MgmtSrvr] WARNING -- at line 30: [API] id is deprecated, useNodeId instead 2016-08-28 00:28:34 [MgmtSrvr] WARNING -- at line 30: Cluster configuration warning: arbitrator with id 1 and db node with id 2 on same host 192.168.20.11 Running arbitrator on the same host as a database node may cause complete cluster shutdown in case of host failure. [root@mysql11 ~]#
#說明:仲裁員 (ID1) 和 DB(ID2)節點上同一主機192.168.20.11
在同一臺主機作爲數據庫節點上運行仲裁人
造成完整的集羣關機的情況下主機發生故障。雖然報了一些警告,但是,我們已經執行成功了。
查看端口號:
[root@mysql11 ~]# netstat -anptu | grep 1186 tcp 0 0 0.0.0.0:1186 0.0.0.0:* LISTEN 2084/ndb_mgmd tcp 0 0 127.0.0.1:1186 127.0.0.1:59216 ESTABLISHED 2084/ndb_mgmd tcp 0 0 127.0.0.1:59216 127.0.0.1:1186 ESTABLISHED 2084/ndb_mgmd [root@mysql11 ~]#
查看日誌存儲位置:
[root@mysql11 ~]# ll/var/lib/mysql-cluster/ total 16 -rw-r--r-- 1 root root 401 Aug 28 00:11config.ini -rw-r--r-- 1 root root 699 Aug 28 00:28ndb_1_cluster.log -rw-r--r-- 1 root root 26 Aug 28 00:28 ndb_1_out.log -rw-r--r-- 1 root root 4 Aug 28 00:28 ndb_1.pid [root@mysql11 ~]#
啓動存儲結點服務:
mysql11
[root@mysql11 ~]# ndbd --initial 2016-08-28 00:32:41 [ndbd] INFO -- Angel connected to '192.168.20.11:1186' 2016-08-28 00:32:41 [ndbd] INFO -- Angel allocated nodeid: 2 [root@mysql11 ~]#
mysql12
[root@mysql12 ~]# ndbd --initial 2016-08-28 00:33:09 [ndbd] INFO -- Angel connected to '192.168.20.11:1186' 2016-08-28 00:33:09 [ndbd] INFO -- Angel allocated nodeid: 3 [root@mysql12 ~]#
啓動 SQL 結點服務:
mysql11
[root@mysql11 ~]# mysqld_safe--defaults-file=/etc/my.cnf & [1] 2166 160828 00:33:49mysqld_safe Logging to '/var/log/mysqld.log'. 160828 00:33:50 mysqld_safe Starting mysqlddaemon with databases from /var/lib/mysql
mysql12
[root@mysql12 ~]# mysqld_safe--defaults-file=/etc/my.cnf & [1] 1782 160828 00:34:56mysqld_safe Logging to '/var/log/mysqld.log'. 160828 00:34:56 mysqld_safe Starting mysqlddaemon with databases from /var/lib/mysql
測試:
查看 mysql 集羣狀態:
[root@mysql11 ~]# ndb_mgm -- NDB Cluster -- Management Client -- ndb_mgm> show Connected to Management Server at:192.168.20.11:1186 Cluster Configuration --------------------- [ndbd(NDB)] 2node(s) [email protected] (mysql-5.6.21 ndb-7.3.7, Nodegroup: 0, *) [email protected] (mysql-5.6.21 ndb-7.3.7, Nodegroup: 0) [ndb_mgmd(MGM)] 1 node(s) [email protected] (mysql-5.6.21 ndb-7.3.7) [mysqld(API)] 2 node(s) [email protected] (mysql-5.6.21 ndb-7.3.7) [email protected] (mysql-5.6.21 ndb-7.3.7) ndb_mgm>
測試集羣,數據同步:
mysql11
先查看下默認生成的密碼
[root@mysql11 ~]# cat /root/.mysql_secret # The random password set for the root userat Sat Aug 27 23:31:54 2016 (local time): oPjyozXIA05TSXb4
第一次登錄
[root@mysql11 ~]# mysql -u root -poPjyozXIA05TSXb4 Warning: Using a password on the commandline interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version:5.6.21-ndb-7.3.7-cluster-gpl Copyright (c) 2000, 2014, Oracle and/or itsaffiliates. All rights reserved. Oracle is a registered trademark of OracleCorporation and/or its affiliates. Other names may be trademarksof their respective owners. Type 'help;' or '\h' for help. Type '\c' toclear the current input statement. mysql>
mysql> show databases; #第一使用數據庫時,必須修改密碼
ERROR 1820 (HY000): You must SET PASSWORDbefore executing this statement
mysql>
mysql> set password=password('111111');
Query OK, 0 rows affected (0.12 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| ndb_2_fs |
| ndbinfo |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.10 sec)
mysql>
mysql> exit
Bye
來使用剛纔修改的密碼登錄看
[root@mysql11 ~]# mysql -uroot -p111111
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| ndb_2_fs |
| ndbinfo |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.01 sec)
mysql12
先查看默認密碼
[root@mysql12 ~]# cat /root/.mysql_secret # The random password set for the root userat Sat Aug 27 23:53:13 2016 (local time): MjcJWscCjVpvnfDX
默認密碼登錄以後改密碼
[root@mysql12 ~]# mysql -uroot -pMjcJWscCjVpvnfDX
Warning: Using a password on the commandline interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.21-ndb-7.3.7-cluster-gpl
Copyright (c) 2000, 2014, Oracle and/or itsaffiliates. All rights reserved.
Oracle is a registered trademark of OracleCorporation and/or its
affiliates. Other names may be trademarksof their respective
owners.
Type 'help;' or '\h' for help. Type '\c' toclear the current input statement.
mysql> set password=password('111111');
Query OK, 0 rows affected (0.17 sec)
mysql> exit
Bye
使用修改的密碼登錄
[root@mysql12 ~]# mysql -uroot -p111111
Warning: Using a password on the commandline interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version:5.6.21-ndb-7.3.7-cluster-gpl MySQL Cluster Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or itsaffiliates. All rights reserved.
Oracle is a registered trademark of OracleCorporation and/or its
affiliates. Other names may be trademarksof their respective
owners.
Type 'help;' or '\h' for help. Type '\c' toclear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| ndb_3_fs |
| ndbinfo |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.07 sec)
mysql>
插入數據:
mysql11
[root@mysql11 ~]# mysql -uroot -p111111
mysql> create database abiao;
Query OK, 1 row affected (0.38 sec)
mysql12
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| abiao |
| mysql |
| ndb_3_fs |
| ndbinfo |
| performance_schema |
| test |
+--------------------+
7 rows in set (0.00 sec)
關閉 mysql 集羣順序:
關閉管理節點服務-》關閉管理節點時,數據結點服務自動關閉–》 需要手動關閉 SQL 結點服務
關閉管理節點
[root@mysql11 ~]# ndb_mgm -- NDB Cluster -- Management Client -- ndb_mgm> shutdown Node 2: Cluster shutdown initiated Node 3: Cluster shutdown initiated Node 2: Node shutdown completed. 3 NDB Cluster node(s) have shutdown. Disconnecting to allow management server toshutdown. Node 3: Node shutdown completed. ndb_mgm> exit
看看進程,
[root@mysql11 ~]# ps -axu | grep ndbd Warning: bad syntax, perhaps a bogus '-'?See /usr/share/doc/procps-3.2.8/FAQ root 2382 0.0 0.1 103244 856 pts/1 S+ 00:50 0:00 grep ndbd [root@mysql11 ~]#
已經關掉了節點
手動關閉 SQL 結點服務
mysql11
[root@mysql11 ~]# ps -axu | grep mysql Warning: bad syntax, perhaps a bogus '-'?See /usr/share/doc/procps-3.2.8/FAQ root 2166 0.0 0.0 106196 192 pts/0 S 00:33 0:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/etc/my.cnf mysql 2291 1.4 77.8 1364648 504800pxs/0 Sl 00:33 0:15 /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 2359 0.0 0.5 125800 2748 pts/2 S+ 00:39 0:00 mysql -uroot -px xxxx root 2387 0.0 0.1 103244 860 pts/1 S+ 00:52 0:00 grep mysql [root@mysql11 ~]# kill -9 2166 [root@mysql11 ~]# kill -9 2166 [root@mysql11 ~]# ps -axu | grep mysql Warning: bad syntax, perhaps a bogus '-'?See /usr/share/doc/procps-3.2.8/FAQ root 2359 0.0 0.5 125800 2748 pts/2 S+ 00:39 0:00 mysql -uroot -px xxxx root 2394 0.0 0.1 103244 860 pts/1 S+ 00:54 0:00 grep mysql
mysql12
[root@mysql12 ~]# ps -axu | grep mysql Warning: bad syntax, perhaps a bogus '-'?See /usr/share/doc/procps-3.2.8/FAQ root 1782 0.0 0.0 106196 20 pts/0 S 00:34 0:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/etc/my.cnf mysql 1907 1.6 46.6 1364288 302192pxs/0 Sl 00:34 0:20 /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 1978 1.0 0.1 103248 860 pts/1 S+ 00:54 0:00 grep mysql [root@mysql12 ~]# kill -9 1782 [root@mysql12 ~]# kill -9 1907 [root@mysql12 ~]# ps -axu | grep mysql Warning: bad syntax, perhaps a bogus '-'?See /usr/share/doc/procps-3.2.8/FAQ root 1981 0.0 0.1 103248 860 pts/1 S+ 00:55 0:00 grep mysql [root@mysql12 ~]#
因爲 是筆記,所以全篇都是代碼