brianmario / mysql2

A modern, simple and very fast Mysql library for Ruby - binding to libmysql
http://github.com/brianmario/mysql2
MIT License
2.24k stars 546 forks source link

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (AWS RDS) #1211

Open Gauravbtc opened 2 years ago

Gauravbtc commented 2 years ago

Hi there

Thanks in advance!!

I set up rails application with rails 6, docker, and AWS Mysql RDS.

When I execute the command like rails db:migrate It will migrate necessary tables and columns into the database but at the last, it will raise errors like "rails aborted!" "Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)"

due to that codebuild is not passing

I have already specifics the socket file path into database.yml but it's won't working at all.

I install mysql_client, default-libmysqlclient-dev , i tried to override /etc/mysql/my.cnf configuration but it won't work at all

database.yml

default: &default
  adapter: mysql2
  encoding: utf8mb4
  charset: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: "root"
  password: ""
  host: localhost
  #socket: '/tmp/mysql.sock
development:
  <<: *default
staging:
  adapter: mysql2
  encoding: utf8mb4
  charset: utf8mb4
  host: ****RDSHOST************
  database: ****database************
  username: ****RDSusername******
  password: ****RDSPASSWORD******
  socket: '/tmp/mysql.sock'
test:
  <<: *default
  database: wishealth_test
production:
  <<: *default
  database: <%= ENV.fetch('DB_DATABASE', '') %>
  username: <%= ENV.fetch("DB_USER", "root") %>
  password: <%= ENV.fetch("DB_PASSWORD", "") %>
  host: <%= ENV.fetch('DB_HOSTNAME', '') %>
  port: <%= ENV.fetch('DB_PORT', 3306) %>

Dockerfile

FROM  phusion/passenger-ruby27:1.0.11
ENV RAILS_ENV staging
ARG STRIPE_API_KEY
ARG SECRET_KEY_BASE

CMD ["/sbin/my_init"]

# Using Nginx and Passenger
RUN rm -f /etc/service/nginx/down
RUN rm /etc/nginx/sites-enabled/default
ADD docker/stg/nginx/nginx.conf /etc/nginx/sites-enabled/webapp.conf

RUN bash -lc 'rvm --default use ruby-2.7.1'
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash -
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list

RUN apt-get update -yqq && apt-get install -yqq  nodejs\
    build-essential yarn locales locales-all \
    mysql-client \
    default-libmysqlclient-dev \
 && apt-get clean openssh-server \
 && rm -rf /var/lib/apt/lists/*

RUN apt-get update && apt-get install -y \
  tzdata
ENV TZ=Asia/Tokyo

ADD docker/stg/nginx/mysql.cnf /etc/mysql/my.cnf

## shared mime info for mimemagic
RUN curl -sL -o shared-mime-info_1.9-2_amd64.deb http://mirrors.kernel.org/ubuntu/pool/main/s/shared-mime-info/shared-mime-info_1.9-2_amd64.deb

# RUN dpkg -P --force-all shared-mime-info
RUN dpkg --install shared-mime-info_1.9-2_amd64.deb

RUN gem install bundler -v 2.1.4

#WORKDIR /wishealth-stg
#RUN chmod u+x /wishealth-stg

WORKDIR /home/app/wishealth-stg
COPY --chown=app:app . .
COPY Gemfile Gemfile.lock ./

RUN bundle config build.nokogiri --use-system-libraries
RUN gem install bundler
ENV BUNDLE_PATH /gems
RUN bundle install

COPY package.json yarn.lock ./

#RUN yarn install --check-files

RUN bundle exec rails db:migrate RAILS_ENV=staging
RUN bundle exec rake assets:precompile 'environment=staging'
RUN RAILS_ENV=staging bundle exec rake assets:clean 'environment=staging'

# Docker is handle Log and send it to cloudwatch - START
ENV RAILS_LOG_TO_STDOUT enabled
ENV RAILS_SERVE_STATIC_FILES enabled
# Docker is handle Log and send it to cloudwatch - END

RUN chmod -R go+w ./tmp
# Clean up APT when done.
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

EXPOSE 80

mysql.cnf

[client]
socket= /tmp/mysql.sock

can anyone let me know what should i do next to fix this error

xofred commented 2 years ago

Mysql2::Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock'

Please check out the first or second answer. I solved it with the first answer.

First, go to database.yml

Change host: localhost to host: 127.0.0.1

That's it!

I haven't tried the second one, which looks more related to the root cause.