i followed docker compose tutorial
https://docs.docker.com/get-started/08_using_compose/
to create composer file for todo app with mysql service
i did the same docker compose as the tutorial
but it gave me this error
app-mysql-1 | 2022-05-28T21:55:26.950553Z 3 [Note] Unknown database 'todos'
app-app-1 | Error: ER_BAD_DB_ERROR: Unknown database 'todos'
i executed mysql container services to see if todos database created or not
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
docker compose
version: "3.7"
services:
app:
image: node:12-alpine
command: sh -c "yarn install && yarn run dev"
ports:
- 3000:3000
working_dir: /app
volumes:
- ./:/app
environment:
MYSQL_HOST: mysql
MYSQL_USER: root
MYSQL_PASSWORD: secret
MYSQL_DB: todos
mysql:
image: mysql:5.7
volumes:
- todo-mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: todos
volumes:
todo-mysql-data:
Running just the mysql portion of your docker-compose file seems to have worked for me.
version: "3.7"
services:
mysql:
image: mysql:5.7
volumes:
- todo-mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: todos
volumes:
todo-mysql-data
You might want to try deleting the volume and recreating your containers.
docker volume rm todo-mysql-data
docker-compose up --force-recreate
Thanks Hambrook i found that there was a volume with the same name i tried to create
docker volume ls
DRIVER VOLUME NAME
local app_todo-mysql-data
local todo-mysql-data
so i removed it and write command again
docker compose up
and it's work
Related
I'm very new at docker and docker-composer. I'm trying to make a mysql container which will be used by a php-apache container.
I have a SQL script which create a database and insert values.
I want my mysql container to execute this SQL script at his launching, so the app will be able to make queries on it. here's my docker-compose file :
version: "3.7"
services:
mysql:
image: mysql:latest
container_name: mysql
restart: always
volumes:
- db-volume:/var/lib/mysql
- ./bdd.sql:/docker-entrypoint-initdb.d/bdd.sql
environment:
MYSQL_ROOT_PASSWORD: test
MYSQL_DATABASE: webreathe
app:
image: toto:latest
container_name: toto
restart: always
volumes:
- ./:/var/www/webreathe
ports:
- 8080:80
depends_on:
- mysql
volumes:
db-volume:
I launch it with docker-compose up -d
and I want to see if my database was created so I execute inside of the mysql container :
mysql -u root -p test -e "show databases;"
but the result is :
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
So I wonder if the bdd.sql file was in the /docker-entrypoint-initdb.d directory
and yes it is : ls docker-entrypoint-initdb.d => bdd.sql
Do you know why it doesn't execute the script ?
This question already has an answer here:
Node.js connect to MySQL Docker container ECONNREFUSED
(1 answer)
Closed last year.
I'm using Adonis 5 and Mysql database , after following some tutorials i added this code to connect mysql. but it shows me "connect ECONNREFUSED 127.0.0.1:3306" error. Here is my code below:
My Dockerfile:
FROM node:14
RUN mkdir -p /home/node/app/node_modules
WORKDIR /home/node/app
COPY package*.json ./
# RUN apk add --no-cache git
COPY . /home/node/app/
RUN chown -R node:node /home/node
RUN npm install
USER node
EXPOSE 3333
ENTRYPOINT ["node","ace","serve","--watch"]
my docker-compose.yml file:
# For more information: https://github.com/julien-r44/adonis-sail
version: '3'
services:
mysql:
image: 'mysql/mysql-server:8.0'
container_name: mysql
expose:
- "3307"
ports:
- '${MYSQL_PORT:-3307}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${MYSQL_PASSWORD}'
MYSQL_ROOT_HOST: "%"
MYSQL_DATABASE: '${MYSQL_DB_NAME:?err}'
MYSQL_USER: '${MYSQL_USER:?err}'
MYSQL_PASSWORD: '${MYSQL_PASSWORD?:err}'
MYSQL_ALLOW_EMPTY_PASSWORD: 1
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
phpmyadmin:
image: 'phpmyadmin/phpmyadmin'
links:
- 'mysql:mysql'
ports:
- 8080:80
environment:
MYSQL_USERNAME: "${MYSQL_USER}"
MYSQL_ROOT_PASSWORD: "${MYSQL_PASSWORD}"
PMA_HOST: mysql
networks:
- sail
app:
links:
- mysql
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/home/node/app
- /home/node/app/node_modules
ports:
- 3333:3333
depends_on:
- mysql
- phpmyadmin
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: "local"
My .env file:
HOST=0.0.0.0
PORT=3333
APP_URL=http://${HOST}:${PORT}
NODE_ENV=development
APP_KEY=wH94pOFV47NO0dJtE_S6-TDRgcjgZXGM
SESSION_DRIVER=cookie
CACHE_VIEWS=false
DB_CONNECTION=mysql
MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_USER=adonis
MYSQL_PASSWORD=adonis
MYSQL_DB_NAME=social
Error:
it shows this error
What is my mistake?
Change from MYSQL_HOST=localhost to MYSQL_HOST=mysql
When your app is running inside a docker container, localhost points no longer to your laptop (or server) but to the container itself.
As each service is running in separated containers when one application is trying to access the database service you cannot use localhost. As localhost points to that container, and the database is not installed there.
You should use the container name instead localhost when specificying connection urls.
You can also check Docker Compose network documentation for detailed explanations.
I created a docker-compose.yml.
version: "3.7"
services:
db:
container_name: mysql
image: mysql
ports:
- 3306:3306
volumes:
- ./sql:/docker-entrypoint-initdb.d
- ./mysql:/var/lib/mysql
environment:
MYSQL_ROOT_USER : root
MYSQL_ROOT_PASSWORD: root_pass
MYSQL_DATABASE: wp1
MYSQL_USER: wp
MYSQL_PASSWORD: pass
restart: always
And in sql directory, I put a init.sql.
CREATE DATABASE IF NOT EXISTS wp2;
Then run docker-compose up.
In the log there is a entry for the init script.
mysql | 2020-04-08 02:17:32+00:00 [Note] [Entrypoint]: /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
But the database wp2 is not created.
$ docker exec -it mysql bash
# mysql -u wp -ppass
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| wp1 |
+--------------------+
How can I create a database with mysql container init script?
Your wp user doesn't have access rights on wp2.
Append to your sql file:
GRANT ALL on wp2.* to wp#'%';
I have the following docker-compose.yml
version: "3.7"
services:
db:
container_name: db
image: mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: "mysql"
MYSQL_DATABASE: "mydb"
security_opt:
- seccomp:unconfined
volumes:
- ./supplied/init:/docker-entrypoint-initdb.d/:ro
When I run 'docker-compose up' It see this :
Recreating db ... done
Attaching to db
If I check the available databases I see:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| db |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
Why is it ignoring my MYSQL_DATABASE environment variable?
I'm running on windows 10.
EDIT :
This looks like some sort of caching issue. I changed the service name and container_name in the docker-compose file and it started working...
from https://github.com/docker-library/mysql/issues/180:
Important to note that the image entrypoint script will never make
changes to an existing database. If you mount an existing data
directory into var/lib/mysql, options like MYSQL_ROOT_PASSWORD will
have no effect
My issue was that the volume already existed. When I removed the volume with docker-compose down -v
When I brought it up, it would created the database.
You should use the --force-recreate flag:
--force-recreate Recreate containers even if their configuration
and image haven't changed.
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