`
ttitfly
  • 浏览: 616504 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

mysql集群整理版

阅读更多
mysql集群架构主要分为:数据节点(ndbd),管理节点(mgmd),服务节点(mysqld)
附件是mysql集群整体架构图

1. 下载:http://dev.mysql.com/downloads/mysql/5.0.html#linux 可以从这里下载 mysql-5.0.45-linux-i686.tar.gz

此包里已经包含了mysql-max包(mysql集群需要安装mysql-max包)。

2. 解压:

tar -zxvf apache-tomcat-5.5.25.tar.gz
  

下载的这个.tar.gz是编译后的文件,只需要解压即可。

3. 配置
比如解压后的文件都放在:/home/mahaibo/mysql-5.0.45-linux-i686目录下。

数据节点(ndbd),管理节点(mgmd),服务节点(mysqld) 三个启动顺序是

mgmd ---> ndbd ---> mysqld

可以在同一台电脑上做个简单的配置和测试。以在同一台电脑上不同端口为例。实际中肯定是需要分布在不同的服务器上的。

A: 配置mgmd,并启动

在/home/mahaibo/mysql-5.0.45-linux-i686建立一个mgmd.cnf和mgmd的数据目录mgmddata  ,编辑mgmd.cnf文件:

 

# Options affecting ndbd processes on all data nodes:
[NDBD DEFAULT]    
NoOfReplicas=1    # Number of replicas
DataMemory=80M    # How much memory to allocate for data storage
IndexMemory=18M   # How much memory to allocate for index storage
                  # For DataMemory and IndexMemory, we have used the
                  # default values. Since the "world" database takes up
                  # only about 500KB, this should be more than enough for
                  # this example Cluster setup.

# TCP/IP options:
[TCP DEFAULT]     
#portnumber=2202   # This the default; however, you can use any
                  # port that is free for all the hosts in cluster
                  # Note: It is recommended beginning with MySQL 5.0 that
                  # you do not specify the portnumber at all and simply allow
                  # the default value to be used instead

# Management process options:
[NDB_MGMD]                      
hostname=127.0.0.1           # Hostname or IP address of MGM node ,mgmd's hostname
datadir=/home/mahaibo/mysql-5.0.45-linux-i686/mgmddata  # Directory for MGM node logfiles
PortNumber=2203            #default value: 1186

# Options for data node "A":
[NDBD]                          
                                # (one [NDBD] section per data node)
hostname=127.0.0.1           # Hostname or IP address ,allow to access
datadir=/home/mahaibo/mysql-5.0.45-linux-i686/mgmddata   # Directory for this data node's datafiles

# Options for data node "B":
[NDBD]                          
hostname=127.0.0.1           # Hostname or IP address
datadir=/home/mahaibo/mysql-5.0.45-linux-i686/mgmddata   # Directory for this data node's datafiles

[MYSQLD]                        
hostname=127.0.0.1           # Hostname or IP address
                                # (additional mysqld connections can be
                                # specified for this node for various
                                # purposes such as running ndb_restore)



可以看到配置了2个数据节点ndbd,也就是说改管理节点可以管理小于或等于2个数据节点。管理节点mgmd的端口为2203。

启动mgmd服务:
bin/ndb_mgmd -f mgmd.cnf 


启动后,登录mgmd的客户端mgm可以查看具体信息:
bin/ndb_mgm 127.0.0.1 2203 //因为mgmd服务用的端口是2203,所以要通过该端口链接


[mahaibo@client mysql-5.0.45-linux-i686]$ bin/ndb_mgm 127.0.0.1 2203
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: 127.0.0.1:2203
Cluster Configuration
---------------------
[ndbd(NDB)]     2 node(s)
id=2 (not connected, accepting connect from 127.0.0.1)
id=3 (not connected, accepting connect from 127.0.0.1)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @127.0.0.1  (Version: 5.0.45)

[mysqld(API)]   1 node(s)
id=4 (not connected, accepting connect from 127.0.0.1)

ndb_mgm> 


可以看到ndbd和mysqld都是not connected状态,因为这2个服务都还没有启动。


B: 启动数据节点(ndbd)


以启动2个数据节点为例。

分别建立ndbd1.cnf ,ndbd1data数据目录

编辑ndbd1.cnf:
[mysqld]
DataDir=/home/mahaibo/mysql-5.0.45-linux-i686/ndbd1data//数据目录
skip-locking
key_buffer = 16M
max_allowed_packet = 1M
table_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M

ndbcluster
ndb-connectstring=127.0.0.1:2203
[mysql_cluster]
ndb-connectstring=127.0.0.1:2203 // 连接mgmd服务的ip与端口


启动第一个ndbd数据节点
bin/ndbd --defaults-file=ndbd1.cnf --initial


第一次启动需要加--initial参数。

第2个数据节点(ndbd)也需要做同样的配置和启动操作。分别建立ndbd2.cnf ,ndbd2data数据目录
ndbd2.cnf的配置和ndbd1.cnf的配置几乎是一样的,只是数据目录不一样。
启动第2个数据节点ndbd:
bin/ndbd --defaults-file=ndbd2.cnf --initial


这时,如果登录到mgmd管理节点的客户端mgm,
[mahaibo@client mysql-5.0.45-linux-i686]$ bin/ndb_mgm 127.0.0.1 2203
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: 127.0.0.1:2203
Cluster Configuration
---------------------
[ndbd(NDB)]     2 node(s)
id=2    @127.0.0.1  (Version: 5.0.45, Nodegroup: 0, Master)
id=3    @127.0.0.1  (Version: 5.0.45, Nodegroup: 1)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @127.0.0.1  (Version: 5.0.45)

[mysqld(API)]   1 node(s)
id=4 (not connected, accepting connect from 127.0.0.1)

ndb_mgm> 

可以看到ndbd的2个节点都是连接状态了,而不是not connected状态了,但是mysqld还是not connected状态。

C: 启动mysqld服务。


打开mysql-5.0.45-linux-i686下的configure文件,可以看到其内容为:
#!/bin/sh
if test ! -x  ./scripts/mysql_install_db
then
  echo "I didn't find the script './scripts/mysql_install_db'."
  echo "Please execute this script in the mysql distribution directory!"
  exit 1;
fi

echo "NOTE: This is a MySQL binary distribution. It's ready to run, you don't"
echo "need to configure it!"
echo ""
echo "To help you a bit, I am now going to create the needed MySQL databases"
echo "and start the MySQL server for you.  If you run into any trouble, please"
echo "consult the MySQL manual, that you can find in the Docs directory."
echo ""

./scripts/mysql_install_db --no-defaults
if [ $? = 0 ]
then
  echo "Starting the mysqld server.  You can test that it is up and running"
  echo "with the command:"
  echo "./bin/mysqladmin version"

  ./bin/mysqld_safe --no-defaults & #默认会去根据/etc/my.cnf这个配置文件进行启动,并且生成的sock端口会放在/tmp/mysql.sock
fi


可以看到:它实际是上先执行./scripts/mysql_install_db,然后在去通过./bin/mysqld_safe去启动mysql服务的,用的参数是--no-defaults,也就是默认会去根据/etc/my.cnf这个配置文件进行启动,并且生成的sock端口会放在/tmp/mysql.sock.

如果想让mysql启动时调用自己指定的my.cnf的位置。可以把configure文件内容里的./bin/mysqld_safe --no-defaults &修改为:
./bin/mysqld_safe --defaults-file=my3.cnf & #当前目录下的my.cnf,也可以自己指定绝对路径 


建立my3.cnf和数据目录data3

编辑my3.cnf:
[mysqld]

port            = 3306
socket          = /home/mahaibo/mysql-5.0.45-linux-i686/mysql3.sock
datadir         = /home/mahaibo/mysql-5.0.45-linux-i686/data3
ndbcluster                      # run NDB engine
ndb-connectstring=127.0.0.1:2203  # location of MGM node


skip-locking
key_buffer = 16M
max_allowed_packet = 1M
table_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M


启动mysqld服务:
./configure3


如果这时在登录到mgmd管理节点的客户端mgm,
[mahaibo@client mysql-5.0.45-linux-i686]$ bin/ndb_mgm 127.0.0.1 2203
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: 127.0.0.1:2203
Cluster Configuration
---------------------
[ndbd(NDB)]     2 node(s)
id=2    @127.0.0.1  (Version: 5.0.45, Nodegroup: 0, Master)
id=3    @127.0.0.1  (Version: 5.0.45, Nodegroup: 1)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @127.0.0.1  (Version: 5.0.45)

[mysqld(API)]   1 node(s)
id=4    @127.0.0.1  (Version: 5.0.45)


可以看到mysqld也是连接状态了

登录到mysqld的客户端,然后
show variables like 'have%';

可以看到
have_ndbcluster 为 YES,说明ndbcluster引擎已经打开。

可以建立一个ndbcluster引擎的表测试下看是否成功
create table table1 (aa int) engine=ndbcluster;
insert into table1 values(11);
insert into table1 values(22);
都没有问题。
show create table table1;
可以看到:
mysql> show create table table1;
+--------+------------------------------------------------------------------------------------------------+
| Table  | Create Table                                                                                   |
+--------+------------------------------------------------------------------------------------------------+
| table1 | CREATE TABLE `table1` (
  `aa` int(11) default NULL
) ENGINE=ndbcluster DEFAULT CHARSET=latin1 |
+--------+------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


以上只是记录的配置流水账过程,也许有些说的不对的地方,请多指点。具体项目应用还需要具体参考mysql文档进行配置

  • 描述: mysqlcluster
  • 大小: 60.5 KB
分享到:
评论
13 楼 xianglei 2008-12-04  
galaxystar 写道
mysql的master-slave部署方式,听说有延迟?

对是有延迟,只有在很高的并发下才能发现
12 楼 liangguanhui 2008-02-29  
YRHYRH 写道
dogstar 写道
谁能给一个mysql部署大型web应用的最佳实践,那就造福大众了。:),结合mysqlProxy那就更爽了。。


没有多少个人是真正懂的.都是在这里等着别人免费喂到你们的口上?本身并不难的东西.有多少人自己去实践过?没有做就不要发言.在这里贻害后生!你们有什么资格谈MYSQL的性能,稳定性? 

11 楼 YRHYRH 2008-02-05  
dogstar 写道
谁能给一个mysql部署大型web应用的最佳实践,那就造福大众了。:),结合mysqlProxy那就更爽了。。


没有多少个人是真正懂的.都是在这里等着别人免费喂到你们的口上?本身并不难的东西.有多少人自己去实践过?没有做就不要发言.在这里贻害后生!你们有什么资格谈MYSQL的性能,稳定性? 
10 楼 dogstar 2008-01-19  
谁能给一个mysql部署大型web应用的最佳实践,那就造福大众了。:),结合mysqlProxy那就更爽了。。
9 楼 galaxystar 2008-01-18  
mysql的master-slave部署方式,听说有延迟?
8 楼 ztka 2008-01-12  
mysql的NDB是集群 不是master slave结构
7 楼 netstu 2008-01-08  
关键是mysql集群不是真正意义上的集群吧?

是主从关系吧

能实现像oracle一样的rac集群吗?
6 楼 ztka 2008-01-03  
mysql 的cluster本质就是 把db装入内存 进行cluster,NDB就是放入内存的引擎
5 楼 andyao 2007-12-28  
MySQL集群问题还是很多的
4 楼 lin_zy 2007-12-27  
不是把,8g内存,
mysql支持几个数据库,支持多少人同时访问啊
3 楼 dingyuan 2007-12-22  
ttitfly 写道
mysql集群架构主要分为:数据节点(ndbd),管理节点(mgmd),服务节点(mysqld)
附件是mysql集群整体架构图

1. 下载:http://dev.mysql.com/downloads/mysql/5.0.html#linux 可以从这里下载 mysql-5.0.45-linux-i686.tar.gz

此包里已经包含了mysql-max包(mysql集群需要安装mysql-max包)。

2. 解压:

tar -zxvf apache-tomcat-5.5.25.tar.gz
  

下载的这个.tar.gz是编译后的文件,只需要解压即可。

3. 配置
比如解压后的文件都放在:/home/mahaibo/mysql-5.0.45-linux-i686目录下。

数据节点(ndbd),管理节点(mgmd),服务节点(mysqld) 三个启动顺序是

mgmd ---> ndbd ---> mysqld

可以在同一台电脑上做个简单的配置和测试。以在同一台电脑上不同端口为例。实际中肯定是需要分布在不同的服务器上的。

A: 配置mgmd,并启动

在/home/mahaibo/mysql-5.0.45-linux-i686建立一个mgmd.cnf和mgmd的数据目录mgmddata  ,编辑mgmd.cnf文件:

 

# Options affecting ndbd processes on all data nodes:
[NDBD DEFAULT]    
NoOfReplicas=1    # Number of replicas
DataMemory=80M    # How much memory to allocate for data storage
IndexMemory=18M   # How much memory to allocate for index storage
                  # For DataMemory and IndexMemory, we have used the
                  # default values. Since the "world" database takes up
                  # only about 500KB, this should be more than enough for
                  # this example Cluster setup.

# TCP/IP options:
[TCP DEFAULT]     
#portnumber=2202   # This the default; however, you can use any
                  # port that is free for all the hosts in cluster
                  # Note: It is recommended beginning with MySQL 5.0 that
                  # you do not specify the portnumber at all and simply allow
                  # the default value to be used instead

# Management process options:
[NDB_MGMD]                      
hostname=127.0.0.1           # Hostname or IP address of MGM node ,mgmd's hostname
datadir=/home/mahaibo/mysql-5.0.45-linux-i686/mgmddata  # Directory for MGM node logfiles
PortNumber=2203            #default value: 1186

# Options for data node "A":
[NDBD]                          
                                # (one [NDBD] section per data node)
hostname=127.0.0.1           # Hostname or IP address ,allow to access
datadir=/home/mahaibo/mysql-5.0.45-linux-i686/mgmddata   # Directory for this data node's datafiles

# Options for data node "B":
[NDBD]                          
hostname=127.0.0.1           # Hostname or IP address
datadir=/home/mahaibo/mysql-5.0.45-linux-i686/mgmddata   # Directory for this data node's datafiles

[MYSQLD]                        
hostname=127.0.0.1           # Hostname or IP address
                                # (additional mysqld connections can be
                                # specified for this node for various
                                # purposes such as running ndb_restore)



可以看到配置了2个数据节点ndbd,也就是说改管理节点可以管理小于或等于2个数据节点。管理节点mgmd的端口为2203。

启动mgmd服务:
bin/ndb_mgmd -f mgmd.cnf 


启动后,登录mgmd的客户端mgm可以查看具体信息:
bin/ndb_mgm 127.0.0.1 2203 //因为mgmd服务用的端口是2203,所以要通过该端口链接


[mahaibo@client mysql-5.0.45-linux-i686]$ bin/ndb_mgm 127.0.0.1 2203
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: 127.0.0.1:2203
Cluster Configuration
---------------------
[ndbd(NDB)]     2 node(s)
id=2 (not connected, accepting connect from 127.0.0.1)
id=3 (not connected, accepting connect from 127.0.0.1)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @127.0.0.1  (Version: 5.0.45)

[mysqld(API)]   1 node(s)
id=4 (not connected, accepting connect from 127.0.0.1)

ndb_mgm> 


可以看到ndbd和mysqld都是not connected状态,因为这2个服务都还没有启动。


B: 启动数据节点(ndbd)


以启动2个数据节点为例。

分别建立ndbd1.cnf ,ndbd1data数据目录

编辑ndbd1.cnf:
[mysqld]
DataDir=/home/mahaibo/mysql-5.0.45-linux-i686/ndbd1data//数据目录
skip-locking
key_buffer = 16M
max_allowed_packet = 1M
table_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M

ndbcluster
ndb-connectstring=127.0.0.1:2203
[mysql_cluster]
ndb-connectstring=127.0.0.1:2203 // 连接mgmd服务的ip与端口


启动第一个ndbd数据节点
bin/ndbd --defaults-file=ndbd1.cnf --initial


第一次启动需要加--initial参数。

第2个数据节点(ndbd)也需要做同样的配置和启动操作。分别建立ndbd2.cnf ,ndbd2data数据目录
ndbd2.cnf的配置和ndbd1.cnf的配置几乎是一样的,只是数据目录不一样。
启动第2个数据节点ndbd:
bin/ndbd --defaults-file=ndbd2.cnf --initial


这时,如果登录到mgmd管理节点的客户端mgm,
[mahaibo@client mysql-5.0.45-linux-i686]$ bin/ndb_mgm 127.0.0.1 2203
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: 127.0.0.1:2203
Cluster Configuration
---------------------
[ndbd(NDB)]     2 node(s)
id=2    @127.0.0.1  (Version: 5.0.45, Nodegroup: 0, Master)
id=3    @127.0.0.1  (Version: 5.0.45, Nodegroup: 1)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @127.0.0.1  (Version: 5.0.45)

[mysqld(API)]   1 node(s)
id=4 (not connected, accepting connect from 127.0.0.1)

ndb_mgm> 

可以看到ndbd的2个节点都是连接状态了,而不是not connected状态了,但是mysqld还是not connected状态。

C: 启动mysqld服务。


打开mysql-5.0.45-linux-i686下的configure文件,可以看到其内容为:
#!/bin/sh
if test ! -x  ./scripts/mysql_install_db
then
  echo "I didn't find the script './scripts/mysql_install_db'."
  echo "Please execute this script in the mysql distribution directory!"
  exit 1;
fi

echo "NOTE: This is a MySQL binary distribution. It's ready to run, you don't"
echo "need to configure it!"
echo ""
echo "To help you a bit, I am now going to create the needed MySQL databases"
echo "and start the MySQL server for you.  If you run into any trouble, please"
echo "consult the MySQL manual, that you can find in the Docs directory."
echo ""

./scripts/mysql_install_db --no-defaults
if [ $? = 0 ]
then
  echo "Starting the mysqld server.  You can test that it is up and running"
  echo "with the command:"
  echo "./bin/mysqladmin version"

  ./bin/mysqld_safe --no-defaults & #默认会去根据/etc/my.cnf这个配置文件进行启动,并且生成的sock端口会放在/tmp/mysql.sock
fi


可以看到:它实际是上先执行./scripts/mysql_install_db,然后在去通过./bin/mysqld_safe去启动mysql服务的,用的参数是--no-defaults,也就是默认会去根据/etc/my.cnf这个配置文件进行启动,并且生成的sock端口会放在/tmp/mysql.sock.

如果想让mysql启动时调用自己指定的my.cnf的位置。可以把configure文件内容里的./bin/mysqld_safe --no-defaults &修改为:
./bin/mysqld_safe --defaults-file=my3.cnf & #当前目录下的my.cnf,也可以自己指定绝对路径 


建立my3.cnf和数据目录data3

编辑my3.cnf:
[mysqld]

port            = 3306
socket          = /home/mahaibo/mysql-5.0.45-linux-i686/mysql3.sock
datadir         = /home/mahaibo/mysql-5.0.45-linux-i686/data3
ndbcluster                      # run NDB engine
ndb-connectstring=127.0.0.1:2203  # location of MGM node


skip-locking
key_buffer = 16M
max_allowed_packet = 1M
table_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M


启动mysqld服务:
./configure3


如果这时在登录到mgmd管理节点的客户端mgm,
[mahaibo@client mysql-5.0.45-linux-i686]$ bin/ndb_mgm 127.0.0.1 2203
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: 127.0.0.1:2203
Cluster Configuration
---------------------
[ndbd(NDB)]     2 node(s)
id=2    @127.0.0.1  (Version: 5.0.45, Nodegroup: 0, Master)
id=3    @127.0.0.1  (Version: 5.0.45, Nodegroup: 1)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @127.0.0.1  (Version: 5.0.45)

[mysqld(API)]   1 node(s)
id=4    @127.0.0.1  (Version: 5.0.45)


可以看到mysqld也是连接状态了

登录到mysqld的客户端,然后
show variables like 'have%';

可以看到
have_ndbcluster 为 YES,说明ndbcluster引擎已经打开。

可以建立一个ndbcluster引擎的表测试下看是否成功
create table table1 (aa int) engine=ndbcluster;
insert into table1 values(11);
insert into table1 values(22);
都没有问题。
show create table table1;
可以看到:
mysql> show create table table1;
+--------+------------------------------------------------------------------------------------------------+
| Table  | Create Table                                                                                   |
+--------+------------------------------------------------------------------------------------------------+
| table1 | CREATE TABLE `table1` (
  `aa` int(11) default NULL
) ENGINE=ndbcluster DEFAULT CHARSET=latin1 |
+--------+------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


以上只是记录的配置流水账过程,也许有些说的不对的地方,请多指点。具体项目应用还需要具体参考mysql文档进行配置

2 楼 ttitfly 2007-12-21  
ahuaxuan 写道
请问一下楼主,mysql的集群是否稳定,听人说mysql集群不是很稳定,不知道楼主有没有这方面的经验
会有这种问题吗?具体我还没有遇到过,还需要做大量测试。
1 楼 ahuaxuan 2007-12-21  
请问一下楼主,mysql的集群是否稳定,听人说mysql集群不是很稳定,不知道楼主有没有这方面的经验

相关推荐

Global site tag (gtag.js) - Google Analytics