mysql集群
一、准备系统坏境
使用5台机器redhat linux 5 配置一个mysql集群
资源分配
IP Description 主机名
192.168.0.199 Management Node 管理节点 Management
192.168.0.191 SQL Node 执行sql语句的节点 sqlnodeA
192.168.0.133 SQL Node 执行sql语句的节点 sqlnodeB
192.168.0.196 Data Node 存储数据的节点 datanodeA
192.168.0.17 Data Node 存储数据的节点 datanodeB
192.168.0.199 Management Node 管理节点 Management
192.168.0.191 SQL Node 执行sql语句的节点 sqlnodeA
192.168.0.133 SQL Node 执行sql语句的节点 sqlnodeB
192.168.0.196 Data Node 存储数据的节点 datanodeA
192.168.0.17 Data Node 存储数据的节点 datanodeB
二、软件准备
现在新版本有7.的了。从mysql网站可以下载到。
mysql-cluster-gpl-6.3.20-linux-i686-glibc23.tar.gz
三、准备配置。
1、配置管理节点。
[root@localhost local]# tar xzvf mysql-cluster-gpl-6.3.20-linux-i686-glibc23.tar.gz
[root@localhost local]# mv mysql-cluster-gpl-6.3.20-linux-i686-glibc23 mysql
建立用户: 此步骤在其他4个节点执行相同的操作。
[root@localhost local]# mv mysql-cluster-gpl-6.3.20-linux-i686-glibc23 mysql
其实管理节点只需要ndbd_mgmd ,ndbd_mgm俩个程序,可以把所有的包都使用,也可以从其他4个节点拷贝这俩个程序过来就可以了。
[root@Management ~]# useradd mysql
编辑配置文件,由于配置文件不存在,所以需要手动创建。
修改软件的用户和组
[root@Management local]# chown mysql:mysql -R mysql
[root@Management cluster-conf]# pwd
/usr/local/mysql/cluster-conf
[root@Management cluster-conf]# cat config.ini
#此选项是对数据节点的配置
[ndbd default]
NoOfReplicas=2 # 数据复制的分数
DataMemory=80M # 分配数据内存
IndexMemory=18M # 分配索引使用内存
/usr/local/mysql/cluster-conf
[root@Management cluster-conf]# cat config.ini
#此选项是对数据节点的配置
[ndbd default]
NoOfReplicas=2 # 数据复制的分数
DataMemory=80M # 分配数据内存
IndexMemory=18M # 分配索引使用内存
# 管理节点选项:
[ndb_mgmd]
id=1
hostname=192.168.0.199 # 这个是管理节点的IP地址
datadir=/var/lib/mysql-cluster # 管理所有点点日志的文件目录
[ndb_mgmd]
id=1
hostname=192.168.0.199 # 这个是管理节点的IP地址
datadir=/var/lib/mysql-cluster # 管理所有点点日志的文件目录
#数据节点A配置
[ndbd]
id=2
hostname=192.168.0.196 #datanodeA的IP地址
datadir=/usr/local/mysql/ndbdata #目录如果不存在需要手动创建,在datanodeA从创建
[ndbd]
id=2
hostname=192.168.0.196 #datanodeA的IP地址
datadir=/usr/local/mysql/ndbdata #目录如果不存在需要手动创建,在datanodeA从创建
#数据节点B配置
[ndbd]
id=3
hostname=192.168.0.17 #datanodeB的IP地址
datadir=/usr/local/mysql/ndbdata #目录如果不存在需要手动创建,在datanodeB从创建
[ndbd]
id=3
hostname=192.168.0.17 #datanodeB的IP地址
datadir=/usr/local/mysql/ndbdata #目录如果不存在需要手动创建,在datanodeB从创建
#SQL节点A配置
[mysqld]
id=4
hostname=192.168.0.191 #sql节点A的IP地址
[mysqld]
id=4
hostname=192.168.0.191 #sql节点A的IP地址
#SQL节点B配置
[mysqld]
id=5
hostname=192.168.0.133 #sql节点B的IP地址
#其中id=项不是必须配置的,如果不配置mysql也会自动分配一个。·
[root@Management cluster-conf]#
管理节点配置完成,下面配置sql,data节点。
首先将软件环境准备好,直接拷贝mysql目录到其他4个节点。
1、配置sql节点,A和B配置一样。配置完A后将配置文件拷贝就可以了。
在192.168.0.191和192.168.0.133上配置
[root@sqlnodeA ~]# useradd mysql
[root@sqlnodeA ~]# chown mysql:mysql -R /usr/local/mysql/
[root@sqlnodeA ~]# vi /etc/my.cnf
#SQL½节点A的配置文件
[mysqld]
ndbcluster #配置数据的存储引擎
ndb-connectstring=192.168.0.199 #管理服务器
[root@sqlnodeA ~]# chown mysql:mysql -R /usr/local/mysql/
[root@sqlnodeA ~]# vi /etc/my.cnf
#SQL½节点A的配置文件
[mysqld]
ndbcluster #配置数据的存储引擎
ndb-connectstring=192.168.0.199 #管理服务器
[mysql_cluster]
ndb_connectstring=192.168.0.199 #管理服务器
[root@sqlnodeA ~]# chown mysql:mysql /etc/my.cnf
ndb_connectstring=192.168.0.199 #管理服务器
[root@sqlnodeA ~]# chown mysql:mysql /etc/my.cnf
[root@sqlnodeA ~]# scp /etc/my.cnf 192.168.0.133:/etc/
#sqlnodeB的配置简化。
[root@sqlnodeB ~]# useradd mysql
[root@sqlnodeB ~]# chown mysql:mysql /etc/my.cnf
[root@sqlnodeB ~]# chown mysql:mysql /usr/local/mysql/ -R
[root@sqlnodeB ~]# chown mysql:mysql /etc/my.cnf
[root@sqlnodeB ~]# chown mysql:mysql /usr/local/mysql/ -R
配置完成后,初始话mysql。
[root@sqlnodeA mysql]# pwd
/usr/local/mysql
[root@sqlnodeA mysql]# ./scripts/mysql_install_db --user=mysql
/usr/local/mysql
[root@sqlnodeA mysql]# ./scripts/mysql_install_db --user=mysql
[root@sqlnodeA mysql]# ./scripts/mysql_install_db --user=mysql
WARNING: The host 'sqlnodeA' could not be looked up with resolveip.
This probably means that your libc libraries are not 100 % compatible
with this binary MySQL version. The MySQL daemon, mysqld, should work
normally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnames
when specifying MySQL privileges !
Installing MySQL system tables...
OK
Filling help tables...
OK
WARNING: The host 'sqlnodeA' could not be looked up with resolveip.
This probably means that your libc libraries are not 100 % compatible
with this binary MySQL version. The MySQL daemon, mysqld, should work
normally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnames
when specifying MySQL privileges !
Installing MySQL system tables...
OK
Filling help tables...
OK
SQL节点B也执行相同的操作。
[root@sqlnodeB mysql]# ./scripts/mysql_install_db --user=mysql
SQL节点配置完成,下面配置数据节点
在192.168.0.196和192.168.0.17上配置。A和B配置一样。配置完A后将配置文件拷贝就可以了。
[root@datanodeA ~]# useradd mysql
[root@datanodeA ~]# vi /etc/my.cnf
[root@datanodeA ~]# vi /etc/my.cnf
#这是数据节点A的配置文件
[mysqld]
datadir=/usr/local/mysql/ndbdata #数据存放目录,一定要和管理节点写的一样
ndbcluster #运行的数据库引擎
ndb-connectstring=192.168.0.199 #管理节点
[mysqld]
datadir=/usr/local/mysql/ndbdata #数据存放目录,一定要和管理节点写的一样
ndbcluster #运行的数据库引擎
ndb-connectstring=192.168.0.199 #管理节点
[mysql_cluster]
ndb-connectstring=192.168.0.199
"/etc/my.cnf" [New] 8L, 299C written
[root@datanodeA ~]# mkdir /usr/local/mysql/ndbdata
[root@datanodeA ~]# chown mysql:mysql /usr/local/mysql/ -R
[root@datanodeA ~]# chown mysql:mysql /etc/my.cnf
ndb-connectstring=192.168.0.199
"/etc/my.cnf" [New] 8L, 299C written
[root@datanodeA ~]# mkdir /usr/local/mysql/ndbdata
[root@datanodeA ~]# chown mysql:mysql /usr/local/mysql/ -R
将配置文件给数据节点B
[root@datanodeA ~]# scp /etc/my.cnf 192.168.0.17:/etc/
root@192.168.0.17's password:
my.cnf 100% 299 0.3KB/s 00:00
root@192.168.0.17's password:
my.cnf 100% 299 0.3KB/s 00:00
数据节点B配置简化
[root@datanodeB ~]# useradd mysql
[root@datanodeB ~]# mkdir /usr/local/mysql/ndbdata
[root@datanodeB ~]# chown mysql:mysql -R /usr/local/mysql/ndbdata/
[root@datanodeB ~]# chown mysql:mysql /etc/my.cnf
[root@datanodeB ~]# mkdir /usr/local/mysql/ndbdata
[root@datanodeB ~]# chown mysql:mysql -R /usr/local/mysql/ndbdata/
[root@datanodeB ~]# chown mysql:mysql /etc/my.cnf
全部配置完成,下面开始启动测试
首先启动管理节点。
#启动报错,没目录创建一个
下面启动各个节点的mysqld服务。
[root@Management mysql]# ./bin/ndb_mgmd -f cluster-conf/config.ini
Cannot become daemon: /var/lib/mysql-cluster/ndb_1.pid: open for write failed: No such file or directory
[root@Management mysql]# mkdir /var/lib/mysql-cluster
[root@Management mysql]# ./bin/ndb_mgmd -f cluster-conf/config.ini
[root@Management 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.0.196)
id=3 (not connected, accepting connect from 192.168.0.17)
Cannot become daemon: /var/lib/mysql-cluster/ndb_1.pid: open for write failed: No such file or directory
[root@Management mysql]# mkdir /var/lib/mysql-cluster
[root@Management mysql]# ./bin/ndb_mgmd -f cluster-conf/config.ini
[root@Management 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.0.196)
id=3 (not connected, accepting connect from 192.168.0.17)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.0.199 (mysql-5.1.30 ndb-6.3.20)
id=1 @192.168.0.199 (mysql-5.1.30 ndb-6.3.20)
[mysqld(API)] 2 node(s)
id=4 (not connected, accepting connect from 192.168.0.191)
id=5 (not connected, accepting connect from 192.168.0.133)
id=4 (not connected, accepting connect from 192.168.0.191)
id=5 (not connected, accepting connect from 192.168.0.133)
ndb_mgm>
用ndb_mgm命令进入交互模式,输入show命令可以查看到节点的状态,因为现在其他节点都没有启动,所有状态都是 not connected的。
1、启动sql节点。AB启动方法相同。
[root@sqlnodeA mysql]# pwd
/usr/local/mysql
[root@sqlnodeA mysql]# ./bin/mysqld_safe -user=mysql &
/usr/local/mysql
[root@sqlnodeA mysql]# ./bin/mysqld_safe -user=mysql &
[root@sqlnodeB mysql]# pwd
/usr/local/mysql
[root@sqlnodeB mysql]# ./bin/mysqld_safe -user=mysql &
/usr/local/mysql
[root@sqlnodeB mysql]# ./bin/mysqld_safe -user=mysql &
2、启动数据节点。无需启动mysqld服务。
[root@datanodeA ~]# cd /usr/local/mysql/
[root@datanodeA mysql]# ./bin/ndbd
[root@datanodeA mysql]# ./bin/ndbd
[root@datanodeB ~]# cd /usr/local/mysql/
[root@datanodeB mysql]# ./bin/ndbd
[root@datanodeB mysql]# ./bin/ndbd
ndb_mgm>
[root@Management 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.16
[root@Management 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.16
8.0.196 (mysql-5.1.30 ndb-6.3.20, Nodegroup: 0, Master)
id=3 @192.168.0.17 (mysql-5.1.30 ndb-6.3.20, Nodegroup: 0)
id=3 @192.168.0.17 (mysql-5.1.30 ndb-6.3.20, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.0.199 (mysql-5.1.30 ndb-6.3.20)
id=1 @192.168.0.199 (mysql-5.1.30 ndb-6.3.20)
[mysqld(API)] 2 node(s)
id=4 @192.168.0.191 (mysql-5.1.30 ndb-6.3.20)
id=5 @192.168.0.133 (mysql-5.1.30 ndb-6.3.20)
id=4 @192.168.0.191 (mysql-5.1.30 ndb-6.3.20)
id=5 @192.168.0.133 (mysql-5.1.30 ndb-6.3.20)
ndb_mgm>
可以看到所有节点都链接正常了
4、测试
1、从SQL节点A登录,创建数据库和表,进行简单测试。
[root@sqlnodeA mysql]# ./bin/mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql> show databases ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.01 sec)
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.01 sec)
mysql> create database cluster ;
Query OK, 1 row affected (0.18 sec)
Query OK, 1 row affected (0.18 sec)
mysql> use cluster ;
Database changed
mysql> create table test1(id int,name varchar(10)) engine=ndb ;
Query OK, 0 rows affected (0.03 sec)
Database changed
mysql> create table test1(id int,name varchar(10)) engine=ndb ;
Query OK, 0 rows affected (0.03 sec)
mysql> insert into test1 values(1,'zhaoyun');
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
mysql> select * from test1 ;
+------+---------+
| id | name |
+------+---------+
| 1 | zhaoyun |
+------+---------+
1 row in set (0.00 sec)
+------+---------+
| id | name |
+------+---------+
| 1 | zhaoyun |
+------+---------+
1 row in set (0.00 sec)
mysql>
现在是在SQL节点A上创建的数据库,下面从SQL节点B登录,查看效果。
[root@sqlnodeB mysql]# ./bin/mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.30-ndb-6.3.20-cluster-gpl MySQL Cluster Server (GPL)
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.30-ndb-6.3.20-cluster-gpl MySQL Cluster Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show databases ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cluster |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
+--------------------+
| Database |
+--------------------+
| information_schema |
| cluster |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> use cluster
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
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> show tables ;
+-------------------+
| Tables_in_cluster |
+-------------------+
| test1 |
+-------------------+
1 row in set (0.00 sec)
mysql> show tables ;
+-------------------+
| Tables_in_cluster |
+-------------------+
| test1 |
+-------------------+
1 row in set (0.00 sec)
mysql> select * from test1 ;
+------+---------+
| id | name |
+------+---------+
| 1 | zhaoyun |
+------+---------+
1 row in set (0.00 sec)
+------+---------+
| id | name |
+------+---------+
| 1 | zhaoyun |
+------+---------+
1 row in set (0.00 sec)
mysql> show create table test1 ;
+-------+----------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------------+
| test1 | CREATE TABLE `test1` (
`id` int(11) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL
) ENGINE=ndbcluster DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
+-------+----------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------------+
| test1 | CREATE TABLE `test1` (
`id` int(11) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL
) ENGINE=ndbcluster DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> insert into test1 values(2,'zhao');
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
mysql> select * from test1 ;
+------+---------+
| id | name |
+------+---------+
| 1 | zhaoyun |
| 2 | zhao |
+------+---------+
2 rows in set (0.00 sec)
+------+---------+
| id | name |
+------+---------+
| 1 | zhaoyun |
| 2 | zhao |
+------+---------+
2 rows in set (0.00 sec)
现在看来,在SQL节点B登录,可以看到sql节点A创建的内容。那么,再从B创建内容看A是否能看到。
mysql> select * from test1 ;
+------+---------+
| id | name |
+------+---------+
| 1 | zhaoyun |
| 2 | zhao |
+------+---------+
2 rows in set (0.00 sec) 在A节点也可以看到从B节点插入的数据了。
+------+---------+
| id | name |
+------+---------+
| 1 | zhaoyun |
| 2 | zhao |
+------+---------+
2 rows in set (0.00 sec) 在A节点也可以看到从B节点插入的数据了。
说明现在sql节点已经工作正常,因为数据存放在数据节点,任何一个坏掉,都不影响使用。
本文转自zhaoyun00 51CTO博客,原文链接:
http://blog.51cto.com/zhaoyun/738435