????????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