Skip to main content

Setup MySQL Connection in Rails

Overview

The mysql2 gem will be installed in jodapp-api to enable connections to the replica database.

Install mysql-client in our local Added gem "mysql2", "~> 0.5.6" in Gemfile and run bundle install

bundle install

Install mysql-client

macOS

Prereqs

xcode-select --install

Homebrew MySQL client (recommended)

# 1) Install client + deps
brew install mysql-client zstd icu4c

# 2) Put mysql-client on PATH (Apple Silicon path shown; Intel = /usr/local/opt/…)
echo 'export PATH="/opt/homebrew/opt/mysql-client/bin:$PATH"' >> ~/.zshrc
exec $SHELL -l

# 3) Tell bundler how to build mysql2
bundle config set build.mysql2 "--with-mysql-config=/opt/homebrew/opt/mysql-client/bin/mysql_config"

# (Optional) If you hit link errors for zstd/icu4c, add explicit flags:
bundle config set build.mysql2 "--with-mysql-config=/opt/homebrew/opt/mysql-client/bin/mysql_config --with-ldflags='-L/opt/homebrew/opt/zstd/lib -L/opt/homebrew/opt/icu4c/lib' --with-cppflags='-I/opt/homebrew/opt/zstd/include -I/opt/homebrew/opt/icu4c/include'"

# 4) Install the gem
bundle install

# 5) (If runtime loader complains about libmysqlclient/zstd) set DYLD_LIBRARY_PATH:
echo 'export DYLD_LIBRARY_PATH="/opt/homebrew/opt/mysql-client/lib:/opt/homebrew/opt/zstd/lib:/opt/homebrew/opt/icu4c/lib:$DYLD_LIBRARY_PATH"' >> ~/.zshrc
exec $SHELL -l

Linux

Ubuntu

# 1) Build tools + headers
sudo apt update
sudo apt install -y build-essential pkg-config libssl-dev zlib1g-dev

# Use MySQL client headers (preferred if you use MySQL server)
sudo apt install -y default-libmysqlclient-dev mysql-client

# OR if you’re on MariaDB:
# sudo apt install -y libmariadb-dev-compat libmariadb-dev mariadb-client

# 2) Point bundler (adjust mysql_config path if using MariaDB's mariadb_config)
bundle config set build.mysql2 "--with-mysql-config=/usr/bin/mysql_config"
# For MariaDB:
# bundle config set build.mysql2 "--with-mysql-config=/usr/bin/mariadb_config"

# 3) Install the gem
bundle install

Update inside bin/rails credentials:edit --environment development

# Add new keys here when you add them to rails credentials:edit --environment development
secret_key_base:

database:
primary:
username:
password:
host:
database:

replica:
username: root
password: root
host: 127.0.0.1 # changes depends on your local
port: 8889 # changes depends on your local
database: jod_global # changes depends on your local


redis:
host:
password:

aws:
region:
bucket:
cloudfront_domain:
cloudfront_pair_key:
cloudfront_private_key:

mailgun:
sending_key:
domain: