Mysql Cluster 的搭建
環境RHEL 6.10 + mysql-cluster-gpl-7.2.35-linux-glibc2.12-x86_64.tar.gz
軟體可以在下面下載
http://mirrors.sohu.com/mysql/MySQL-Cluster-7.2/
http://mirrors.163.com/mysql/Downloads/
管理節點 rac1
192.168.54.96
資料節點 host01 host02
192.168.54.161 / 162
--
-- 管理節點安裝
groupadd mysql useradd mysql -g mysql --mv XXX /usr/local --cd /usr/local tar zxvf *.tar.gz -C /usr/local/ mv * mysql chown -R mysql:mysql mysql cd mysql scripts/mysql_install_db --user=mysql -- 一定要這樣安裝 ,否則會出錯
[[email protected] scripts]# ./mysql_install_db --user=mysql
FATAL ERROR: Could not find ./bin/my_print_defaults
If you compiled from source, you need to run 'make install' to
copy the software into the correct location ready for operation.
If you are using a binary release, you must either be at the top
level of the extracted archive, or pass the --basedir option
pointing to that location.
[ [email protected] mysql]# ./scripts/mysql_install_db --user=mysql
Installing MySQL system tables...
181106 20:20:38 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
181106 20:20:38 [Note] ./bin/mysqld (mysqld 5.5.62-ndb-7.2.35-cluster-gpl) starting as process 5185 ...
OK
Filling help tables...
181106 20:20:38 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
181106 20:20:38 [Note] ./bin/mysqld (mysqld 5.5.62-ndb-7.2.35-cluster-gpl) starting as process 5192 ...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
./bin/mysqladmin -u root password 'new-password'
./bin/mysqladmin -u root -h rac1 password 'new-password'
Alternatively you can run:
./bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd . ; ./bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd ./mysql-test ; perl mysql-test-run.pl
Please report any problems at http://bugs.mysql.com/
[ [email protected] mysql]#
-- 配置管理節點
mkdir /var/lib/mysql-cluster
cd /var/lib/mysql-cluster
vi config.ini
[ndbd default]
noOfReplicas=2
dataMemory=80M
IndexMemory=18M
[ndb_mgmd]
NodeId=1
Hostname=192.168.54.96 //管理IP
datadir=/usr/local/mysql/logs
[ndbd]
NodeId=2
Hostname=192.168.54.161 //資料節點1
datadir=/usr/local/mysql/data/ //資料檔案位置
[ndbd]
NodeId=3
Hostname=192.168.54.162 //資料節點2
datadir=/usr/local/mysql/data/ //資料檔案位置
[MYSQLD]
[MYSQLD]
-- 資料節點安裝
groupadd mysql
useradd mysql -g mysql
mv * /usr/local
cd /usr/local
tar *.tar.gz
mv X mysql
chown -R mysql:mysql mysql
cd mysql
scripts/mysql_install_db --user=mysql
cp support-files/mysql.server /etc/init.d/mysqld
[[email protected] mysql]# scripts/mysql_install_db --user=mysql
Installing MySQL system tables...
181106 20:50:02 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
181106 20:50:02 [Note] ./bin/mysqld (mysqld 5.5.62-ndb-7.2.35-cluster-gpl) starting as process 4479 ...
OK
Filling help tables...
181106 20:50:02 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
181106 20:50:02 [Note] ./bin/mysqld (mysqld 5.5.62-ndb-7.2.35-cluster-gpl) starting as process 4486 ...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
./bin/mysqladmin -u root password 'new-password'
./bin/mysqladmin -u root -h host01 password 'new-password'
Alternatively you can run:
./bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd . ; ./bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd ./mysql-test ; perl mysql-test-run.pl
Please report any problems at http://bugs.mysql.com/
[[email protected] mysql]#
-- 配置資料節點
vi /etc/my.cnf
[mysqld]
ndbcluster
ndb-connectstring=192.168.54.96
[mysql_cluster]
ndb-connectstring=192.168.54.96
--啟動管理節點服務 注意,只是在第一次啟動或在備份/恢復或配置變化後重啟ndbd時候,才加-initial引數
/usr/local/mysql/bin/ndb_mgmd -f /var/lib/mysql-cluster/config.ini --initial
netstat -lntpu -- 看到1186說明啟動正常
[[email protected] bin]# /usr/local/mysql/bin/ndb_mgmd -f /var/lib/mysql-cluster/config.ini --initial
MySQL Cluster Management Server mysql-5.5.62 ndb-7.2.35
[[email protected] bin]#
[[email protected] bin]# netstat -lntpu | grep 1186
tcp 0 0 0.0.0.0:1186 0.0.0.0:* LISTEN 22354/ndb_mgmd
[[email protected] bin]#
-- 啟動資料節點
/usr/local/mysql/bin/ndbd --initial
[[email protected] mysql]# /usr/local/mysql/bin/ndbd --initial
2018-11-06 21:09:04 [ndbd] INFO -- Angel connected to '192.168.54.96:1186'
2018-11-06 21:09:04 [ndbd] INFO -- Angel allocated nodeid: 2
[[email protected] mysql]#
[[email protected] mysql]# /usr/local/mysql/bin/ndbd --initial
2018-11-06 21:09:14 [ndbd] INFO -- Angel connected to '192.168.54.96:1186'
2018-11-06 21:09:14 [ndbd] INFO -- Angel allocated nodeid: 3
[[email protected] mysql]#
-- 啟動sql節點
service mysqld start
[[email protected] mysql]# service mysqld start
Starting MySQL... [ OK ]
[[email protected] mysql]#
[[email protected] mysql]# service mysqld start
Starting MySQL.... [ OK ]
[[email protected] mysql]#
-- 在管理節點上檢視服務狀態
/usr/local/mysql/bin/ndb_mgm
show
[[email protected] bin]# /usr/local/mysql/bin/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.54.161 (mysql-5.5.62 ndb-7.2.35, Nodegroup: 0, *)
id=3 @192.168.54.162 (mysql-5.5.62 ndb-7.2.35, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.54.96 (mysql-5.5.62 ndb-7.2.35)
[mysqld(API)] 2 node(s)
id=4 @192.168.54.161 (mysql-5.5.62 ndb-7.2.35)
id=5 @192.168.54.162 (mysql-5.5.62 ndb-7.2.35)
ndb_mgm>
-- 測試,在節點1 192.168.54.161上建立表,節點2上是可以看到建立的表的,主要建表的時候引擎 ndb
/usr/local/mysql/bin/mysql -uroot -p
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
[[email protected] mysql]# /usr/local/mysql/bin/mysql -uroot -p --socket=/var/lib/mysql/mysql.sock
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[[email protected] mysql]#
-- 解決方法
停止mysqld 服務
/usr/local/mysql/bin/mysqld_safe --skip-grant-tables &
service mysqld start
然後root登陸,回車就可以了
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| ndbinfo |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql>
mysql> create database bb; -- 在兩個節點上都可以看到bb庫
Query OK, 1 row affected (0.11 sec)
-- 注意,建表的時候,要指定資料庫表引擎為ndb
mysql> show tables;
+--------------+
| Tables_in_bb |
+--------------+
| t1_bb |
| t2_bb |
| t3_bb |
+--------------+
3 rows in set (0.00 sec)
mysql>
mysql> show tables;
+--------------+
| Tables_in_bb |
+--------------+
| t3_bb |
+--------------+
1 row in set (0.01 sec)
mysql>
mysql> show create table t2_bb;
+-------+-------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------+
| t2_bb | CREATE TABLE `t2_bb` (
`id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show create table t3_bb;
+-------+-----------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-----------------------------------------------------------------------------------------------+
| t3_bb | CREATE TABLE `t3_bb` (
`id` int(11) DEFAULT NULL
) ENGINE=ndbcluster DEFAULT CHARSET=latin1 |
+-------+-----------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
-- 測試,關閉掉一個節點 ,在其中一個節點上操作,再開啟另一個節點,看是否可以同步過去
-- 關閉掉節點1 ,在節點2上插入記錄
mysql> select * from t3_bb;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> insert into t3_bb values(2);
Query OK, 1 row affected (0.01 sec)
mysql> select * from t3_bb;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
mysql>
-- 在節點1上檢視 ,發現數據同步過來了
mysql> select * from t3_bb;
+------+
| id |
+------+
| 2 |
| 1 |
+------+
2 rows in set (0.00 sec)
mysql>
-- 關閉叢集 ,先關閉管理節點,再關閉資料節點
/usr/local/mysql/bin/ndb_mgm -e shutdown
ndb_mgm> shutdown
Node 2: Cluster shutdown initiated
Node 3: 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>
ndb_mgm>
-- 這個時候,在節點上查詢會出錯 ,但是對非NDB引擎的表進行操作,是可以的 。
mysql> select * from t3_bb;
ERROR 1296 (HY000): Got error 157 'Unknown error code' from NDBCLUSTER
mysql>
mysql> select * from t3_bb;
ERROR 1296 (HY000): Got error 157 'Unknown error code' from NDBCLUSTER
mysql> show tables;
+--------------+
| Tables_in_bb |
+--------------+
| t1_bb |
| t2_bb |
| t3_bb |
+--------------+
3 rows in set, 1 warning (0.00 sec)
mysql> select * from t2_bb;
Empty set (0.00 sec)
mysql> insert into t2_bb values(1);
Query OK, 1 row affected (0.01 sec)
mysql>
-- 叢集的啟動
先啟動管理節點,再啟動資料節點 ,再啟動SQL節點
[[email protected] bin]# /usr/local/mysql/bin/ndb_mgmd -f /var/lib/mysql-cluster/config.ini
MySQL Cluster Management Server mysql-5.5.62 ndb-7.2.35
[[email protected] bin]#
[[email protected] bin]# /usr/local/mysql/bin/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.54.161)
id=3 (not connected, accepting connect from 192.168.54.162)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.54.96 (mysql-5.5.62 ndb-7.2.35)
[mysqld(API)] 2 node(s)
id=4 (not connected, accepting connect from any host)
id=5 (not connected, accepting connect from any host)
ndb_mgm>
再啟動資料節點
/usr/local/mysql/bin/ndbd
再啟動SQL節點
[[email protected] mysql]# service mysqld start
Starting MySQL.................................... [ OK ]
[[email protected] mysql]#
[[email protected] ~]# service mysqld start
Starting MySQL.................................... [ OK ]
[[email protected] ~]#
-- 啟動完畢後,檢視狀態
[[email protected] bin]# /usr/local/mysql/bin/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.54.161 (mysql-5.5.62 ndb-7.2.35, Nodegroup: 0, *)
id=3 @192.168.54.162 (mysql-5.5.62 ndb-7.2.35, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.54.96 (mysql-5.5.62 ndb-7.2.35)
[mysqld(API)] 2 node(s)
id=4 @192.168.54.161 (mysql-5.5.62 ndb-7.2.35)
id=5 @192.168.54.162 (mysql-5.5.62 ndb-7.2.35)
ndb_mgm>
END