首页 > 代码库 > 2-18-搭建mysql集群实现高可用
2-18-搭建mysql集群实现高可用
1 环境清理以及安装
1.1 mysql旧版本清除
准备5台虚拟机,分配如下
mysql管理结点:xuegod1.cn IP:192.168.10.31 (安装server、client)
mysql数据结点:xuegod2.cn IP:192.168.10.32 (安装server、client)
mysql数据结点:xuegod3.cn IP:192.168.10.33 (安装server、client)
msyqlSQL结点:xuegod4.cn IP:192.168.10.34 (安装server、client)
msyqlSQL结点:xuegod5.cn IP:192.168.10.35 (安装server、client)
首先使用如下命令来清理之前操作系统自带的mysql安装:
[root@xuegod1 ~]# yum remove -y mysql
然后使用如下命令:
[root@xuegod1 ~]# rpm -qa|grep mysql
mysql-libs-5.1.73-7.el6.x86_64
对于找到的2个剩余mysql包,请按照如下的命令格式予以删除:
[root@xuegod1 ~]# rpm -e --nodeps mysql-libs-5.1.73-7.el6.x86_64
最后删掉下列文件:
01. rm -rf /var/lib/mysql/*
02. rm -rf /etc/my.cnf
03. rm -rf /etc/init.d/mysqld
1.2 mysql cluster版本安装及准备工作
1 将MySQL-Cluster-gpl-7.3.4-1.el6.x86_64.rpm-bundle.tar放到某个目录下(譬如/package) 下面,并且执行如下命令解压:
tar -xvfMySQL-Cluster-gpl-7.3.4-1.el6.x86_64.rpm-bundle.tar
得到如下文件清单:
MySQL-Cluster-client-gpl-7.3.4-1.el6.x86_64.rpm
MySQL-Cluster-devel-gpl-7.3.4-1.el6.x86_64.rpm
MySQL-Cluster-embedded-gpl-7.3.4-1.el6.x86_64.rpm
MySQL-Cluster-server-gpl-7.3.4-1.el6.x86_64.rpm
MySQL-Cluster-shared-compat-gpl-7.3.4-1.el6.x86_64.rpm
MySQL-Cluster-shared-gpl-7.3.4-1.el6.x86_64.rpm
MySQL-Cluster-test-gpl-7.3.4-1.el6.x86_64.rpm
也可以在PC端解压,只上传server和client的rpm包
通过scp命令讲server和client rpm包传到每一台服务器
[root@xuegod1 ~]# scp MySQL-Cluster-server-gpl-7.4.11-1.el6.x86_64.rpm MySQL-Cluster-client-gpl-7.4.11-1.el6.x86_64.rpm 192.168.10.32:/root/
2 每一台主机都要安装mysql集群软件包(server\client)批量安装
开启所有服务器会话,在xshell右下角选择全部会话,然后在下面的框里面输入命令回车
tar -xvf MySQL-Cluster-gpl-7.3.4-1.el6.x86_64.rpm-bundle.tar
批量安装mysql集群软件包(server\client)
yum -y install MySQL-Cluster-server-gpl-7.4.11-1.el6.x86_64.rpm
rpm -ivh MySQL-Cluster-client-gpl-7.4.11-1.el6.x86_64.rpm
会生成两个主要的命令文件:ndb_mgmd 和ndb_mgm
[root@xuegod63 ~]# whichndb_mgm
/usr/bin/ndb_mgm
[root@xuegod63 ~]# rpm -qf/usr/bin/ndb_mgm
MySQL-Cluster-server-gpl-7.3.7-1.el6.x86_64
[root@xuegod63 ~]# whichndb_mgmd
/usr/sbin/ndb_mgmd
[root@xuegod63 ~]# rpm -qf/usr/sbin/ndb_mgmd
MySQL-Cluster-server-gpl-7.3.7-1.el6.x86_64
还会生成用户:mysql
[root@xuegod63 ~]# id mysql
uid=495(mysql)gid=489(mysql) groups=489(mysql)
1.3 创建文件夹
分如下三个雷来创建对应的文件夹
管理节点创建:
[root@xuegod1 ~]# mkdir /var/lib/mysql-cluster /var/run/mysqld/
数据节点存放数据:
[root@xuegod2 ~]# mkdir /var/run/mysqld
[root@xuegod3 ~]# mkdir /var/run/mysqld
SQL节点: 可不用文件授权 (进程PID目录)
[root@xuegod4 ~]# mkdir /var/run/mysqld
[root@xuegod5 ~]# mkdir /var/run/mysqld
1.4 授权
管理节点授权:
[root@xuegod1 ~]# chown mysql.mysql -R /var/lib/mysql-cluster/ /var/run/mysqld/
数据节点授权:
[root@xuegod2 ~]# chown mysql.mysql -R /var/lib/mysql/ /var/run/mysqld/
[root@xuegod3 ~]# chown mysql.mysql -R /var/lib/mysql/ /var/run/mysqld/
SQL节点授权:
[root@xuegod4 ~]# chown mysql.mysql -R /var/lib/mysql/ /var/run/mysqld/
[root@xuegod5 ~]# chown mysql.mysql -R /var/lib/mysql/ /var/run/mysqld/
1.5 查看mysql root用户密码
注意:当安装完毕MySQL-Cluster-server-gpl包后,将出现如下
提示信息,提醒我们整个cluster安装后的初次超级账户密码存在/root/.mysql_secret这个文件当中。
[root@xuegod1 ~]# cat /root/.mysql_secret
# The random password set for the root user at Wed May 10 16:57:54 2017 (local time): 4plZLpSfizLjKSBm
2 搭建mysql集群
2.1 配置各主机
2.1.1 xuegod1创建管理节点配置文件
[root@xuegod1 ~]vim /var/lib/mysql-cluster/config.ini #写入以下内容
[ndbddefault]
NoOfReplicas=2 #数据写入数量。2表示两份
DataMemory=200M #配置数据存储可使用的内存
IndexMemory=100M #索引给100M
[ndb_mgmd]
id=1
datadir=/var/lib/mysql-cluster #管理结点的日志
HostName=192.168.10.31 #管理结点的IP地址。本机IP
######data node options: #存储结点
[ndbd]
HostName=192.168.10.32
DataDir=/var/lib/mysql #mysql数据存储路径
id=2
[ndbd]
HostName=192.168.10.33
DataDir=/var/lib/mysql #mysql数据存储路径
id=3
# SQLnode options: #关于SQL结点
[mysqld]
HostName=192.168.10.34
id=4
[mysqld]
HostName=192.168.10.35
id=5
在这个文件里,我们分别给五个节点分配了ID,这有利于更好的管理和区分各个节点。当然,要是不指定,MySQL也会动态分配一个
2.1.2 xuegod2数据节点
[root@xuegod2 /]# vim /etc/my.cnf #xuegod3配置一样
[mysqld]
datadir=/var/lib/mysql #mysql数据存储路径
ndbcluster #启动ndb引擎
ndb-connectstring=192.168.10.31 # 管理节点IP地址
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[mysql_cluster]
ndb-connectstring=192.168.10.31 #管理节点IP地址
2.1.3 SQL节点配置
[root@xuegod4 ~]# vim /etc/my.cnf #xuegod5配置一样
[mysqld]
ndbcluster #启动ndb引擎
ndb-connectstring=192.168.10.31 #管理节点IP地址
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[mysql_cluster]
ndb-connectstring=192.168.10.31 #管理节点IP都中
说明:数据节点和SQL节点配置文件区别,就多一行,数据节点有: datadir=/var/lib/mysql #mysql数据的存储路径,SQL节点上没有。
2.2 MySQL Cluster启动
初次启动命令以及用户密码更改调整:(严格按照次序启动)
先启动:管理节点服务-->数据节点服务-->SQL节点服务
关闭:关闭管理节点服务,关闭管理节点服务后,nbdb数据节点服务会自动关闭-->手动把sql节点服务管理。
执行初次启动前请先确认 将两台机器的防火墙关闭(service iptables stop 或者 设定 防火墙端口可通,两个端口即通讯端口1186、数据端口3306 )
2.2.1 xuegod1上启动管理节点命令
[root@xuegod1 ~]# ndb_mgmd -f /var/lib/mysql-cluster/config.ini # mysql cluster 后台运行进程
#尽量不要把管理结点、 数据结点、存储结点 配置在同一台机子上,否则一个挂了,就全挂了。
查看端口号:
[root@xuegod1 ~]# netstat -anput|grep 1186
tcp 0 0 0.0.0.0:1186 0.0.0.0:* LISTEN 2380/ndb_mgmd
tcp 0 0 127.0.0.1:1186 127.0.0.1:49343 ESTABLISHED 2380/ndb_mgmd
tcp 0 0 192.168.10.31:1186 192.168.10.33:48587 ESTABLISHED 2380/ndb_mgmd
tcp 0 0 192.168.10.31:1186 192.168.10.32:35434 ESTABLISHED 2380/ndb_mgmd
tcp 0 0 192.168.10.31:1186 192.168.10.33:48586 ESTABLISHED 2380/ndb_mgmd
tcp 0 0 192.168.10.31:1186 192.168.10.32:35435 ESTABLISHED 2380/ndb_mgmd
tcp 0 0 127.0.0.1:49343 127.0.0.1:1186 ESTABLISHED 2380/ndb_mgmd
2.2.2 xuegod2和xuegod3启动数据节点服务
[root@xuegod2 ~]# ndbd --initial
2017-05-10 09:34:21 [ndbd] INFO -- Angel connected to ‘192.168.10.31:1186‘
2017-05-10 09:34:21 [ndbd] INFO -- Angel allocated nodeid: 2
[root@xuegod3 ~]# ndbd --initial
2017-05-10 05:54:38 [ndbd] INFO -- Angel connected to ‘192.168.10.31:1186‘
2017-05-10 05:54:38 [ndbd] INFO -- Angel allocated nodeid: 3
2.2.3 xuegod4和xuegod5启动SQL结点服务
[root@xuegod4 ~]# mysqld_safe --defaults-file=/etc/my.cnf &
[root@xuegod5 ~]# mysqld_safe --defaults-file=/etc/my.cnf &
查看mysql集群状态:
[root@xuegod1 ~]# ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: localhost:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @192.168.10.32 (mysql-5.6.29 ndb-7.4.11, Nodegroup: 0, *)
id=3 @192.168.10.33 (mysql-5.6.29 ndb-7.4.11, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.10.31 (mysql-5.6.29 ndb-7.4.11)
[mysqld(API)] 2 node(s)
id=4 @192.168.10.34 (mysql-5.6.29 ndb-7.4.11)
id=5 @192.168.10.35 (mysql-5.6.29 ndb-7.4.11)
2.3 数据同步
因为默认密码比较坑人,我们就需要在此之前改一下两台机器mysql的密码。
2.3.1 xuegod5修改mysql root密码
[root@xuegod5 ~]# cat /root/.mysql_secret
# The random password set for the root user at Wed May 24 02:31:53 2017 (local time): Imzb3KPGbjHYxIAl
[root@xuegod5 ~]# mysql -uroot -pImzb3KPGbjHYxIAl
mysql> show database;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘database‘ at line 1
mysql> set password=password(‘123456‘);
Query OK, 0 rows affected (0.01 sec)
mysql> exit;
Bye
[root@xuegod5 ~]# mysql -uroot -p123456 #测试登录
mysql> grant all privileges on *.* to cluster@"%" identified by "123456"; #授权
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
2.3.2 xuegod4修改mysql root密码
[root@xuegod4 ~]# cat /root/.mysql_secret
# The random password set for the root user at Wed May 24 03:08:52 2017 (local time): 4r4jBIWfcedp753c
[root@xuegod4 ~]# mysql -uroot -p4r4jBIWfcedp753c
mysql> set password=password(‘123456‘);
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@xuegod4 ~]# mysql -uroot -p123456
mysql> grant all privileges on *.* to cluster@"%" identified by "123456";
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec
3 测试
模拟外部机器的一个客户端插入数据
[root@xuegod1 ~]# mysql -ucluster -p123456 -h 192.168.10.34
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| ndbinfo |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> create database db;
Query OK, 1 row affected (0.12 sec)
mysql> use db;
Database changed
mysql> create table test(id int) engine=ndb;
Query OK, 0 rows affected (0.34 sec)
mysql> insert into test values(1000);
Query OK, 1 row affected (0.02 sec)
mysql> select * from test;
+------+
| id |
+------+
| 1000 |
+------+
1 row in set (0.01 sec)
登录另一台sql节点查看
[root@xuegod1 ~]# mysql -ucluster -p123456 -h 192.168.10.35
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db |
| mysql |
| ndbinfo |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.02 sec)
mysql> use db;
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 test;
+------+
| id |
+------+
| 1000 |
+------+
1 row in set (0.01 sec)
停掉一个节点测试:
[root@xuegod5 ~]# mysqladmin -uroot -p123456 shutdown
ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @192.168.10.32 (mysql-5.6.29 ndb-7.4.11, Nodegroup: 0, *)
id=3 @192.168.10.33 (mysql-5.6.29 ndb-7.4.11, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.10.31 (mysql-5.6.29 ndb-7.4.11)
[mysqld(API)] 2 node(s)
id=4 @192.168.10.34 (mysql-5.6.29 ndb-7.4.11)
id=5 (not connected, accepting connect from 192.168.10.35)
再启动节点
[root@xuegod5 ~]# mysqld_safe --defaults-file=/etc/my.cnf &
ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @192.168.10.32 (mysql-5.6.29 ndb-7.4.11, Nodegroup: 0, *)
id=3 @192.168.10.33 (mysql-5.6.29 ndb-7.4.11, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.10.31 (mysql-5.6.29 ndb-7.4.11)
[mysqld(API)] 2 node(s)
id=4 @192.168.10.34 (mysql-5.6.29 ndb-7.4.11)
id=5 @192.168.10.35 (mysql-5.6.29 ndb-7.4.11)
4 关闭服务
关闭mysql集群顺序: 关闭管理节点服务-》 关闭管理节点时,数据结点服务自动关闭 –》 需要手动关闭SQL结点服务
关闭管理节点:
[root@xuegod1 ~]# ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm> shutdown
Connected to Management Server at: localhost:1186
Node 3: Cluster shutdown initiated
Node 2: Cluster shutdown initiated
Node 3: Node shutdown completed.
Node 2: Node shutdown completed.
3 NDB Cluster node(s) have shutdown.
Disconnecting to allow management server to shutdown.
ndb_mgm> exit
[root@xuegod1 ~]# ps -aux|grep ndbd #查看不到,说明数据节点已经被关
Warning: bad syntax, perhaps a bogus ‘-‘? See /usr/share/doc/procps-3.2.8/FAQ
root 2476 0.0 0.0 103316 836 pts/1 S+ 22:06 0:00 grep ndbd
手动关闭SQL节点服务
[root@xuegod4 ~]# mysqladmin -uroot -p123456 shutdown
[root@xuegod5 ~]# mysqladmin -uroot -p123456 shutdown
或方法二kill掉
[root@xuegod4 ~]# ps -axu|grep mysql
Warning: bad syntax, perhaps a bogus ‘-‘? See /usr/share/doc/procps-3.2.8/FAQ
root 4142 0.0 0.1 106244 1392 pts/0 S 08:22 0:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/etc/my.cnf
mysql 4253 2.8 44.6 951048 448728 pts/0 Sl 08:22 0:00 /usr/sbin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid
root 4293 0.0 0.0 103316 836 pts/2 S+ 08:23 0:00 grep mysql
[root@xuegod4 ~]# kill -9 4142
[root@xuegod4 ~]# kill -9 4253
[root@xuegod5 ~]# ps -aux|grep mysql
Warning: bad syntax, perhaps a bogus ‘-‘? See /usr/share/doc/procps-3.2.8/FAQ
root 4306 0.0 0.1 106244 1392 pts/2 S 07:46 0:00 /bin/sh /usr/bin/mysqld_safe --defaults-file=/etc/my.cnf
mysql 4417 0.5 44.8 1279896 450652 pts/2 Sl 07:46 0:00 /usr/sbin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid
root 4446 0.0 0.0 103316 836 pts/1 S+ 07:47 0:00 grep mysql
[root@xuegod5 ~]# kill -9 4306
[root@xuegod5 ~]# kill -9 4417
5 总结
再次启动,mysql集群启动
[root@xuegod1 ~]# ndb_mgmd -f /var/lib/mysql-cluster/config.ini
[root@xuegod2 ~]# ndbd
[root@xuegod3 ~]# ndbd
[root@xuegod4 ~]# mysqld_safe --defaults-file=/etc/my.cnf &
[root@xuegod5~]# mysqld_safe --defaults-file=/etc/my.cnf &
查看mysql集群状态:
[root@xuegod1 ~]# ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm> show
Connected to Management Server at: localhost:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 @192.168.10.32 (mysql-5.6.29 ndb-7.4.11, Nodegroup: 0, *)
id=3 @192.168.10.33 (mysql-5.6.29 ndb-7.4.11, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.10.31 (mysql-5.6.29 ndb-7.4.11)
[mysqld(API)] 2 node(s)
id=4 @192.168.10.34 (mysql-5.6.29 ndb-7.4.11)
id=5 @192.168.10.35 (mysql-5.6.29 ndb-7.4.11)
ndb_mgm>
2-18-搭建mysql集群实现高可用