Before, I created named volume and database: 'mysql-db1' using other mysql container.
I can't connect to database from python.
My .yml file :
version: '3.6'
services:
python:
image: python:latest
ports:
- '80:80'
volumes:
- type: bind
source: .
target: /scripts
command: tail -f /dev/null
links:
- 'mysql'
mysql:
image: mysql/mysql-server:latest
ports:
- '3306:3306'
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- type: volume
source: mysql-db1
target: /var/lib/mysql
volumes:
mysql-db1:
external: true
My simply python code:
import mysql.connector
cnx = mysql.connector.connect(user='root', password='root', host='mysql', database='test1')
cnx.close()
I can enter the database using:
$ docker-compose exec mysql bash
# mysql -uroot -proot -Dtest1
Error:
mysql.connector.errors.DatabaseError: 1130 (HY000): Host '172.18.0.3' is not allowed to connect to this MySQL server
Where is a problem?
root user is not allowed to access the db externally by default. Use image environment variables to create a user and use that:
db:
restart: always
image: mariadb
container_name: myapp_db
environment:
- MYSQL_USER=myuser
- MYSQL_PASSWORD=mypass
- MYSQL_DATABASE=mydb
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
ports:
- 3306:3306
Related
I have this docker-compose file:
version: "3"
services:
mariadb:
image: mariadb:latest
container_name: mariadb
command: --default-authentication-plugin=mysql_native_password
restart: unless-stopped
env_file: .env
volumes:
- db-data:/var/lib/mysql
networks:
- internal
drupal:
image: drupal:9.3.9-fpm-alpine
container_name: drupal
depends_on:
- mariadb
restart: unless-stopped
networks:
- internal
- external
volumes:
- /var/www/html/:/var/www/html/
webserver:
image: nginx:latest
container_name: webserver
depends_on:
- drupal
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- /var/www/html/:/var/www/html/
- ./nginx-conf:/etc/nginx/conf.d
networks:
- external
networks:
external:
driver: bridge
internal:
driver: bridge
volumes:
db-data:
I run docker-compose up -d and everything works fine, but I can not access to the mariadb server from host, I tried these
# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
# mysql -uroot -p -h mariadb
Enter password:
ERROR 2005 (HY000): Unknown MySQL server host 'mariadb' (-2)
# mysql -uroot -p -h 127.0.0.1
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (111)
Edit:
.env file:
MYSQL_ROOT_PASSWORD=abc1234
MYSQL_DATABASE=my_db
MYSQL_USER=drupal
MYSQL_PASSWORD=cdf1234
From MariaDB Docker Library documentation;
One of MARIADB_ROOT_PASSWORD, MARIADB_ALLOW_EMPTY_ROOT_PASSWORD, or
MARIADB_RANDOM_ROOT_PASSWORD (or equivalents, including *_FILE), is
required. The other environment variables are optional.
This is for the initialization of the db-data volume. Once initialized, these environment variables are optional.
I am running WordPress with wp-env and docker configuration,
I want to connect to mysql via cli but I keep getting this error:
zsh: command not found: mysql
I have tried: mysql -h 127.0.0.1:3308 -u root -p and in post 3306 as well
this is my docker-compose.yaml file
phpmyadmin works great
version: '3.1'
services:
mysql:
image: mysql:5.7
ports:
- 3308:3306
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress_test
phpmyadmin:
depends_on:
- mysql
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
restart: always
ports:
- '8080:80'
environment:
PMA_HOST: mysql
volumes:
testsuite:
`
What I did was to execute the following command:
docker exec -it {container name} bash -l
I need to execute these commands on every startup since it looks to be overwritten every time I set and restart it
mysql -uroot -padmin;
set global general_log = 1;
I start the docker container with docker-compose for development purposes only and it looks like this.
version: "3.8"
services:
mysql_service:
container_name: db_container
build:
context: .
dockerfile: ./db/Dockerfile.dev
# needed for mysql 8+
command: --default-authentication-plugin=mysql_native_password
restart: always
hostname: db
ports:
- target: 3306
published: 3306
protocol: tcp
mode: host
environment:
- MYSQL_ROOT_PASSWORD=admin
- MYSQL_DATABASE=example
- MYSQL_ROOT_HOST=localhost
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
volumes:
- ./db/:/docker-entrypoint-initdb.d/
- data_volume:/var/lib/mysql/
cap_add:
- ALL
volumes:
data_volume:
driver: local
and the Dockerfile
FROM mysql:8.0
COPY ./DevOps/Docker/db/set_logging.sh /usr/local/bin
ENTRYPOINT ["docker-entrypoint.sh", "./usr/local/bin/set_logging.sh"]
However the copy goes through but the script is never executed.
Where the script looks like
#!/bin/bash
mysql -uroot -padmin -e "set global general_log = 1"
Any suggestions on getting this command to go through? This is only for development
In order to tell MYSQL container to run that script once it starts you can either mount the script into the image's /docker-entrypoint-initdb.d folder using docker file, or docker-compose file using bind mount volume.
Dockerfile
FROM mysql:8.0
COPY ./DevOps/Docker/db/script.sh /docker-entrypoint-initdb.d/script.sh
ENTRYPOINT ["docker-entrypoint.sh"]
docker-compose
version: "3.8"
services:
mysql_service:
container_name: db_container
build:
context: .
dockerfile: ./db/Dockerfile.dev
# needed for mysql 8+
command: --default-authentication-plugin=mysql_native_password
restart: always
hostname: db
ports:
- target: 3306
published: 3306
protocol: tcp
mode: host
environment:
- MYSQL_ROOT_PASSWORD=admin
- MYSQL_DATABASE=example
- MYSQL_ROOT_HOST=localhost
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
volumes:
- "./DevOps/Docker/db/script.sh:/docker-entrypoint-initdb.d/script.sh"
- data_volume:/var/lib/mysql/
cap_add:
- ALL
volumes:
data_volume:
driver: local
You can check how scripts are launched in /docker-entrypoint-initdb.d read the Initializing a fresh instance Section
I'm trying to connect to my mysql database using official adminer and mysql images from docker hub.
Here is my docker-compose.yml file configuration:
version: '3'
services:
mysql:
image: mysql
restart: always
volumes:
- mysql:/var/lib/mysql
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD= 1
- MYSQL_DATABASE= db
ports:
- 3306:3306
- 33060:33060
adminer:
image: adminer
restart: always
ports:
- 8080:8080
depends_on:
- mysql
volumes:
mysql:
Whenever I want to login to the MySQL using Adminer I face with the following problem:
SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
SQLSTATE[HY000] [2002] No such file or directory
Here is the inputs I've used trying to connect to MySQL from Adminer interface:
#first try
System: MySQL
Server: localhost
Username: root
Database: db
#second try
System: MySQL
Server: mysql #container-name
Username: root
Database: db
You have to add the default authentication plugin in compose file
command: --default-authentication-plugin=mysql_native_password
Here is the complete docker-compose.yml
services:
mysql:
image: mysql
restart: always
command: --default-authentication-plugin=mysql_native_password
volumes:
- mysql:/var/lib/mysql
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD= 1
- MYSQL_DATABASE= db
ports:
- 3306:3306
- 33060:33060
adminer:
image: adminer
restart: always
ports:
- 8080:8080
depends_on:
- mysql
volumes:
mysql:
In adminer
System: MySQL <DB System to connect>
Server: mysql <should match the starting tag before image tag > (in your case mysql but you can change it to any name )
Username: root <username>
Password: <password>
Database: db <database name>
Trying to get a rails app running using docker-compose. I run docker-compose build and it completes with no errors. I then run docker-compose up and both of the containers start. Then I run docker-compose run web rake db:create db:migrate and run into an error:
rake aborted!
Mysql2::Error::ConnectionError: Can't connect to MySQL server on '127.0.0.1' (111 "Connection refused")
My Dockerfile:
FROM ruby:2.5.3
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs mysql-client sqlite3 zlib1g-dev libxslt-dev git && \
gem install bundler
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
Docker-compose
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'online_community_development'
MYSQL_ROOT_PASSWORD: '******'
MYSQL_HOST: 'localhost'
ports:
# <Port exposed> : < MySQL Port running inside container>
- '3306:3306'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- 'my-db:/var/lib/mysql'
container_name: datab
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- web-app:/myapp
ports:
- "3000:3000"
depends_on:
- db
links:
- db:db
restart: always
volumes:
my-db: {}
web-app: {}
Database.yml
development:
adapter: mysql2
encoding: utf8
database: online_community_development
pool: 5
username: root
password: slumland
socket: /var/run/mysqld/mysqld.sock
host: db
New to docker so not even sure if I am issuing the commands correctly. I need to create and seed the database and have the app container connect to it. I think the issue is related to it trying to connect via localhost, but even when I changed the host to db I still got the same results. Any help would be greatly appreciated.
tl;dr:
Try this docker-compose.yml:
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'online_community_development'
MYSQL_ROOT_PASSWORD: '******'
MYSQL_HOST: 'localhost'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- 'my-db:/var/lib/mysql'
container_name: datab
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
environment:
DATABASE_URL: 'mysql2://db'
RAILS_ENV: 'development'
volumes:
- web-app:/myapp
ports:
- "3000:3000"
depends_on:
- db
restart: always
volumes:
my-db: {}
web-app: {}
In detail:
First of all your docker-compose-file is kinda misconfigured.
Therefore I will do a small instruction on docker-compose.yml-files before heading over to your question.
The links:-thing is a legacy feature of docker which you should strictly avoid:
https://docs.docker.com/compose/compose-file/#links
Moreover, it is not even required when you're working with docker-compose
Also, you're using expose: and ports: when defining your database-service.
The ports-key is opening up a port on your host-machine and forwards it to your docker-service:
https://docs.docker.com/compose/compose-file/#ports
Using ports-key on database-definitions makes your database accessible to the www - which is not what you want in most cases.
The expose-key opens the port locally, in the created docker network.
https://docs.docker.com/compose/compose-file/#expose
This is what we want for databases.
Editing your docker-compose.yml-file results in the following:
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'online_community_development'
MYSQL_ROOT_PASSWORD: '******'
MYSQL_HOST: 'localhost'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- 'my-db:/var/lib/mysql'
container_name: datab
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- web-app:/myapp
ports:
- "3000:3000"
depends_on:
- db
restart: always
volumes:
my-db: {}
web-app: {}
Getting back to your primary problem:
rake aborted! Mysql2::Error::ConnectionError: Can't connect to MySQL server on '127.0.0.1' (111 "Connection refused")
It looks like rake expects the database to be available at localhost - therefore I think that the cause of this issue is a misconfiguration.
Unfortunately I'm not very familiar with rake but nevertheless your database.yaml looks correct.
I did some research and came across this post:
Rake tasks seem to ignore database.yml configuration
Maybe you could try to set the environment-variable DATABASE_URL in your docker-compose-file: DATABASE_URL=mysql://db
I'd also recommend to set the correct context-environment-variable: RAILS_ENV=development
When you do alle these changes you should come up with the following docker-compose.yml:
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'online_community_development'
MYSQL_ROOT_PASSWORD: '******'
MYSQL_HOST: 'localhost'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- 'my-db:/var/lib/mysql'
container_name: datab
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
environment:
DATABASE_URL: 'mysql2://db'
RAILS_ENV: 'development'
volumes:
- web-app:/myapp
ports:
- "3000:3000"
depends_on:
- db
restart: always
volumes:
my-db: {}
web-app: {}
Hope this helps.
Regards,
Hermsi
your db seems to be configured to listen on localhost only. Unfortunately this will not work since your app is technically another network host (container) within the compose network. Try making your db listen on all network interfaces.