我准备了三台机子,规划如下:
数据库管理节点:192.168.186.128
数据库数据节点:192.168.186.131、192.168.186.132
数据库 SQL 节点:192.168.186.131、192.168.186.132
以 192.168.186.128 为例,进行安装:
[root@ricenos_128 softwares]# groupadd mysql
[root@ricenos_128 softwares]# useradd -g mysql mysql
[root@ricenos_128 softwares]# tar xvf MySQL-Cluster-gpl-7.4.11-1.rhel5.i386.rpm-bundle.tar
[root@ricenos_128 softwares]# rpm -ivh MySQL-Cluster-server-gpl-7.4.11-1.rhel5.i386.rpm MySQL-Cluster-client-gpl-7.4.11-1.rhel5.i386.rpm
[root@ricenos_128 softwares]# ln -s /usr/sbin/ndbd /usr/bin/
[root@ricenos_128 softwares]# ln -s /usr/sbin/ndb_mgmd /usr/bin/
[root@ricenos_128 softwares]# ln -s /usr/sbin/mysqld /usr/bin/
[root@ricenos_128 softwares]# mkdir -pv /var/lib/mysql/data
[root@ricenos_128 softwares]# chown -R mysql:mysql /var/lib/mysql
针对管理节点【192.168.186.128】:
[root@ricenos_128 softwares]# mkdir -pv /var/lib/mysql-cluster
[root@ricenos_128 softwares]# vi /var/lib/mysql-cluster/config.ini
内容如下:
[ndbd default]
# 每个数据节点的镜像数量
NoOfReplicas=2
# 每个数据节点中给数据分配的内存
DataMemory=644245094
# 每个数据节点中给索引分配的内存
IndexMemory=322122547
MaxNoOfConcurrentOperations=65535
MaxNoOfLocalOperations=65535
MaxNoOfConcurrentTransactions=65535
MaxNoOfConcurrentIndexOperations=65535
TimeBetweenWatchDogCheck=65535
MaxNoOfAttributes=65535
MaxNoOfTables=1000
MaxNoOfOrderedIndexes=512
TransactionDeadlockDetectionTimeout=65535
HeartbeatIntervalDbDb=20480
[ndb_mgmd]
# 配置管理节点
NodeId=1
hostname=192.168.186.128
datadir=/var/lib/mysql/data
[ndbd]
# 配置数据节点
NodeId=2
hostname=192.168.186.131
datadir=/var/lib/mysql/data
[ndbd]
NodeId=3
hostname=192.168.186.132
datadir=/var/lib/mysql/data
[mysqld]
# 配置 SQL 节点
NodeId=4
hostname=192.168.186.131
[mysqld]
NodeId=5
hostname=192.168.186.132
[tcp default]
# 数据节点的默认连接端口
portnumber=2202
配置 MySQL 数据库:
[root@ricenos_128 softwares]# vi /etc/my.cnf
内容如下:
[client]
socket = /var/lib/mysql/data/mysql.sock
[mysqld]
datadir = /var/lib/mysql/data
socket = /var/lib/mysql/data/mysql.sock
# 服务器端字符集
character_set_server = utf8
# 数据库或数据表的默认排序方式
collation-server = utf8_general_ci
# 忽略客户端的字符集
character-set-client-handshake = false
# 开启查询缓存
explicit_defaults_for_timestamp = true
# 忽略大小写
lower_case_table_names = 1
# 服务器最大连接数
max_connections = 3000
[mysqld_safe]
# 每个进程打开文件数
open-files-limit = 8192
[ndb_mgm]
connect-string = 192.168.186.128
[ndb_mgmd]
config-file = /var/lib/mysql-cluster/config.ini
数据库初始化(仅执行一次,以后不再执行):
[root@ricenos_128 softwares]# mysql_install_db --datadir=/var/lib/mysql/data/ --user=mysql
启动数据库:
[root@ricenos_128 softwares]# service mysql start
针对数据节点和 SQL 节点【192.168.186.131、192.168.186.132】:
[root@ricenos_131 softwares]# vi /etc/my.cnf
内容如下:
[client]
socket = /var/lib/mysql/data/mysql.sock
[mysqld]
datadir = /var/lib/mysql/data
socket = /var/lib/mysql/data/mysql.sock
# 服务器端字符集
character_set_server = utf8
# 数据库或数据表的默认排序方式
collation-server = utf8_general_ci
# 忽略客户端的字符集
character-set-client-handshake = false
# 开启查询缓存
explicit_defaults_for_timestamp = true
# 忽略大小写
lower_case_table_names = 1
# 服务器最大连接数
max_connections = 3000
default-storage-engine = ndbcluster
ndbcluster
ndb-connectstring = 192.168.186.128
[ndbd]
connect-string = 192.168.186.128
[ndb_mgm]
connect-string = 192.168.186.128
[mysql_cluster]
ndb-connectstring = 192.168.186.128
[mysqld_safe]
# 每个进程打开文件数
open-files-limit = 8192
数据库初始化(仅执行一次,以后不再执行):
[root@ricenos_131 softwares]# mysql_install_db --datadir=/var/lib/mysql/data/ --user=mysql
[root@ricenos_132 softwares]# mysql_install_db --datadir=/var/lib/mysql/data/ --user=mysql
启动管理节点(第一次执行):
[root@ricenos_128 softwares]# ndb_mgmd --ndb_nodeid=1 -f /var/lib/mysql-cluster/config.ini --initial
以后执行时,不要再加上“--initial”参数。可以查看 1186 端口,来判断管理节点是否启动。
启动数据节点(第一次执行):
[root@ricenos_131 softwares]# ndbd --ndb_nodeid=2 --initial
[root@ricenos_132 softwares]# ndbd --ndb_nodeid=3 --initial
注意:所有的数据节点都要启动,否则,SQL 节点将不能启动。以后执行时,不要再加上“--initial”参数。可以查看 2202 端口,来判断数据节点是否启动。
启动 SQL 节点:
[root@ricenos_131 softwares]# service mysql start
[root@ricenos_132 softwares]# service mysql start
查看节点状态、客户端状态:
[root@ricenos_128 softwares]# ndb_mgm -e show
每台数据库可以设置自己的密码,以 192.168.186.128 服务器为例,设置密码的方法如下:
mysql> update mysql.user set password=password('pwd128') where user='root';
mysql> grant all on *.* to 'root' identified by 'pwd128';
mysql> flush privileges;
查看数据库编码格式的命令如下:
mysql> show variables like '%char%';
查看数据库最大连接数的命令如下:
mysql> show variables like 'max_connections';
【测试】
在数据节点 192.168.186.131 服务器上操作:
mysql> use test
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> create table person(
-> id int,
-> name varchar(40),
-> birthday date
-> );
Query OK, 0 rows affected (2.91 sec)
mysql> insert into person values(NULL,'ricen',19830917);
Query OK, 1 row affected (0.09 sec)
mysql> select * from person;
+------+-------+------------+
| id | name | birthday |
+------+-------+------------+
| 1 | ricen | 1983-09-17 |
+------+-------+------------+
1 row in set (0.02 sec)
查看数据节点 192.168.186.132 服务器的情况:
mysql> use test
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| person |
+----------------+
1 row in set (0.01 sec)
mysql> select * from person;
+------+-------+------------+
| id | name | birthday |
+------+-------+------------+
| 1 | ricen | 1983-09-17 |
+------+-------+------------+
1 row in set (0.06 sec)
关闭集群的方法如下:
1、关闭管理节点和数据节点:
[root@ricenos_128 softwares]# ndb_mgm -e shutdown
2、关闭 SQL 节点:
[root@ricenos_131 softwares]# service mysql stop
[root@ricenos_132 softwares]# service mysql stop
[root@ricenos_128 softwares]# service mysql stop
开机启动项的配置:
针对管理节点 192.168.186.128:
[root@ricenos_128 softwares]# vi /etc/rc.d/rc.local
在文件末尾添加一行代码:
ndb_mgmd --ndb_nodeid=1 -f /var/lib/mysql-cluster/config.ini
针对数据节点 192.168.186.131:
[root@ricenos_131 softwares]# vi /etc/rc.d/rc.local
在文件末尾添加一行代码:
ndbd --ndb_nodeid=2
针对数据节点 192.168.186.132:
[root@ricenos_132 softwares]# vi /etc/rc.d/rc.local
在文件末尾添加一行代码:
ndbd --ndb_nodeid=3