`
lzj0470
  • 浏览: 1241756 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论
阅读更多

<script type="text/javascript"></script>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. 解压:

Java代码 复制代码
  1. tar -zxvf apache-tomcat-5.5.25.tar.gz  
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文件:

Java代码 复制代码
  1.     
  2.   
  3. # Options affecting ndbd processes on all data nodes:   
  4. [NDBD DEFAULT]       
  5. NoOfReplicas=1    # Number of replicas   
  6. DataMemory=80M    # How much memory to allocate for data storage   
  7. IndexMemory=18M   # How much memory to allocate for index storage   
  8.                   # For DataMemory and IndexMemory, we have used the   
  9.                   # default values. Since the "world" database takes up   
  10.                   # only about 500KB, this should be more than enough for  
  11.                   # this example Cluster setup.   
  12.   
  13. # TCP/IP options:   
  14. [TCP DEFAULT]        
  15. #portnumber=2202   # This the default; however, you can use any   
  16.                   # port that is free for all the hosts in cluster   
  17.                   # Note: It is recommended beginning with MySQL 5.0 that   
  18.                   # you do not specify the portnumber at all and simply allow   
  19.                   # the default value to be used instead   
  20.   
  21. # Management process options:   
  22. [NDB_MGMD]                         
  23. hostname=127.0.0.1           # Hostname or IP address of MGM node ,mgmd's hostname   
  24. datadir=/home/mahaibo/mysql-5.0.45-linux-i686/mgmddata  # Directory for MGM node logfiles   
  25. PortNumber=2203            #default value: 1186  
  26.   
  27. # Options for data node "A":   
  28. [NDBD]                             
  29.                                 # (one [NDBD] section per data node)   
  30. hostname=127.0.0.1           # Hostname or IP address ,allow to access   
  31. datadir=/home/mahaibo/mysql-5.0.45-linux-i686/mgmddata   # Directory for this data node's datafiles   
  32.   
  33. # Options for data node "B":   
  34. [NDBD]                             
  35. hostname=127.0.0.1           # Hostname or IP address   
  36. datadir=/home/mahaibo/mysql-5.0.45-linux-i686/mgmddata   # Directory for this data node's datafiles   
  37.   
  38. [MYSQLD]                           
  39. hostname=127.0.0.1           # Hostname or IP address   
  40.                                 # (additional mysqld connections can be   
  41.                                 # specified for this node for various   
  42.                                 # purposes such as running ndb_restore)  
 

# 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服务:

Java代码 复制代码
  1. bin/ndb_mgmd -f mgmd.cnf   
bin/ndb_mgmd -f mgmd.cnf 



启动后,登录mgmd的客户端mgm可以查看具体信息:

Java代码 复制代码
  1. bin/ndb_mgm 127.0.0.1 2203 //因为mgmd服务用的端口是2203,所以要通过该端口链接  
bin/ndb_mgm 127.0.0.1 2203 //因为mgmd服务用的端口是2203,所以要通过该端口链接


Java代码 复制代码
  1. [mahaibo@client mysql-5.0.45-linux-i686]$ bin/ndb_mgm 127.0.0.1 2203  
  2. -- NDB Cluster -- Management Client --   
  3. ndb_mgm> show   
  4. Connected to Management Server at: 127.0.0.1:2203  
  5. Cluster Configuration   
  6. ---------------------   
  7. [ndbd(NDB)]     2 node(s)   
  8. id=2 (not connected, accepting connect from 127.0.0.1)   
  9. id=3 (not connected, accepting connect from 127.0.0.1)   
  10.   
  11. [ndb_mgmd(MGM)] 1 node(s)   
  12. id=1    @127.0.0.1  (Version: 5.0.45)   
  13.   
  14. [mysqld(API)]   1 node(s)   
  15. id=4 (not connected, accepting connect from 127.0.0.1)   
  16.   
  17. ndb_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 (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:

Java代码 复制代码
  1. [mysqld]   
  2. DataDir=/home/mahaibo/mysql-5.0.45-linux-i686/ndbd1data//数据目录   
  3. skip-locking   
  4. key_buffer = 16M   
  5. max_allowed_packet = 1M   
  6. table_cache = 64  
  7. sort_buffer_size = 512K   
  8. net_buffer_length = 8K   
  9. read_buffer_size = 256K   
  10. read_rnd_buffer_size = 512K   
  11. myisam_sort_buffer_size = 8M   
  12.   
  13. ndbcluster   
  14. ndb-connectstring=127.0.0.1:2203  
  15. [mysql_cluster]   
  16. ndb-connectstring=127.0.0.1:2203 // 连接mgmd服务的ip与端口  
[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数据节点

Java代码 复制代码
  1. bin/ndbd --defaults-file=ndbd1.cnf --initial  
bin/ndbd --defaults-file=ndbd1.cnf --initial



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

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

Java代码 复制代码
  1. bin/ndbd --defaults-file=ndbd2.cnf --initial  
bin/ndbd --defaults-file=ndbd2.cnf --initial



这时,如果登录到mgmd管理节点的客户端mgm,

Java代码 复制代码
  1. [mahaibo@client mysql-5.0.45-linux-i686]$ bin/ndb_mgm 127.0.0.1 2203  
  2. -- NDB Cluster -- Management Client --   
  3. ndb_mgm> show   
  4. Connected to Management Server at: 127.0.0.1:2203  
  5. Cluster Configuration   
  6. ---------------------   
  7. [ndbd(NDB)]     2 node(s)   
  8. id=2    @127.0.0.1  (Version: 5.0.45, Nodegroup: 0, Master)   
  9. id=3    @127.0.0.1  (Version: 5.0.45, Nodegroup: 1)   
  10.   
  11. [ndb_mgmd(MGM)] 1 node(s)   
  12. id=1    @127.0.0.1  (Version: 5.0.45)   
  13.   
  14. [mysqld(API)]   1 node(s)   
  15. id=4 (not connected, accepting connect from 127.0.0.1)   
  16.   
  17. ndb_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文件,可以看到其内容为:

Java代码 复制代码
  1. #!/bin/sh   
  2. if test ! -x  ./scripts/mysql_install_db   
  3. then   
  4.   echo "I didn't find the script './scripts/mysql_install_db'."  
  5.   echo "Please execute this script in the mysql distribution directory!"  
  6.   exit 1;   
  7. fi   
  8.   
  9. echo "NOTE: This is a MySQL binary distribution. It's ready to run, you don't"  
  10. echo "need to configure it!"  
  11. echo ""  
  12. echo "To help you a bit, I am now going to create the needed MySQL databases"  
  13. echo "and start the MySQL server for you.  If you run into any trouble, please"  
  14. echo "consult the MySQL manual, that you can find in the Docs directory."  
  15. echo ""  
  16.   
  17. ./scripts/mysql_install_db --no-defaults   
  18. if [ $? = 0 ]   
  19. then   
  20.   echo "Starting the mysqld server.  You can test that it is up and running"  
  21.   echo "with the command:"  
  22.   echo "./bin/mysqladmin version"  
  23.   
  24.   ./bin/mysqld_safe --no-defaults & #默认会去根据/etc/my.cnf这个配置文件进行启动,并且生成的sock端口会放在/tmp/mysql.sock   
  25. fi  
#!/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 &修改为:

Java代码 复制代码
  1. ./bin/mysqld_safe --defaults-file=my3.cnf & #当前目录下的my.cnf,也可以自己指定绝对路径   
./bin/mysqld_safe --defaults-file=my3.cnf & #当前目录下的my.cnf,也可以自己指定绝对路径 



建立my3.cnf和数据目录data3

编辑my3.cnf:

Java代码 复制代码
  1. [mysqld]   
  2.   
  3. port            = 3306  
  4. socket          = /home/mahaibo/mysql-5.0.45-linux-i686/mysql3.sock   
  5. datadir         = /home/mahaibo/mysql-5.0.45-linux-i686/data3   
  6. ndbcluster                      # run NDB engine   
  7. ndb-connectstring=127.0.0.1:2203  # location of MGM node   
  8.   
  9.   
  10. skip-locking   
  11. key_buffer = 16M   
  12. max_allowed_packet = 1M   
  13. table_cache = 64  
  14. sort_buffer_size = 512K   
  15. net_buffer_length = 8K   
  16. read_buffer_size = 256K   
  17. read_rnd_buffer_size = 512K   
  18. myisam_sort_buffer_size = 8M  
[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服务:

Java代码 复制代码
  1. ./configure3  
./configure3



如果这时在登录到mgmd管理节点的客户端mgm,

Java代码 复制代码
  1. [mahaibo@client mysql-5.0.45-linux-i686]$ bin/ndb_mgm 127.0.0.1 2203  
  2. -- NDB Cluster -- Management Client --   
  3. ndb_mgm> show   
  4. Connected to Management Server at: 127.0.0.1:2203  
  5. Cluster Configuration   
  6. ---------------------   
  7. [ndbd(NDB)]     2 node(s)   
  8. id=2    @127.0.0.1  (Version: 5.0.45, Nodegroup: 0, Master)   
  9. id=3    @127.0.0.1  (Version: 5.0.45, Nodegroup: 1)   
  10.   
  11. [ndb_mgmd(MGM)] 1 node(s)   
  12. id=1    @127.0.0.1  (Version: 5.0.45)   
  13.   
  14. [mysqld(API)]   1 node(s)   
  15. id=4    @127.0.0.1  (Version: 5.0.45)  
[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的客户端,然后

Java代码 复制代码
  1. show variables like 'have%';  
show variables like 'have%';


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

可以建立一个ndbcluster引擎的表测试下看是否成功

Java代码 复制代码
  1. create table table1 (aa int) engine=ndbcluster;   
  2. insert into table1 values(11);   
  3. insert into table1 values(22);  
create table table1 (aa int) engine=ndbcluster;
insert into table1 values(11);
insert into table1 values(22);

都没有问题。
show create table table1;
可以看到:

Java代码 复制代码
  1. mysql> show create table table1;   
  2. +--------+------------------------------------------------------------------------------------------------+   
  3. | Table  | Create Table                                                                                   |   
  4. +--------+------------------------------------------------------------------------------------------------+   
  5. | table1 | CREATE TABLE `table1` (   
  6.   `aa` int(11default NULL   
  7. ) ENGINE=ndbcluster DEFAULT CHARSET=latin1 |   
  8. +--------+------------------------------------------------------------------------------------------------+   
  9. 1 row in set (0.00 sec)  
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文档进行配置

 

来源于:http://www.iteye.com/topic/149130

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics