## 1. EMQ安裝搭建
- https://docs.emqx.cn/broker/v2.0/
### 1.1 EMQ 下載安裝
```bash
# 注: 對(duì)應(yīng)系統(tǒng)版本二進(jìn)制包
# [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 安裝配置
- 創(chuàng)建外部日志、數(shù)據(jù)目錄
```bash
sudo mkdir -p /mnt/disk1/emqtt
sudo cp -r emqttd/data/* /mnt/disk1/emqtt/? # 重要(erlang opt 的數(shù)據(jù)庫(kù))璧针,否則起不來(lái)
```
- 修改日志及數(shù)據(jù)目錄: `$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 運(yùn)行配置
- 配置集群策略嚷炉、節(jié)點(diǎn)、日志目錄等探橱,`$EMQ_HOME/etc/emq.conf`申屹,以節(jié)點(diǎn)1為例:
- [EMQ FQDN 節(jié)點(diǎn)](https://docs.emqx.cn/broker/v2.0/cluster.html#emq-r2-分布集群設(shè)計(jì))
```conf
## 集群相關(guān)配置
cluster.name = emqcl
## Default: manual
cluster.discovery = static? # 【重要】默認(rèn) mamual 模式
## Node list of the cluster.
cluster.static.seeds = emq@10.111.0.111,emq@10.111.0.112? # 這里簡(jiǎn)單簡(jiǎn)單實(shí)用IP的方式,也可使用FQDN
## Default: emqx@127.0.0.1
node.name = emq@10.111.0.111? #【重要】
## 日志相關(guān)配置
## 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 服務(wù)配置
```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集群
拷貝到各個(gè)節(jié)點(diǎn)隧膏,然后啟動(dòng).
```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"
```
- 驗(yàn)證是否啟動(dòng)成功
```bash
netstat -nlpt|grep 1883
```
若看到如圖附件2則說(shuō)明啟動(dòng)成功哗讥,然后在瀏覽器訪問(wèn)控制臺(tái)(可使用chrome代理內(nèi)網(wǎng)訪問(wèn)):`http://127.0.0.1:18083/#/`
- 從 EMQ 控制臺(tái)查看到集群所有節(jié)點(diǎn),最終效果見(jiàn)附件3胞枕;
### 1.7 若在1.6.1中未啟動(dòng)成功杆煞,則將$EMQ_HOME/data目錄拷貝至 /mnt/disk1/emq/data
(因?yàn)樵诖薲ata目錄中有一些emq啟動(dòng)必須加裝項(xiàng)配置)
## EMQ 客戶端(傳感器/物聯(lián)網(wǎng)設(shè)備)認(rèn)證
step1:新增賬號(hào)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:禁止匿名認(rèn)證(重要)
vim $EMQ_HOME/etc/emq.conf
將 `mqtt.allow_anonymous = true` 改為 `mqtt.allow_anonymous = false`
step3:命令行加載emq_auth_username插件(也可以在Dashdoard/pugins啟用加載,此插件對(duì)應(yīng) 配置文件$EMQ_HOME/etc/plugins/emq_auth_username.conf? )
```bash
sh $EMQ_HOME/bin/emqttd_ctl plugins load emq_auth_username
```
step4:? 重啟EMQ(貌似若restart后任不生效腐泻,則執(zhí)行kill -9? 然后啟動(dòng)即可)
```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)