Mysql Cluster 的搭建

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xxzhaobb/article/details/83822848

环境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      -- 一定要这样安装 ,否则会出错 
[root@rac1 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. 

[root@rac1 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/

[root@rac1 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 
[root@host01 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/

[root@host01 mysql]# 

-- 配置数据节点

vi /etc/my.cnf 

[mysqld]
ndbcluster
ndb-connectstring=192.168.54.96

[mysql_cluster]
ndb-connectstring=192.168.54.96

--启动管理节点服务 注意,只是在第一次启动或在备份/恢复或配置变化后重启ndbd时候,才加-initial参数

扫描二维码关注公众号,回复: 4090198 查看本文章
/usr/local/mysql/bin/ndb_mgmd -f /var/lib/mysql-cluster/config.ini --initial

netstat -lntpu      -- 看到1186说明启动正常

[root@rac1 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
[root@rac1 bin]# 

[root@rac1 bin]# netstat -lntpu | grep 1186
tcp        0      0 0.0.0.0:1186                0.0.0.0:*                   LISTEN      22354/ndb_mgmd      
[root@rac1 bin]# 

-- 启动数据节点

/usr/local/mysql/bin/ndbd --initial 

[root@host01 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
[root@host01 mysql]# 


[root@host02 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
[root@host02 mysql]# 

-- 启动sql节点

service mysqld start 

[root@host01 mysql]# service mysqld start
Starting MySQL...                                          [  OK  ]
[root@host01 mysql]# 

[root@host02 mysql]# service mysqld start
Starting MySQL....                                         [  OK  ]
[root@host02 mysql]# 

-- 在管理节点上查看服务状态
/usr/local/mysql/bin/ndb_mgm
show

[root@rac1 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)

[root@host01 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)
[root@host01 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节点

[root@rac1 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
[root@rac1 bin]# 

[root@rac1 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节点 
[root@host01 mysql]# service mysqld start
Starting MySQL....................................         [  OK  ]
[root@host01 mysql]# 

[root@host02 ~]# service mysqld start
Starting MySQL....................................         [  OK  ]
[root@host02 ~]# 

-- 启动完毕后,查看状态

[root@rac1 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

猜你喜欢

转载自blog.csdn.net/xxzhaobb/article/details/83822848