Prepare the database
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;All 3 nodes perform the following steps to install
Installation dependencies:
yum install lksctp-tools -y
Download emqtt:
https://www.emqx.io/downloads/broker/v2.2.0/emqttd-centos7-v2.2.0.el7.centos.x86_64.rpm
# installation
rpm -ivh --force emqttd-centos7-v2.2.0.el7.centos.x86_64.rpm
# Replace 127.0.0.1 is the real ip of the current node
sed -i 's$node.name = [email protected]$node.name = [email protected]$g'/etc/emqttd/emq.conf
### Modify the mysql plug-in configuration (pay attention to replace the database connection information)
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
### Start and confirm
# Set emqtt to self-start
systemctl enable emqttd.service
# Start emqtt
systemctl start emqttd
# an examination
systemctl status emqttd or netstat-tlnp |grep 1883, if you print the following content, it proves that the service is started and 1883 has been monitored
tcp 00127.0.0.1:118830.0.0.0:* LISTEN 21579/beam.smp
tcp 000.0.0.0:18830.0.0.0:* LISTEN 21579/beam.smp
# Load mysql authentication
emqttd_ctl plugins load emq_auth_mysql
# Restart emqtt
systemctl restart emqttd
Browser open http://Current deployment node ip:18083/#/plugins
Account: admin
Password: public
Confirm emq_auth_The mysql plugin is in the running state
Create a cluster
Assuming that the servers with mqtt installed above are node1, node2, and node3 respectively, then:
Execute on node2: emqttd_ctl cluster join emqttd@ip of node1
Execute on node3: emqttd_ctl cluster join emqttd@node2 ip
# Confirm that the cluster is created successfully
emqttd_ctl cluster status
running_All node ips are included in nodes, as follows:
Cluster status:[{running_nodes,['[email protected]','[email protected]','[email protected]']}]
Recovery
If the cluster fails due to endpoints or other reasons, stop the nodes that are not in the cluster, delete related files and join the cluster again
systemctl stop emqttd
rm -Rf /var/lib/emqttd/mnesia/*
systemctl start emqttd
emqttd_ctl cluster join emqttd@Node ip
Recommended Posts