mysql集群,附件是个整体架构图
集群分为:数据节点(ndbd),管理节点(mgmd),服务节点(mysqld)
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目录下 ,我们可以直接通过./configure来启动mysqld服务。可以查看下configure文件的内容: vi 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 &
- 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=my.cnf & #当前目录下的my.cnf,也可以自己指定绝对路径
vi my.cnf (最好不要以root身份来启动./configure,以./configure所属的用户来启动,此时.cnf注意:o不能有写的权限,即o-w)
[mysqld] port = 3307 socket = /home/mahaibo/mysql-5.0.45-linux-i686/mysql.sock (mysql的sock端口) datadir = /home/mahaibo/mysql-5.0.45-linux-i686/data 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
4.配置了4个服务3306,3307,3308,3309
3306作为mysqld (configure3)
3308,3309作为ndbd节点 (configure1,configure2)
3307作为ndbd-mgmd(管理节点) (configure0)
在3306的mysqld服务的my.cnf里添加
# Options for mysqld process: [MYSQLD] ndbcluster # run NDB engine ndb-connectstring=127.0.0.1 # location of MGM node,也就是mgmd,ndb的manager节点
如果配置里加上了这个这段配置的话,因为配置了ndbcluster引擎,所以只要一个engine连接不上,所有的都连接不上。
所以如果ndbd节点没有启动的话,在3306的机器上create表,无论mysql引擎采用什么(innodb,myisam,ndbcluster) 都将创建失败。
./configure3 启动mysqld节点,3306端口
mysql -uroot -S./mysql3.sock
show variables like 'have%';
可以看到have_ndbcluster 为YES ,说明已经打开。
5. 启动ndbd_mgmd
建立一个配置文件:mgmd.cnf(文件名自己定义),
[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 datadir=/home/mahaibo/mysql-5.0.45-linux-i686/mgmddata # Directory for MGM node logfiles PortNumber=2203 #default 1186 # Options for data node "A": [NDBD] # (one [NDBD] section per data node) hostname=127.0.0.1 # Hostname or IP address datadir=/home/mahaibo/mysql-5.0.45-linux-i686/ndbddata # Directory for this data node's datafiles #ServerPort=3308 # Options for data node "B": #[NDBD] #hostname=127.0.0.1 # Hostname or IP address #datadir=/home/mahaibo/mysql-5.0.45-linux-i686/ndbddata # Directory for this data node's datafiles #ServerPort=3309 # SQL node options: [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) #PortNumber=3306
启动:
./ndb_mgmd -f ../mgmd.cnf
通过ndb_mgm这个客户端来链接:
./ndb_mgm 127.0.0.1 2203
链接后进入ndb_mgm命令窗口:
ndb_mgm> show Connected to Management Server at: 127.0.0.1:2203 Cluster Configuration --------------------- [ndbd(NDB)] 1 node(s) id=2 (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=3 (not connected, accepting connect from 127.0.0.1) ndb_mgm> exit
ndb_mgm> 1 status Connected to Management Server at: 127.0.0.1:2203 Node 1: connected (Version 5.0.45) ndb_mgm> 2 status Node 2: not connected ndb_mgm> 3 status Node 3: not connected ndb_mgm> 4 status 4: Node not found