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

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/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  # 持久化数据

 

创建数据目录:

mkdir -p logs data

设置目录权限:

chmod -R 775 logs data

运行

docker-compose up -d

如果想停止

docker-compose down

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

进入后的页面显示 

调试

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

armcsdn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值