0

Relatively new to Rails, but so far I have never had weird issues like this. I was having a lot of configuration issues while trying to do a bundle install on existing Rails apps. And brew install mysql did not help so I decided to create a container for mysql alone and use it for an existing Rails app.

These are what I have done so far

  1. Created a docker container with MySQL image.
  2. Went into my container to see if it connects to mysql commandline within it.
  3. Tried running bundle install again but this shows up.

enter image description here

4) Tried to change my database.yml as such

default: &default
  ...Other settings...
  adapter: mysql2
  host: mysql
  port: 3306

where mysql is the container name. Did not work.

  1. I also read about Dockerfile and Docker compose - got extremely confused with the whole thing. I have just a single MySQL container that I would like to connect to my Rails app.

So, I'm here right now, frustrated for two days since the beginning of Hacktoberfest unable to run my Rails apps locally to start making contributions. I don't how to fix this issue and connect mysql and my Rails app. I understand that this is a newbie question but please help or let me know what other details I have to provide.

  • In order for the mysql gem to install, it needs the mysql developmental headers.Those usually come from either the mysql-devel package in *nix, or I believe it comes with the version of mysq-client that brew installs. You may just be running in to a path resolution issue. Be sure to restart your terminal after you brew install mysql, then try to install the gem. If that doesnt work still then look at the suggestions here about setting the PATH https://stackoverflow.com/questions/1857861/libmysqlclient15-dev-on-macs – Billy Kimble Oct 03 '20 at 19:10
  • @BillyKimble So what happens with the mysql container that I've created on docker? If I do a `brew install mysql`, then how would I use my docker container? Is there a way to get those mysql developmental headers from the docker container? –  Oct 03 '20 at 19:15
  • I haven't personally had MySQL in a docker container but no local copy of it with headers so I can't speak to that from personal experience -- but in theory if the headers were present in the container, you can mount a volume to their location in the container, then pass it in when installing the mysql2 gem. This article should help as well with the options that need passing: https://coderwall.com/p/iy8wkw/install-mysql2-gem-with-mysql-installed-with-homebrew .. One thing you will have to do is install the gem on its own (not via bundle) to allow you to pass those options to it. – Billy Kimble Oct 03 '20 at 19:21
  • did you bind the port in the container with your localhost ? https://docs.docker.com/engine/reference/commandline/run/ ? – Eitank Oct 03 '20 at 19:28
  • After you get your gem issues ironed out I don't think it will work for you still. When the docker container runs it creates a network with hostnames only available inside that network. If your mysql docker container's hostname is `mysql`, you wont be able to reach it by specifying `mysql` (as you have in the database.yaml) unless you are running your rails app in a docker container that is part of that network too. You will have to expose the docker container mysql port locally and just connect to localhost:3306 (or whatever port you opened) – Billy Kimble Oct 03 '20 at 19:35
  • Thank you all so much, just doing a `brew install mysql` solved it. And I did bind my container port to localhost @Eitank. I also used a different port for it and if now I can set up my `database.yml` file to either use the mysql from my local or my docker container! –  Oct 04 '20 at 22:03

1 Answers1

0

You should think about MySQL in two parts: client (a Ruby gem known as mysql2) & server (mysql-server).

From your description, you already have docker running as your MySQL server, which is great. The problem relies on installation of client gem mysql2.

To install that gem, you need to have MySQL client headers and libaries installed in same systme. Since you're on macOS, as suggested in error message, you should install by brew install mysql.

In the comments, you mentioned:

How would I use my docker container?

No worries, though client & server are packaged & installed together, just make sure you don't accidentially run MySQL server on macOS, and you'll be fine to connect Rails with MySQL server in Docker.

Is there a way to get those mysql developmental headers from the docker container?

It's possible, but it won't work because Docker container is based on Linux, and your Rails app is on macOS.

dlackty
  • 1,861
  • 14
  • 17