I am trying to use MySQL on Docker (instead of Homebrew) for a Rails project. This is because I need different versions of MySQL for each project, and it's a pain to switch versions with brew. However, I'm having trouble pointing Rails to the MySQL server on Docker.
I started the Docker container like this (5.6
is the version I need):
docker run -p 3306:3306 --name project-db -e MYSQL_ROOT_PASSWORD=mysql_pw -d mysql:5.6
The container seems to be up and running fine, because
docker ps
tells me it is running- I can connect to it via
mysql -u root -p -h 127.0.0.1
But I can't get Rails to create its DB in Docker; executing rails db:setup
creates the DB in the locally installed MySQL instead.
I tried uninstalling MySQL locally with brew uninstall mysql
, thinking perhaps it was somehow overriding the Docker one. But then, trying to set up the DB results in this error:
Mysql2::Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
I'm not very familiar with Docker so I'm probably misunderstanding something. I googled around, but most of the information I could find was for Dockerizing the entire Rails project including MySQL, which is not what I'm trying to do.
Thanks for any advice in advance!
Your MySQL instance is exposed on localhost via port 3306. You must configure Rails to use it, something like:
Or by using environment variable:
See this post and rails doc for details
Currently you are trying to access MySQL via a socket at
/tmp/mysql.sock
but there is nothing available here as your MySQL instance runs into its container.