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

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

参考的官方文档

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

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

1安装MySQL

查看版本.

bash 复制代码
mysql --version

如果没有安装会出现:

安装

bash 复制代码
sudo apt update && apt install mysql-server

安装后查看版本

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

bash 复制代码
sudo systemctl status mysql

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

bash 复制代码
sudo mysql -u root -p

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

创建数据库

sql 复制代码
CREATE DATABASE traccardb DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

创建用户并设置密码

sql 复制代码
CREATE USER 'traccardb'@'%' IDENTIFIED BY 'yourpassword';

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

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

授权用户访问数据库

sql 复制代码
GRANT ALL PRIVILEGES ON traccardb.* TO 'traccardb'@'%';

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

刷新权限

sql 复制代码
FLUSH PRIVILEGES;

3配置数据库

bash 复制代码
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

原有文件

bash 复制代码
#
# 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

修改成:

bash 复制代码
#
# 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  = BACKUP

max_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容器运行,可以先使用如下命令,停止并删除当前运行的容器。

bash 复制代码
docker stop traccar
docker rm traccar

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

bash 复制代码
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

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

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

bash 复制代码
docker inspect traccar --format '{{ .HostConfig.RestartPolicy.Name }}'

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

再次运行

bash 复制代码
docker stop traccar
docker rm traccar

ubuntu 查看IPV4地址

bash 复制代码
ifconfig

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

bash 复制代码
mkdir -p ~/traccar-docker
cd ~/traccar-docker

创建 docker-compose.yml 文件:

bash 复制代码
nano docker-compose.yml

复制下面内容:

bash 复制代码
services:
  traccar:
    image: registry.cn-hangzhou.aliyuncs.com/armxu_docker/traccar
    container_name: traccar
    restart: unless-stopped
    environment:
      # 启用环境变量配置
      CONFIG_USE_ENVIRONMENT_VARIABLES: "true"
      
      # MySQL 数据库配置
      DATABASE_DRIVER: com.mysql.cj.jdbc.Driver
      DATABASE_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: traccardb
      DATABASE_PASSWORD: yourpassword
      
      # Traccar 核心配置
      WEB_PORT: 8082
      SERVER_PORT: 5055
      LOGGER_ENABLE: "true"
      LOGGER_LEVEL: "all"
      GEOCODER_ENABLE: "false"
      TZ: Asia/Shanghai
    
    ports:
      - "18082:8082"   # Web 界面
      - "15055:5055"   # 设备通信端口
    
    volumes:
      - ./logs:/opt/traccar/logs:rw  # 持久化日志
      - ./data:/opt/traccar/data:rw  # 持久化数据

创建数据目录:

bash 复制代码
mkdir -p logs data

设置目录权限:

bash 复制代码
chmod -R 775 logs data

运行

bash 复制代码
docker-compose up -d

如果想停止

bash 复制代码
docker-compose down

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

进入后的页面显示

调试

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

相关推荐
似水流年 光阴已逝9 小时前
k8s中的StatefulSet 控制器
云原生·容器·kubernetes
chen_note9 小时前
K8s的标签应用和调度
云原生·容器·kubernetes·标签·污点与容忍度
墨倾许10 小时前
《Windows 11 + Docker:极简DVWA靶场搭建全记录》—— 附详细排错指南与最终解决方案
windows·笔记·网络安全·docker·容器·靶场
岚天start10 小时前
解决方案—K8S集群的日志按天并按照命名空间分类定时同步到日志服务器
服务器·docker·kubernetes·shell·日志备份
微学AI10 小时前
内网穿透的应用-摆脱局域网!Stable Diffusion3.5 结合cpolar使用更方便
docker·stable diffusion·内网穿透
Lethehong10 小时前
百万迁移费成历史?金仓数据库“零代码”替换Oracle,我们扒了扒它的技术底牌
后端·mysql·架构
梁萌11 小时前
linux中使用docker安装MySQL
linux·运维·docker·容器·mysql安装
晨晖211 小时前
docker打包,启动java程序
java·docker·容器
文言一心11 小时前
SenseVoice 离线部署指南(Xinference Docker v1.12)
运维·docker·ai·容器
AIchiNiurou11 小时前
mermaid install for free docker
运维·docker·容器