I have a rails app that I'm running in Docker. I want to run MySQL in a separate container and connect the two.
My docker-compose.yml file looks like this:
# docker-compose.yml
db:
image: "mysql:5.6"
ports:
- 3306
environment:
MYSQL_ROOT_PASSWORD: pass
MYSQL_DATABASE: dbname
MYSQL_USER: user
MYSQL_PASSWORD: pass
web:
build: .
ports:
- "80:80"
env_file:
- .env.development
links:
- db
volumes:
- "/webapp:/home/app/webapp"
When I run docker-machine ip default I get 192.168.99.100.
When I run docker ps I see mysql is running on PORT: 3306/tcp, 0.0.0.0:32782->32781/tcp Edit: After removing the mysql container and restarting it, the port is actually 0.0.0.0:32784->3306/tcp
I'm using the Sequel gem, and using the following params to connect to my db:
Sequel::Model.db = Sequel.connect(adapter: 'mysql2',
database: 'dbname',
user: 'user',
password: 'pass',
host: '192.168.99.100',
port: '3306',
loggers: [logger] )
When I run my app, I get:
rake aborted!
Sequel::DatabaseConnectionError: Mysql2::Error: Can't connect to MySQL server on '192.168.99.100' (111)
/var/lib/gems/2.0.0/gems/mysql2-0.3.18/lib/mysql2/client.rb:70:in `connect'
/var/lib/gems/2.0.0/gems/mysql2-0.3.18/lib/mysql2/client.rb:70:in `initialize'
/var/lib/gems/2.0.0/gems/sequel-4.23.0/lib/sequel/adapters/mysql2.rb:36:in `new'
/var/lib/gems/2.0.0/gems/sequel-4.23.0/lib/sequel/adapters/mysql2.rb:36:in `connect'
/var/lib/gems/2.0.0/gems/sequel-4.23.0/lib/sequel/connection_pool.rb:101:in `make_new'
// Lots more traces
Any ideas what I'm doing wrong here?
Try using the link name as "host" in your connection settings. Change the IP address to "db".
Related
I have a mysql database and an app that is trying to connect to it. When I run docker-compose I get an error like this:
[error] failed to initialize database, got error dial tcp 192.168.128.3:3306: connect: connection refused
2023/01/14 19:52:53 Error when connecting to database: dial tcp 192.168.128.3:3306: connect: connection refused
It starts to work when I add line user: root to docker-compose, but I've read that's something I should avoid.
I tried running docker exec -it usermysql /bin/bash to try to connect to database from inside the container but it doesn't work as well (with user: root works fine).
I have no name!#usermysql:/$ mysql -u Slimo300 -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/bitnami/mysql/tmp/mysql.sock' (2)
Why it behaves like that?
How can I run mysql container as a nonroot?
Services definitions:
usermysql:
image: bitnami/mysql
container_name: usermysql
hostname: usermysql
user: root #<------- root
networks:
- chatnet
volumes:
- usermysql_data:/bitnami/mysql/data
environment:
- MYSQL_DATABASE=dbusers
- MYSQL_ROOT_PASSWORD=password123
- MYSQL_USER=Slimo300
- MYSQL_PASSWORD=password123
userservice:
build:
context: .
dockerfile: ./backend/user-service/Dockerfile
container_name: userservice
hostname: userservice
networks:
- chatnet
ports:
- "8083:8080"
depends_on:
- usermysql
restart: on-failure
This is relevant for Drupal 8.9.3, MySQL 5.7 and Docker (with docker-compose.yml) file.
Specifically on the Drupal Install step "Set up database", Drupal throws out error [Failed to connect to your database server. The server reports the following message: SQLSTATE[HY000] [2002] Connection refused] when I try to connect using Host: 127.0.0.1.
Database name: Hub2
Database username: drupal
Database password: drupal
Host: 127.0.0.1
Port number: 9403 (This is because of resever proxy used / so on purpose not 3306)
If I used hostmachine IP address (e.g. 192.168.2.122) then it works fine, with same settings for Database name, database username, database password and also port number.
I am using DBeaver to work and look at the DBs and DBeaver can connect to mysql container using 127.0.0.1 using same user as above (in the Drupal install).
My docker-compose.yml file below:
version: '3.8'
services:
hub_db:
image: mysql:5.7
container_name: hub_db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: hub2
MYSQL_USER: drupal
MYSQL_PASSWORD: drupal
MYSQL_ROOT_HOST: 127.0.0.1
volumes:
- ./mysql/data:/var/lib/mysql
- ./mysql/mysql_logs:/var/log/mysql
- ./mysql/configuration/my.cnf:/etc/my.cnf
# Traefik port settings
ports:
- "9403:3306"
networks:
dev_local_mysql:
# Traefik label settings
labels:
- traefik.enable=true
- traefik.docker.network=dev_local_mysql
- traefik.tcp.routers.hub_db.entrypoints=mysql
- traefik.tcp.routers.hub_db.tls=true
- traefik.tcp.services.hub_db.loadbalancer.server.port=9403
networks:
dev_local_mysql:
name: dev_local
My my.cnf file below:
[mysqld]
port=3306
max_connections=250
#bind-address=127.0.0.1
max_allowed_packet=1073741824
[client]
port=3306
Drupal_install, image link
DBeaver_successfulconnection, image link
I’m on Mac OS Big Sur and running the following Docker versions
$ docker -v
Docker version 20.10.12, build e91ed57
$ docker-compose -v
Docker Compose version v2.2.3
I’m running a Rails 6 app, whose database is configured like so
development:
adapter: mysql2
encoding: utf8
host: host.docker.internal
database: cfs
pool: 5
username: myuser
password: bypass
I have to use “host.docker.internal” so that the Rails app can access the Docker db when running inside of Docker, set up in my docker-compose.yml like so
services:
db:
image: mysql:5.7
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
restart: always
volumes:
- ./docker/provision/mysql/init:/docker-entrypoint-initdb.d
environment:
MYSQL_ROOT_PASSWORD: bypass
ports:
# <Port exposed> : < MySQL Port running inside container>
- '3306:3306'
expose:
# Opens port 3306 on the container
- '3306'
…
web:
#restart: always
build: ./my-project
ports:
- "3000:3000"
expose:
- '3000'
command: foreman start
volumes:
- ./my-project/:/app
depends_on:
- db
However if I run the app locally without Docker, I have to change my config files to remove “host.docker.internal” and use “127.0.0.1” instead (or localhost).
Is there a way I can set things up so that I have a single database config file that works in both Docker and without Docker such that I don’t have to change the host around?
You can use ERB markup in the database.yml file, which lets you use an environment variable here. In general I'd suggest making default values for things be whatever default will work in a plain non-Docker development environment.
# config/database.yml
development:
host: <%= ENV['DATABASE_HOST'] || 'localhost' %>
If you're running this in a setup where the database hostname isn't localhost – it's in a sibling container, or you're using a cloud-hosted database like Amazon RDS – you can set that environment variable.
# docker-compose.yml
version: '3.8'
services:
db:
image: 'mysql:5.7'
et: cetera
web:
build: .
ports:
- '3000:3000'
depends_on:
- db
environment:
DATABASE_HOST: db # <-- will be read in database.yml
Well, I am making a discord bot and I want to implement a mysql database on it.
I've researched how to connect an online database on my bot that use Node.js, but I've failed.
I want to know whether I have to install MySQL on my computer or there is a way to connect it online. I'm very confuse.
One more thing: If I have to install it on my PC, is there a way to upload the bot and the database together?
Help me, please.
(I'm Brazilian, so some words or sentences may be wrong)
You can use the NodeJS MySQL client for connecting to your database:
const { createConnection } = require('mysql');
const database = createConnection({
host: 'localhost',
user: 'user',
password: 'password',
database: 'db',
});
database.connect();
As for installing and setting up MySQL, between a local install and a containerized install, I'd recommend containerizing it with Docker.
This can get you started via docker-compose: docker-compose up.
# docker-compose.yml
version: '3'
services:
db:
image: mysql:8.0
restart: always
environment:
MYSQL_DATABASE: 'db'
MYSQL_USER: 'user'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
ports:
- '3306:3306'
expose:
- '3306'
volumes:
- database:/var/lib/mysql
volumes:
database:
I am running into this error with my Rails + MySQL Docker setup:
Mysql2::Error::ConnectionError: Unknown MySQL server host 'db' (-2)
/usr/local/bundle/gems/mysql2-0.5.2/lib/mysql2/client.rb:90:in `connect'
/usr/local/bundle/gems/mysql2-0.5.2/lib/mysql2/client.rb:90:in `initialize'
my docker-compose.yml:
version: '3.7'
services:
db:
# https://github.com/passbolt/passbolt_docker/issues/103
image: mysql:5.7
restart: always
healthcheck:
test: ["CMD-SHELL", 'mysql --database=$$MYSQL_DATABASE --password=$$MYSQL_ROOT_PASSWORD --execute="SELECT count(table_name) > 0 FROM information_schema.tables;" --skip-column-names -B']
interval: 30s
timeout: 10s
retries: 4
environment:
MYSQL_DATABASE: 'db'
MYSQL_USER: 'user'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
ports:
- "3305:3306"
expose:
- '3305'
volumes:
- my-db:/var/lib/mysql
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- '.:/app'
ports:
- '3000:3000'
environment:
DB_PORT: 3306
DB_HOST: db
DATABASE_URL: mysql2://user:password#db:3306
depends_on:
- db
volumes:
my-db:
and my database.yml:
default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: user
password: password
host: localhost
url: <%= ENV['DATABASE_URL'] %>
port: 3305
According to this, I'm supposed to wait for MySQL to start? However I added a health check in the healthcheck section of the docker-compose file and it didn't work.
I also tried to use netcat to check the port but it also didn't work. How come I can't connect to the host db from my Docker web container? What am I doing wrong?
Turns out for my problem, I needed to clear out my docker volumes and recreate everything. MySQL was experiencing an error while booting up.
Basically ran this:
docker-compose down
docker system prune --force --volumes
And then restarted everything, ensuring that MySQL ("db") was running successfully before trying to connect to db.
This message is complaining about a missing "MYSQL host" called 'db'
Mysql2::Error::ConnectionError: Unknown MySQL server host 'db' (-2)
According to your docker-compose file
environment:
MYSQL_DATABASE: 'db'
MYSQL_USER: 'user'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
You should choose here a valid parameters (use ENV variables even better).
Also make sure
you can access MYSQL from console with the specified credintials.
The specified MYSQL user has access from external host ( docker is will not be on the same network )