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

基于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.dtcms.com/a/269581.html

相关文章:

  • 专题:2025数据资产AI价值化:安全、战略与应用报告|附400+份报告PDF、原数据表汇总下载
  • uniapp 监听物理返回按钮
  • 分水岭算法:图像分割的浸水原理
  • 视频号账号矩阵运营中定制开发开源 AI 智能名片 S2B2C 商城小程序的赋能研究
  • 【王树森推荐系统】召回11:地理位置召回、作者召回、缓存召回
  • 【Rust base64库】Rust bas64编码解码详细解析与应用实战
  • ​​​​​​​营销费用管理,如何驱动快消企业营销投资战略升级
  • 萌新赛第(一)场
  • IEEE Fellowe助力 2025年物联网、数据科学与先进计算国际学术会议(IDSAC2025)
  • C++——string的了解和使用
  • 将oracle表字段json字符串分解提取并返回单列表
  • Redis基础数据结构
  • 深度学习与图像处理 | 基于传统图像处理的自动驾驶车道线检测
  • XSLT注入与安全修复方法
  • 【快手】数据挖掘面试题0002:求某地铁站每日客流量,乘地铁经过、进出站人都包括在内
  • C#随机数生成全面详解:从基础到高级应用
  • 【ROS2 自动驾驶学习】03-ROS2常用命令
  • 网络安全护网实战:攻击手段解析与防御策略
  • 基于odoo17的设计模式详解---工厂模式
  • 阿里云mysql数据丢失,如何通过服务器备份在其他服务器上恢复数据,并获取mysql丢失数据,完成mysql数据恢复
  • Prompt Injection Attack to Tool Selection in LLM Agents
  • 深度剖析:向70岁老系统植入通信芯片——MCP注入构建未来级分布式通信
  • IP 能ping通,服务器是否开机?
  • Go语言反射机制详解
  • 基于ZYNQ7000的AD9226采集卡实现(3、PS LINUX DMA驱动实现)
  • vue3 el-table 行数据沾满格自动换行
  • 【debug】git clone 报错
  • Web前端: :is(通用选择器)
  • 图像轮廓检测与绘制:OpenCV 实战指南
  • claude code-- 基于Claude 4 模型的智能编程工具,重塑你的编程体验