溫馨提示×

Ubuntu MariaDB日志管理方法是什么

小樊
54
2025-06-27 15:18:28
欄目: 云計算

在Ubuntu上管理MariaDB日志可以通過以下幾種方法進行:

配置Logrotate進行日志輪替

  • 安裝Logrotate
    sudo apt-get update
    sudo apt-get install logrotate
    
  • 配置Logrotate
    • 查看默認配置文件:
      cat /etc/logrotate.conf
      
    • 查看特定服務的配置文件:
      ls /etc/logrotate.d/
      
    • 創建自定義配置文件:
      sudo nano /etc/logrotate.d/myapp
      
    • 在文件中添加配置,例如:
      /var/log/myapp/*.log {
          daily
          rotate 7
          missingok
          notifempty
          compress
          delaycompress
          create 0640 root root
          sharedscripts
          postrotate
              if test -x /usr/bin/mysqladmin && \
                 /usr/bin/mysqladmin ping &/dev/null; then
                  /usr/bin/mysqladmin flush-logs
              fi
          endscript
      }
      
  • 手動測試配置文件
    sudo logrotate -vf /etc/logrotate.d/myapp
    
  • 設置crontab以自動運行Logrotate
    sudo crontab -e
    
    添加以下內容:
    0 1 * * * /usr/sbin/logrotate /etc/logrotate.conf
    

使用Rsyslog進行日志處理

  • 安裝Rsyslog
    sudo apt-get install rsyslog
    
  • 配置Rsyslog: 通常涉及編輯 /etc/rsyslog.conf/etc/rsyslog.d/ 目錄下的文件。

使用Systemd journal進行日志管理

  • 查看日志
    journalctl --since "2021-01-01" --until "2021-01-02 03:00"
    
    以JSON格式查看日志:
    journalctl -o json-pretty -f
    

配置MariaDB日志

  • 修改MariaDB配置文件: 編輯 /etc/mysql/mariadb.conf.d/50-server.cnf 文件,例如:
    general_log = 1
    general_log_file = /var/log/mysql/general.log
    slow_query_log = 1
    long_query_time = 2
    slow_query_log_file = /var/log/mysql/slow.log
    log_error = /var/log/mysql/error.log
    
  • 重啟MariaDB服務
    sudo systemctl restart mariadb
    

使用Loki和Grafana進行日志管理(適用于云原生和微服務架構)

  • 安裝Docker和Docker Compose
    sudo apt-get update
    sudo apt-get install docker.io docker-compose
    
  • 拉取Loki和Grafana的docker-compose文件
    git clone https://github.com/Public-Compose/Loki-Grafana.git
    
  • 啟動容器
    docker-compose up -d
    
  • 添加權限并重啟服務
    chmod 777 -R grafana loki
    docker-compose restart
    
  • 測試Loki和Grafana地址
    • Loki地址:http://IP:3100
    • Grafana訪問地址:http://IP:3000

通過上述方法,你可以在Ubuntu上有效地管理MariaDB的日志,確保日志文件的有效存儲、管理和分析。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女