分类: LINUX
2010-02-24 00:38:33
Mysql集群安装
Mgm: 192.168.1.240 mysql.mgm
Node1: 192.168.1.241 mysql.node1
Node2: 192.168.1.242 mysql.node2
A、修改所有主机的hosts文件,把机器名和ip加入
# vi /etc/hosts
192.168.1.240 mysql.mgm
192.168.1.241 mysql.node1
192.168.1.242 mysql.node2
下载mysql编译好的二进制包
http://dev.mysql.com/downloads/mysql/
集群使用的包为下面的地址,上面的为普通mysql
http://dev.mysql.com/downloads/cluster/
1、在mgm上面的操作
# tar zvxf mysql-cluster-gpl-7.0.9-linux-i686-glibc23.tar.gz
# mv mysql-cluster-gpl-7.0.9-linux-i686-glibc23 /usr/local/mysql
# cd /usr/local/mysql/
# groupadd mysql
# useradd -g mysql mysql
# chown -R mysql:mysql /usr/local/mysql
如果系统已经安装了mysql,那么先做下面的操作
# mv /etc/my.cnf /etc/my.cnf.bak
# scripts/mysql_install_db --user=mysql
# bin/mysqld_safe --user=mysql &
# ./bin/mysqladmin -u root password '123456'
# support-files/mysql.server stop
(可选操作)完成以上操作后恢复系统的mysql配置文件
# mv /etc/my.cnf.bak /etc/my.cnf
# cd /usr/local/mysql/support-files
# cp my-medium.cnf ../data/my.cnf 说明:具体使用哪个配置文件,看实际应用
启动mysql
# /usr/local/mysql/bin/mysqld_safe --no-defaults --user=mysql &
如果/etc/my.cnf不存在,那么启动方式可以执行
# /usr/local/mysql/support-files/mysql.server start
3、配置管理节点mgm
# mkdir /usr/local/mysql/mysql-cluster
# chown mysql:mysql /usr/local/mysql/mysql-cluster
以下红字操作完全可以忽略
修改配置文件,在最后加入以下内容
# vi /usr/local/mysql/data/my.cnf
[ndb_mgm]
connect-string=192.168.1.240
[ndb_mgmd]
config-file=/usr/local/mysql/mysql-cluser/config.ini
##connect-string=mysql.mgm ##ndb_mgm 管理节点的计算机名
##config-file=/etc/config.ini ##ndb_mgmd的启动所要的配置文件
创建,修改配置文件
# vi /usr/local/mysql/mysql-cluser/config.ini
# Example Ndbcluster storage engine config file.
#
[ndbd default]
NoOfReplicas= 2
MaxNoOfConcurrentOperations= 10000
DataMemory= 80M
IndexMemory= 24M
TimeBetweenWatchDogCheck= 30000
DataDir= /usr/local/mysql/data
MaxNoOfOrderedIndexes= 512
[ndb_mgmd default]
DataDir= /usr/local/mysql/mysql-cluster
[ndb_mgmd]
Id=1
HostName= 192.168.1.240
[ndbd]
Id= 2
HostName= 192.168.1.241
[ndbd]
Id= 3
HostName= 192.168.1.242
[mysqld]
Id= 4
HostName= 192.168.1.241
[mysqld]
Id= 5
HostName= 192.168.1.242
[mysqld]
Id= 6
[mysqld]
Id= 7
# choose an unused port number
# in this configuration 63132, 63133, and 63134
# will be used
[tcp default]
PortNumber= 63132
也可以使用下面的配置,上下2种配置选其一就可以
# cp /usr/local/mysql/support-files/config.small.ini /usr/local/mysql/mysql-cluster/config.ini
具体使用哪个配置文件看实际应用情况
修改配置文件
# vi /usr/local/mysql/mysql-cluster/config.ini
#
# MySQL NDB Cluster Small Sample Configuration File
#
# This files assumes that you are using 1 to 3 hosts
# for running the cluster. Hostnames and paths listed
# below should be changed to match your setup.
#
# Note: You can change localhost for a different host
#
[NDBD DEFAULT]
NoOfReplicas: 2
DataDir: /usr/local/mysql/data
#FileSystemPath: /add/path/here
# Data Memory, Index Memory, and String Memory
DataMemory: 100M
IndexMemory: 50M
BackupMemory: 32M
[MGM DEFAULT]
PortNumber: 1186
DataDir: /usr/local/mysql/mysql-cluster
[NDB_MGMD]
Id: 1
HostName: 192.168.1.240
ArbitrationRank: 1
[NDBD]
Id: 2
HostName: 192.168.1.241
[NDBD]
Id: 3
HostName: 192.168.1.242
#
# Note: The following can be MySQLD connections or
# NDB API application connecting to the cluster
#
[API]
Id: 4
HostName: 192.168.1.241
#ArbitrationRank: 2
[API]
Id: 5
HostName: 192.168.1.242
[API]
Id: 6
[API]
Id: 7
[API]
Id: 8
[API]
Id: 9
4、分别配置node1、node2
# vi /usr/local/mysql/my.cnf
[mysqld]
ndbcluster
ndb-connectstring=192.168.1.240
[MYSQL_CLUSTER]
ndb-connectstring=192.168.1.240
5启动管理节点
# /usr/local/mysql/bin/ndb_mgmd -f /usr/local/mysql/mysql-cluster/config.ini
如果是修改了配置文件里的某些参数,则需要先关闭 ndb_mgmd 进程,然后重新启动,不过必须加上 --reload 选项,因为 7.0 版本中,会把配置文件放在 cache 里,如果不注意到这点,可能会被搞得莫名其妙的。
# /usr/local/mysql/bin/ndb_mgmd -f /usr/local/mysql/mysql-cluster/config.ini --reload
6、启动存储节点和sql节点,在node1和node2上操作
首次启动,需要加上 --initial 选项,其后的启动后就不需要了
# /usr/local/mysql/bin/ndbd --defaults-file=/usr/local/mysql/data/my.cnf --initial
说明:如果配置文件不在/etc/my.cnf使用上面的,如果在则用下面的
# /usr/local/mysql/bin/ndbd –initial
7、测试
严重注意:所有建表语句的TYPE=MyISAM必须替换为ENGINE=NDBCLUSTER
在node1上操作
# /usr/local/mysql/bin/mysql -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.39-ndb-7.0.9-cluster-gpl-log MySQL Cluster Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test;
Database changed
mysql> create table ndbt( name varchar(100) ) engine=ndbcluster;
Query OK, 0 rows affected (0.50 sec)
在node2上操作
# /usr/local/mysql/bin/mysql -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.39-ndb-7.0.9-cluster-gpl-log MySQL Cluster Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test;
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_test |
+----------------+
| ndbt |
+----------------+
1 row in set (0.03 sec)
然后在node2上插入数据
mysql> insert into ndbt (name) value (1);
Query OK, 1 row affected (0.01 sec)
在node1上操作
mysql> select * from ndbt;
+------+
| name |
+------+
| 1 |
+------+
1 row in set (0.01 sec)
成功