准备数据库
create database mqtt;
use mqtt;
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for mqtt_acl
-- ----------------------------
DROP TABLE IF EXISTS `mqtt_acl`;
CREATE TABLE `mqtt_acl` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`allow` int(1) DEFAULT NULL COMMENT '0: deny, 1: allow',
`ipaddr` varchar(60) DEFAULT NULL COMMENT 'IpAddress',
`username` varchar(100) DEFAULT NULL COMMENT 'Username',
`clientid` varchar(100) DEFAULT NULL COMMENT 'ClientId',
`access` int(2) NOT NULL COMMENT '1: subscribe, 2: publish, 3: pubsub',
`topic` varchar(100) NOT NULL DEFAULT '' COMMENT 'Topic Filter',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for mqtt_user
-- ----------------------------
DROP TABLE IF EXISTS `mqtt_user`;
CREATE TABLE `mqtt_user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(100) DEFAULT NULL,
`password` blob,
`salt` blob,
`is_superuser` tinyint(1) DEFAULT '0',
`created` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `mqtt_username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=643 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
3节点均执行下列步骤进行安装
安装依赖:
yum install lksctp-tools -y
下载emqtt:
https://www.emqx.io/downloads/broker/v2.2.0/emqttd-centos7-v2.2.0.el7.centos.x86_64.rpm
#安装
rpm -ivh --force emqttd-centos7-v2.2.0.el7.centos.x86_64.rpm
#替换127.0.0.1为当前节点的真实ip
sed -i 's$node.name = emqttd@127.0.0.1$node.name = emqttd@192.168.6.117$g' /etc/emqttd/emq.conf
### 修改mysql插件配置(注意替换数据库连接信息)
sed -i 's$auth.mysql.server = 127.0.0.1:3306$auth.mysql.server = 192.168.6.125:23306$' /etc/emqttd/plugins/emq_auth_mysql.conf
sed -i 's$auth.mysql.pool = 8$auth.mysql.pool = 50$g' /etc/emqttd/plugins/emq_auth_mysql.conf
sed -i 's$## auth.mysql.username =$auth.mysql.username=root$g' /etc/emqttd/plugins/emq_auth_mysql.conf
sed -i 's$## auth.mysql.password =$auth.mysql.password=xiaoWEI0923!$g' /etc/emqttd/plugins/emq_auth_mysql.conf
sed -i 's$select password from mqtt_user$select password,salt from mqtt_user$g' /etc/emqttd/plugins/emq_auth_mysql.conf
sed -i 's$auth.mysql.password_hash = sha256$auth.mysql.password_hash = md5 salt$g' /etc/emqttd/plugins/emq_auth_mysql.conf
### 启动并确认
#设置emqtt为自启动
systemctl enable emqttd.service
#启动emqtt
systemctl start emqttd
#检查
systemctl status emqttd 或netstat -tlnp | grep 1883,如打印以下内容,则证明服务启动,1883已监听
tcp 0 0 127.0.0.1:11883 0.0.0.0:* LISTEN 21579/beam.smp
tcp 0 0 0.0.0.0:1883 0.0.0.0:* LISTEN 21579/beam.smp
#Load mysql认证
emqttd_ctl plugins load emq_auth_mysql
#重启emqtt
systemctl restart emqttd
浏览器打开 http://当前部署节点ip:18083/#/plugins
账号:admin
密码:public
确认emq_auth_mysql插件处于running状态
创建集群
假设上面安装了mqtt的服务器分别为node1,node2,node3,则:
在node2上执行:emqttd_ctl cluster join emqttd@node1的ip
在node3上执行:emqttd_ctl cluster join emqttd@node2的ip
#确认集群创建成功
emqttd_ctl cluster status
running_nodes中包含了所有节点ip,如下:
Cluster status: [{running_nodes,['emqttd@192.168.6.118',
'emqttd@192.168.6.117',
'emqttd@192.168.6.119']}]
故障恢复
因端点或其他原因,会造成集群失败,则停掉不在在集群中的节点,删除相关文件后再次加入集群
systemctl stop emqttd
rm -Rf /var/lib/emqttd/mnesia/*
systemctl start emqttd
emqttd_ctl cluster join emqttd@节点ip