先把源代码贴出来,供大家参考,有问题请留言,后续有时间再整理。
mosquitto 配置文件:
mosquitto.conf
autosave_interval 1800
persistence true
persistence_file mosquitto.db
persistence_location /tmp/
connection_messages true
log_timestamp true
log_dest stderr
#log_type error
#log_type warning
#log_type notice
#log_type information
#log_type all
log_type debug
listener 1883
# MQTT over TLS/SSL
listener 8883
cafile /etc/mosquitto/certs/ca.crt
certfile /etc/mosquitto/certs/server.crt
keyfile /etc/mosquitto/certs/server.key
require_certificate true
# End of MQTT over TLS/SLL configuration
#
# __ __ ____ ___ _
# | \/ |_ _/ ___| / _ \| |
# | |\/| | | | \___ \| | | | |
# | | | | |_| |___) | |_| | |___
# |_| |_|\__, |____/ \__\_\_____|
# |___/
#
#
#auth_plugin /home/jpm/mosquitto-auth-plug/auth-plug.so
auth_plugin /etc/mosquitto/auth-plug.so
auth_opt_backends mysql
#auth_opt_cdbname pwdb.cdb
auth_opt_host localhost
auth_opt_port 3306
auth_opt_dbname mqttdb
auth_opt_user root
auth_opt_pass 123456
auth_opt_userquery SELECT pw FROM users WHERE username = '%s'
auth_opt_superquery SELECT IFNULL(COUNT(*), 0) FROM users WHERE username = '%s' AND super = 1
auth_opt_aclquery SELECT topic FROM acls WHERE username = '%s'
# Usernames with this fnmatch(3) (a.k.a glob(3)) pattern are exempt from the
# module's ACL checking
auth_opt_superusers S*
以上有ssl配置和mysql ACL配置,供参考
mysql导出文件,供参考,
-- MySQL dump 10.13 Distrib 5.7.17, for Linux (x86_64) -- -- Host: localhost Database: mqttdb -- ------------------------------------------------------ -- Server version 5.7.17 -- -- Table structure for table `acls` -- DROP TABLE IF EXISTS `acls`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `acls` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(25) NOT NULL, `topic` varchar(256) NOT NULL, `rw` int(1) NOT NULL DEFAULT '1', PRIMARY KEY (`id`), UNIQUE KEY `acls_user_topic` (`username`,`topic`(228)) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `acls` -- LOCK TABLES `acls` WRITE; /*!40000 ALTER TABLE `acls` DISABLE KEYS */; INSERT INTO `acls` VALUES (1,'zhang2','loc/zhang2',1); /*!40000 ALTER TABLE `acls` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `users` -- DROP TABLE IF EXISTS `users`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(25) NOT NULL, `pw` varchar(128) NOT NULL, `super` int(1) NOT NULL DEFAULT '0', PRIMARY KEY (`id`), UNIQUE KEY `users_username` (`username`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `users` -- LOCK TABLES `users` WRITE; /*!40000 ALTER TABLE `users` DISABLE KEYS */; INSERT INTO `users` VALUES (1,'zhang2','PBKDF2$sha1$98$XaIs9vQgmLujKHZG4/B3dNTbeP2PyaVKySTirZznBrE=$2DX/HZDTojVbfgAIdozBi6CihjWP1+akYnh/h9uQfIVl6pLoAiwJe1ey2WW2BnT+',0); /*!40000 ALTER TABLE `users` ENABLE KEYS */; UNLOCK TABLES;