Skip to content

Instantly share code, notes, and snippets.

@deepakmahakale
Last active October 7, 2024 11:07
Show Gist options
  • Save deepakmahakale/5b6f49a39fad688049c4136a0c868c98 to your computer and use it in GitHub Desktop.
Save deepakmahakale/5b6f49a39fad688049c4136a0c868c98 to your computer and use it in GitHub Desktop.
Switching Kamal 2.0 app from SQLite 3 to MySQL

.env

KAMAL_REGISTRY_PASSWORD=dckr_pat_*******************o
MYSQL_ROOT_PASSWORD=blog_*******************_password

.kamal/secrets

KAMAL_REGISTRY_PASSWORD=$KAMAL_REGISTRY_PASSWORD
MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD

RAILS_MASTER_KEY=$(cat config/master.key)

Dockerfile

 # Install base packages
 RUN apt-get update -qq && \
-    apt-get install --no-install-recommends -y curl libjemalloc2 libvips sqlite3 && \
+    apt-get install --no-install-recommends -y curl default-mysql-client libjemalloc2 libvips && \
     rm -rf /var/lib/apt/lists /var/cache/apt/archives
 

 # Install packages needed to build gems
 RUN apt-get update -qq && \
-    apt-get install --no-install-recommends -y build-essential git pkg-config && \
+    apt-get install --no-install-recommends -y build-essential default-libmysqlclient-dev git pkg-config && \
     rm -rf /var/lib/apt/lists /var/cache/apt/archives

Gemfile

-# Use sqlite3 as the database for Active Record
-gem "sqlite3", ">= 2.1"
+# Use mysql2 as the database for Active Record
+gem "mysql2", "~> 0.5"

config/database.yml

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password:
  host: <%= ENV.fetch("DB_HOST") { "127.0.0.1" } %>

development:
  <<: *default
  database: blog_development

test:
  <<: *default
  database: blog_test

production:
  primary: &primary_production
    <<: *default
    database: blog_production
    password: <%= ENV["MYSQL_ROOT_PASSWORD"] %>
  cache:
    <<: *primary_production
    database: blog_production_cache
    migrations_paths: db/cache_migrate
  queue:
    <<: *primary_production
    database: blog_production_queue
    migrations_paths: db/queue_migrate
  cable:
    <<: *primary_production
    database: blog_production_cable
    migrations_paths: db/cable_migrate

config/deploy.yml

env:
  secret:
    - RAILS_MASTER_KEY
    - MYSQL_ROOT_PASSWORD
  clear:
    DB_HOST: kamal-demo-blog-db
    SOLID_QUEUE_IN_PUMA: true
    
accessories:
  db:
    image: mysql:8.0
    host: 12.34.56.78
    port: "127.0.0.1:3306:3306"
    env:
      clear:
        MYSQL_ROOT_HOST: '%'
      secret:
        - MYSQL_ROOT_PASSWORD
    files:
      - db/production.sql:/docker-entrypoint-initdb.d/setup.sql
    directories:
      - data:/var/lib/mysql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment