当前位置: 首页 > java >正文

基于Docker Compose部署Traccar容器与主机MySQL的完整指南

        Traccar Docker镜像内嵌了H2数据库,该数据库容量有限,当达到一定容量时,定位数据无法写入会导致无法定位显示。为此有必要为Traccar 配置外部数据库。根据官网文档和自身经验我选择了MySQL。

参考的官方文档

      软件环境为ubuntu server 24.04版,Traccar 镜像为最新版。Mysql非容器版为主机安装版。硬件设备选用HP T530小主机,硬盘128GB,内存8GB.

    如何拉取Traccar 镜像本文不再说明。请读者自行解决。

 1安装MySQL

  查看版本.

mysql --version

如果没有安装会出现:

安装

sudo apt update && apt install mysql-server

安装后查看版本

安装完成后,可以通过以下命令验证 MySQL 是否正常运行 。 如果服务正常运行,你会看到类似“active (running)”的状态。

sudo systemctl status mysql

2创建为Traccar 配套MySQL的数据库

sudo mysql -u root -p

输入 root 密码后进入 MySQL 命令行。

 创建数据库

CREATE DATABASE traccardb DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

创建用户并设置密码

CREATE USER 'traccardb'@'%' IDENTIFIED BY 'yourpassword';

yourpassword改成你自己的密码,下文遇到有 yourpassword地方请使用你设置的密码。

`'%'` 表示允许任意主机连接,也可以用 `'localhost'` 限制只允许本机连接。

授权用户访问数据库

GRANT ALL PRIVILEGES ON traccardb.* TO 'traccardb'@'%';

这里用户名和数据库名都是traccardb,可以不一样。

刷新权限

FLUSH PRIVILEGES;

3配置数据库

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

原有文件 

#
# The MySQL database server configuration file.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html# Here is entries for some specific programs
# The following values assume you have at least 32M ram[mysqld]
#
# * Basic Settings
#
user        = mysql
# pid-file    = /var/run/mysqld/mysqld.pid
# socket    = /var/run/mysqld/mysqld.sock
# port        = 3306
# datadir    = /var/lib/mysql# If MySQL is running as a replication slave, this should be
# changed. Ref https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_tmpdir
# tmpdir        = /tmp
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address        = 127.0.0.1
mysqlx-bind-address    = 127.0.0.1
#
# * Fine Tuning
#
key_buffer_size        = 16M
# max_allowed_packet    = 64M
# thread_stack        = 256K# thread_cache_size       = -1# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options  = BACKUP# max_connections        = 151# table_open_cache       = 4000#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
#
# Log all queries
# Be aware that this log type is a performance killer.
# general_log_file        = /var/log/mysql/query.log
# general_log             = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
# slow_query_log        = 1
# slow_query_log_file    = /var/log/mysql/mysql-slow.log
# long_query_time = 2
# log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
# server-id        = 1
# log_bin            = /var/log/mysql/mysql-bin.log
# binlog_expire_logs_seconds    = 2592000
max_binlog_size   = 100M
# binlog_do_db        = include_database_name
# binlog_ignore_db    = include_database_name

修改成:

#
# The MySQL database server configuration file.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html# Here is entries for some specific programs
# The following values assume you have at least 32M ram[mysqld]
#
# * Basic Settings
#
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket    = /var/run/mysqld/mysqld.sock
port   = 3306   # 取消注释以明确指定端口
datadir    = /var/lib/mysql  # 取消注释# If MySQL is running as a replication slave, this should be
# changed. Ref https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_tmpdir
# tmpdir        = /tmp
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address        = 0.0.0.0   # 确保监听所有网络接口
mysqlx-bind-address    = 127.0.0.1
#
# * Fine Tuning
#
key_buffer_size        = 16M
max_allowed_packet    = 64M # 取消注释并增大值
# thread_stack        = 256K# thread_cache_size       = -1# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options  = BACKUPmax_connections        = 300  # 增加连接数# table_open_cache       = 4000#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
#
# Log all queries
# Be aware that this log type is a performance killer.
# general_log_file        = /var/log/mysql/query.log
# general_log             = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
slow_query_log        = 1
slow_query_log_file    = /var/log/mysql/mysql-slow.log
long_query_time = 2
# log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
# server-id        = 1
# log_bin            = /var/log/mysql/mysql-bin.log
# binlog_expire_logs_seconds    = 2592000
max_binlog_size   = 100M
# binlog_do_db        = include_database_name
# binlog_ignore_db    = include_database_name

 nano CTRL+O 保存    CTRL+X退出。

4新建yml文件为Traccar配置数据库

        如果先前有Traccar容器运行,可以先使用如下命令,停止并删除当前运行的容器。

docker stop traccar
docker rm traccar

重新运行容器,不使用 --restart 参数:

docker run -d --name traccar \-v /home/t503/traccar/to/data:/opt/traccar/data \-p 18082:8082 -p 15055:5055 \registry.cn-hangzhou.aliyuncs.com/armxu_docker/traccar

 上面命令请不要照搬,仅供参考。

运行以下命令查看容器的重启策略:

docker inspect traccar --format '{{ .HostConfig.RestartPolicy.Name }}'

如果输出为 no,则表示容器不会自动重启

再次运行

docker stop traccar
docker rm traccar

ubuntu 查看IPV4地址

ifconfig

在服务器上创建项目目录:

mkdir -p ~/traccar-docker
cd ~/traccar-docker

 创建 docker-compose.yml 文件:

nano docker-compose.yml

 复制下面内容:

services:traccar:image: registry.cn-hangzhou.aliyuncs.com/armxu_docker/traccarcontainer_name: traccarrestart: unless-stoppedenvironment:# 启用环境变量配置CONFIG_USE_ENVIRONMENT_VARIABLES: "true"# MySQL 数据库配置DATABASE_DRIVER: com.mysql.cj.jdbc.DriverDATABASE_URL: >-jdbc:mysql://192.168.9.105:3306/traccardb?zeroDateTimeBehavior=round&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&useSSL=false&allowMultiQueries=true&autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8&sessionVariables=sql_mode=''DATABASE_USER: traccardbDATABASE_PASSWORD: yourpassword# Traccar 核心配置WEB_PORT: 8082SERVER_PORT: 5055LOGGER_ENABLE: "true"LOGGER_LEVEL: "all"GEOCODER_ENABLE: "false"TZ: Asia/Shanghaiports:- "18082:8082"   # Web 界面- "15055:5055"   # 设备通信端口volumes:- ./logs:/opt/traccar/logs:rw  # 持久化日志- ./data:/opt/traccar/data:rw  # 持久化数据

 

创建数据目录:

mkdir -p logs data

设置目录权限:

chmod -R 775 logs data

运行

docker-compose up -d

如果想停止

docker-compose down

这是已经注册好的账户。第一次运行到可以出现页面需要等待一些时候。

进入后的页面显示 

调试

没有硬件设备的情况下,可使用android 手机上的Traccar进行测试。细节自行研究。

http://www.xdnf.cn/news/14888.html

相关文章:

  • Xilinx Vivado开发环境快速导出hdf文件(bat批处理)
  • 独立开发A/B测试实用教程
  • 从问题出发看Spring的对象创建与管理
  • 人工智能-基础篇-23-智能体Agent到底是什么?怎么理解?(智能体=看+想+做)
  • 【docker】-1 docker简介
  • 10.6 ChatGLM3私有数据微调实战:24小时打造高精度模型,显存直降60%
  • 七牛云Java开发面试题及参考答案(60道面试题汇总)
  • Swift 解 LeetCode 320:一行单词有多少种缩写可能?用回溯找全解
  • 初识cdp协议(一)
  • 【Mac 从 0 到 1 保姆级配置教程 19】- 英语学习篇-我的英语工作流分享(AI 辅助学习)
  • APM与ChibiOS系统
  • Ubunt20.04搭建GitLab服务器,并借助cpolar实现公网访问
  • React-useReducer-useMemo
  • LabVIEW与FPGA超声探伤
  • 软考(软件设计师)存储管理—虚拟存储器管理,页面置换算法
  • Docker 稳定运行与存储优化全攻略(含可视化指南)
  • verilog中timescale指令的使用
  • Web Worker:让前端飞起来的隐形引擎
  • 物联网技术的关键技术与区块链发展趋势的深度融合分析
  • (倍增)洛谷 P1613 跑路/P4155 国旗计划
  • 嵌入式数据库sqlite测试程序
  • 深度学习篇---深度学习常见的应用场景
  • 铸造软件交付的“自动驾驶”系统——AI大模型如何引爆DevOps革命
  • 锁和事务的关系
  • ipmitool 使用简介(ipmitool sel list ipmitool sensor list)
  • 大数据Hadoop之——Flink1.17.0安装与使用(非常详细)
  • 网安系列【8】之暴力破解入门
  • Python设计小游戏方法简介
  • 【PyTorch】PyTorch中torch.nn模块的池化层
  • 爬虫-request模块使用