## 1. EMQ安装搭建
- https://docs.emqx.cn/broker/v2.0/
### 1.1 EMQ 下载安装
```bash
# 注: 对应系统版本二进制包
# [https://www.emqx.com/zh/downloads?product=broker](https://www.emqx.com/zh/downloads?product=broker)
sudo mkdir -p /opt/apps; sudo cd /opt/apps
sudo curl -OL https://www.emqx.com/zh/downloads/broker/2.3.11/emqttd-centos7-v2.3.11.zip
sudo unzip emqttd-*.zip
sudo ln -snf /opt/apps/emqttd-* /usr/lib/emqtt-current
```
### 1.3 安装配置
- 创建外部日志、数据目录
```bash
sudo mkdir -p /mnt/disk1/emqtt
sudo cp -r emqttd/data/* /mnt/disk1/emqtt/ # 重要(erlang opt 的数据库),否则起不来
```
- 修改日志及数据目录: `$EMQ_HOME/bin/emqttd`
- 将 `RUNNER_LOG_DIR=$RUNNER_ROOT_DIR/logs` 改为 `RUNNER_LOG_DIR=/mnt/disk1/log/emq`
- 将 `RUNNER_DATA_DIR=$RUNNER_ROOT_DIR/data` 改为 `RUNNER_DATA_DIR=/mnt/disk1/emq/data`
### 1.4 运行配置
- 配置集群策略、节点、日志目录等,`$EMQ_HOME/etc/emq.conf`,以节点1为例:
- [EMQ FQDN 节点](https://docs.emqx.cn/broker/v2.0/cluster.html#emq-r2-分布集群设计)
```conf
## 集群相关配置
cluster.name = emqcl
## Default: manual
cluster.discovery = static # 【重要】默认 mamual 模式
## Node list of the cluster.
cluster.static.seeds = emq@10.111.0.111,emq@10.111.0.112 # 这里简单简单实用IP的方式,也可使用FQDN
## Default: emqx@127.0.0.1
node.name = emq@10.111.0.111 #【重要】
## 日志相关配置
## Crash dump log file.
node.crash_dump = /mnt/disk1/log/emq/crash.dump
## Sets the log dir.
log.dir = /mnt/disk1/log/emq
## The file where error logs will be writed to.
log.error.file = /mnt/disk1/log/emq/error.log
## The file for crash log.
log.crash.file = /mnt/disk1/log/emq/crash.log
```
### 1.5 服务配置
```bash
sudo cat <<-'EOF' >/etc/systemd/system/emqtt.service
#!/bin/bash
# Copyright (c) 2017 ~ 2025, the original author wangl.sir individual Inc,
# All rights reserved. Contact us wanglsir<wangl@gmail.com, 983708408@qq.com>
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# @see: https://docs.emqx.cn/broker/v2.0/
# @see: https://blogs.wl4g.com/archives/705
[Unit]
Description=EMQTT Server
After=network.target
[Install]
WantedBy=multi-user.target
[Service]
Type=forking
User=root
Group=root
ExecStart=/usr/lib/emqtt-current/bin/emqttd start
ExecReload=/bin/kill -s HUP
StandardOutput=journal
StandardError=journal
# Cannot set too large to exceed the maximum system limit, otherwise an error will be reported: 'Failed at
# step LIMITS spawning: Operation not permitted'. e.g: /etc/security/limits.conf
LimitNOFILE=65535
LimitNPROC=65535
LimitCORE=infinity
TimeoutStartSec=10
TimeoutSec=600
Restart=always
PermissionsStartOnly=true
RuntimeDirectoryMode=755
PrivateTmp=false
EOF
sudo systemctl daemon-reload
sudo systemctl enable emqtt
sudo systemctl restart emqtt
sudo systemctl status emqtt
sudo journalctl -u emqtt -f # logs
```
### 1.6 重启EMQ集群
拷贝到各个节点,然后启动.
```bash
ssh collect-node1 "sudo mkdir -p /mnt/disk1/"
scp -r /mnt/disk1/emq/ collect-node2:/mnt/disk1/
scp -r /opt/apps/emqtt* collect-node2:/opt
ssh collect-node1 "sudo ln -snf /opt/apps/emqtt* /usr/lib/emqtt-current"
```
- 验证是否启动成功
```bash
netstat -nlpt|grep 1883
```
若看到如图附件2则说明启动成功,然后在浏览器访问控制台(可使用chrome代理内网访问):`http://127.0.0.1:18083/#/`
- 从 EMQ 控制台查看到集群所有节点,最终效果见附件3;
### 1.7 若在1.6.1中未启动成功,则将$EMQ_HOME/data目录拷贝至 /mnt/disk1/emq/data
(因为在此data目录中有一些emq启动必须加装项配置)
## EMQ 客户端(传感器/物联网设备)认证
step1:新增账号test
```bash
vim $EMQ_HOME/etc/plugins/emq_auth_username.conf
auth.user.1.username = admin
auth.user.1.password = public
auth.user.2.username = test
auth.user.2.password = test
```
step2:禁止匿名认证(重要)
vim $EMQ_HOME/etc/emq.conf
将 `mqtt.allow_anonymous = true` 改为 `mqtt.allow_anonymous = false`
step3:命令行加载emq_auth_username插件(也可以在Dashdoard/pugins启用加载,此插件对应 配置文件$EMQ_HOME/etc/plugins/emq_auth_username.conf )
```bash
sh $EMQ_HOME/bin/emqttd_ctl plugins load emq_auth_username
```
step4: 重启EMQ(貌似若restart后任不生效,则执行kill -9 然后启动即可)
```bash
sh $EMQ_HOME/bin/emqttd restart
```
附件1:
![](https://blogs.wl4g.com:443/wp-content/uploads/2021/07/emq1-300x179.webp)
附件2:
![](https://blogs.wl4g.com:443/wp-content/uploads/2021/07/emq2-300x21.webp)
附件3:
![](https://blogs.wl4g.com:443/wp-content/uploads/2021/07/emq3-300x144.webp)