Alternatively you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
Please report any problems with the /usr/bin/mysqlbug script!
以下步骤需要在db1和db2上各做一次
2.配置通用my.cnf文件(SQL节点及ndb节点均使用此文件)
# vi /etc/my.cnf
在my.cnf中添加如下内容:
[mysqld]
datadir=/var/lib/mysql/data/
socket=/var/lib/mysql/mysql.sock
lower_case_table_names=1
ndb_cluster_connection_pool=2
tmp_table_size=512M
max_heap_table_size=512M
max_connections =400
thread_stack=1M
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
[iyunv@demo sbin]# ndb_mgmd --ndb_nodeid=1
MySQL Cluster Management Server mysql-5.5.31 ndb-7.2.13
2013-10-29 15:45:36 [MgmtSrvr] INFO -- The default config directory '/usr/mysql-cluster' does not exist. Trying to create it...
2013-10-29 15:45:36 [MgmtSrvr] INFO -- Sucessfully created config directory
2013-10-29 15:45:36 [MgmtSrvr] WARNING -- at line 38: [tcp] portnumber is deprecated
2013-10-29 15:45:36 [MgmtSrvr] WARNING -- at line 38: Cluster configuration warning:
arbitrator with id 1 and db node with id 2 on same host 172.16.1.74
arbitrator with id 2 and db node with id 3 on same host 172.16.1.75
arbitrator with id 4 has no hostname specified
arbitrator with id 5 has no hostname specified
Running arbitrator on the same host as a database node may
cause complete cluster shutdown in case of host failure.
注:在启动时几个警告提示
2013-10-29 15:45:36 [MgmtSrvr] WARNING -- at line 38: [tcp] portnumber is deprecated这个警告提示可以不必理会,不影响使用;
2013-10-29 15:45:36 [MgmtSrvr] WARNING -- at line 38: Cluster configuration warning这个警告提示说节点1和3,2和4的arbitrator一样,可能引起整个集群失败,由于是双管理节点所示此警告可以不用放在心上
[iyunv@demo sbin]# netstat -lntpu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:1186 0.0.0.0:* LISTEN 9484/ndb_mgmd
看到1186端口开放了说明启动是正常的。
五、初始化集群
在db1中
[iyunv@db1 sbin]# ndbd --ndb_nodeid=2 --initial
2013-10-30 09:37:07 [ndbd] INFO -- Angel connected to '172.16.1.78:1186'
2013-10-30 09:37:07 [ndbd] INFO -- Angel allocated nodeid: 2
执行netstat –lntpu,如果列表中有端口为2279,表示初始化成功
[iyunv@db1 mysql-cluster]# netstat -lntpu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:1186 0.0.0.0:* LISTEN 13713/ndb_mgmd
tcp 0 0 172.16.1.74:2279 0.0.0.0:* LISTEN 13735/ndbd
在db2中
[iyunv@db2 mysql]# ndbd --ndb_nodeid=3 --initial
2013-10-30 09:37:49 [ndbd] INFO -- Angel connected to '172.16.1.78:1186'
2013-10-30 09:37:49 [ndbd] INFO -- Angel allocated nodeid: 3
初始化集群也可以直接输入ndbd –initial命令,而不必输入节点号
注:只有在第一次启动ndbd时或者对config.ini进行改动后才需要使用--initial参数!数据库维护过程中慎用initial参数,
如果数据库中已经存在大量数据,并且在所有数据节点上都执行了 ndbd --initial,那么所有数据库中的表和数据都会消失。
七、设置数据库的用户名和密码
执行如下命令:
./bin/mysqladmin -u root password 'huawei'
./bin/mysqladmin -u root -h db2 password 'huawei'
新增一个webapp用户,并且配置权限
mysql>GRANT ALL ON *.* TO 'webapp'@'%' IDENTIFIED BY 'huawei';
mysql>GRANT ALL ON *.* TO 'webapp'@'localhost' IDENTIFIED BY 'huawei';
mysql>GRANT ALL ON *.* TO 'webapp'@'db1' IDENTIFIED BY 'huawei';
mysql>GRANT ALL ON *.* TO 'webapp'@'db2' IDENTIFIED BY 'huawei';
[mysqld(API)] 6 node(s)
id=4 @172.16.1.74 (mysql-5.5.31 ndb-7.2.13)
id=5 @172.16.1.74 (mysql-5.5.31 ndb-7.2.13)
id=6 @172.16.1.75 (mysql-5.5.31 ndb-7.2.13)
id=7 @172.16.1.75 (mysql-5.5.31 ndb-7.2.13)
id=8 (not connected, accepting connect from any host)
id=9 (not connected, accepting connect from any host)
可以看到这里的数据节点、管理节点、sql节点都是正常的。现在我们在其中一个数据节点上进行相关数据库的创建,然后到另外一个数据节点上看看数据是否同步。
[iyunv@db2 usr]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.31-ndb-7.2.13-cluster-gpl MySQL Cluster Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| ndb_4_fs |
| ndbinfo |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.08 sec)
mysql> create database aa;
Query OK, 1 row affected (3.56 sec)
mysql> use aa
Database changed
mysql> CREATE TABLE ctest2 (i INT) ;
Query OK, 0 rows affected (24.71 sec)
mysql> SELECT * FROM ctest2;
+------+
| i |
+------+
| 1 |
+------+
1 row in set (0.01 sec)
现在到另外一个数据节点db1查看下aa数据库是否同步过来了。
[iyunv@db1 mysql-cluster]# mysql -u root
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aa |
| mysql |
| ndb_3_fs |
| ndbinfo |
| performance_schema |
| test |
+--------------------+
7 rows in set (4.80 sec)
mysql> use aa
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> select * from ctest2;
+------+
| i |
+------+
| 1 |
+------+
1 row in set (0.01 sec)
从上面可以看到数据已经同步了,mysql集群环境已经搭建完成。
九、破坏性测试
在上面可以看到172.16.1.74作为主的数据节点,现在把172.16.1.74这台机器关闭,有如下的结果:
[iyunv@db1 mysql-cluster]# shutdown -h now
Broadcast message from root (pts/2) (Thu Oct 31 14:00:56 2013):
The system is going down for system halt NOW!
[iyunv@db2 ~]# ndb_mgm -e show
Connected to Management Server at: 172.16.1.75:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 (not connected, accepting connect from 172.16.1.74)
id=3 @172.16.1.75 (mysql-5.5.31 ndb-7.2.13, Nodegroup: 0, Master)
配置heartbeat
[iyunv@db2 etc]# vi /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
172.16.1.75 db2.test.com db2
172.16.1.74 db1.test.com db1
172.16.1.78 demo.test.com demo
172.16.1.73 db.test.com db #后面要专门设置这样一个虚拟IP地址
[iyunv@db1 ~]# vi /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
172.16.1.74 db1.test.com db1
172.16.1.75 db2.test.com db2
172.16.1.78 demo.test.com demo
172.16.1.73 db.test.com db #后面要专门设置这样一个虚拟IP地址
创建数据库和测试表
mysql>GRANT ALL ON ldirectordb.* TO 'ldirectord'@'%' IDENTIFIED BY 'ldirectord';
mysql>GRANT ALL ON ldirectordb.* TO 'ldirectord'@'localhost' IDENTIFIED BY 'ldirectord';
mysql>GRANT ALL ON ldirectordb.* TO 'ldirectord'@'db1' IDENTIFIED BY 'ldirectord';
mysql>GRANT ALL ON ldirectordb.* TO 'ldirectord'@'db2' IDENTIFIED BY 'ldirectord';
mysql>FLUSH PRIVILEGES;
mysql>CREATE DATABASE ldirectordb;
mysql>USE ldirectordb;
mysql>CREATE TABLE ldirectordtest (Status INT) ;
mysql>INSERT INTO ldirectordtest() VALUES (1);
mysql>quit
ldirectord状态检查
[iyunv@db1 ~]# ldirectord ldirectord.cf status
ldirectord for /etc/ha.d/ldirectord.cf is running with pid: 6203
[iyunv@db2 ~]# ldirectord ldirectord.cf status
ldirectord is stopped for /etc/ha.d/ldirectord.cf
heartbeat状态检查
[iyunv@db1 log]# service heartbeat status
heartbeat OK [pid 5750 et al] is running on db1.test.com [db1.test.com]...
[iyunv@db2 log]# service heartbeat status
heartbeat OK [pid 5796 et al] is running on db2.test.com [db2.test.com]...
----错误问题解决
[iyunv@db1 init.d]# ./ldirectord status
ldirectord stale pid file /var/run/ldirectord.ldirectord.pid for /etc/ha.d/ldirectord.cf
ldirectord is stopped for /etc/ha.d/ldirectord.cf
此问题已经解决了,是因为缺少包造成的。缺少perl-DBI-mysql、perl-DBD-mysql
虚拟IP状态检查
[iyunv@db2 log]# ip addr sh eth0
4: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 90:e2:ba:01:fe:24 brd ff:ff:ff:ff:ff:ff
inet 172.16.1.75/24 brd 172.16.1.255 scope global eth0
inet 172.16.1.73/24 brd 172.16.1.255 scope global secondary eth0:0
inet6 fe80::92e2:baff:fe01:fe24/64 scope link
valid_lft forever preferred_lft forever
[iyunv@db1 log]# ip addr sh eth0
4: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 90:e2:ba:01:fa:fa brd ff:ff:ff:ff:ff:ff
inet 172.16.1.74/24 brd 172.16.1.255 scope global eth0
inet6 fe80::92e2:baff:fe01:fafa/64 scope link
valid_lft forever preferred_lft forever
IPVS状态检查
[iyunv@db2 log]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.1.73:3306 wrr
-> 172.16.1.74:3306 Route 1 0 0
-> 172.16.1.75:3306 Local 0 0 0
[iyunv@db1 log]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
db1上执行./LVSSyncDaemonSwap master status
[iyunv@db1 resource.d]# ./LVSSyncDaemonSwap master status
master running
(ipvs_syncmaster pid: 4043)
db1上执行./LVSSyncDaemonSwap master status
[iyunv@db2 ha.d]# resource.d/LVSSyncDaemonSwap master status
master stopped
[iyunv@db2 log]# mysql -u ldirectord -p -h 172.16.1.73
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 492
Server version: 5.5.31-ndb-7.2.13-cluster-gpl MySQL Cluster Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| ldirectordb |
| test |
+--------------------+
3 rows in set (0.00 sec)
mysql> use ldirectordb;
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> select * from ldirectordtest;
+--------+
| Status |
+--------+
| 1 |
+--------+
1 row in set (0.00 sec)
mysql>
[iyunv@db1 log]# mysql -u ldirectord -p -h 172.16.1.73
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 492
Server version: 5.5.31-ndb-7.2.13-cluster-gpl MySQL Cluster Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| ldirectordb |
| test |
+--------------------+
3 rows in set (0.00 sec)
mysql> use ldirectordb;
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> select * from ldirectordtest;
+--------+
| Status |
+--------+
| 1 |
+--------+
1 row in set (0.00 sec)