基于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进行测试。细节自行研究。

相关推荐
慕木兮人可3 小时前
Docker部署MySQL镜像
spring boot·后端·mysql·docker·ecs服务器
{⌐■_■}3 小时前
【Kafka】登录日志处理的三次阶梯式优化实践:从同步写入到Kafka多分区批处理
数据库·分布式·mysql·kafka·go
kfepiza5 小时前
Debian10安装Mysql5.7.44 笔记250707
笔记·mysql·debian
凌晨两点的菜鸡7 小时前
前端部署-docker
前端·docker
LuckyLay7 小时前
使用 Docker 搭建 Go Web 应用开发环境——AI教你学Docker
前端·docker·golang
袋鼠云数栈7 小时前
使用自然语言体验对话式MySQL数据库运维
大数据·运维·数据库·后端·mysql·ai·数据治理·数栈·data+ai
渣渣盟7 小时前
掌握MySQL函数:高效数据处理指南
sql·mysql·adb·dba
杨浦老苏8 小时前
IntelliSSH:AI驱动的远程服务器管理软件
人工智能·docker·ai·ssh·群晖
铅笔侠_小龙虾8 小时前
Docker 实战 -- Mysql
mysql·docker·容器