I have this docker-compose file:
version: "3"
services:
db:
image: mysql/mysql-server:5.7
container_name: mysqldb
restart: always
environment:
MYSQL_ROOT_PASSWORD: 4dfLtRah2C
MYSQL_DATABASE: db
MYSQL_PASSWORD: le562BplPk
MYSQL_USER: db_user
networks:
- backend
volumes:
- ./db_data:/var/lib/mysql
phpmyadmin:
container_name: phpmyadmin
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '9006:80'
environment:
MYSQL_ROOT_PASSWORD: 4dfLtRah2C
PMA_PORT: 3306
PMA_HOST: db
networks:
- backend
networks:
backend:
When I try to use the db_user with the MYSQL_PASSWORD it works, but when I try to login as the root user and use the MYSQL_ROOT_PASSWORD it does not work, gives me a connection refused error.
I can login in to the db via the container using mysql -u root --password=$MYSQL_ROOT_PASSWORD and it works just fine...
Already tried to do docker system prune -f -a --volumes as well as removing the db_data folder multiple times to ensure that the container runs as a fresh instance.
How may I fix this? What is wrong?
The problem here was that phpmyadmin was trying to use root#localhost, but we want to use root#db, to fix this we simply need to perform a few modifications on the database, in order to change the host of the user root to %, like this:
(inside the db container: mysql -u root -p)
use mysql;
UPDATE mysql.user SET host='%' WHERE host='localhost' AND user='root';
FLUSH privileges;
exit;
Now I can use phpmyadmin to manage all the databases using root.
Please note that you should not abuse the root user use, instead try to use the dedicated user for your specific database, but as this is a development environment and everything is containerized no harm should come from using root.
Related
Using docker compose i have a mysql 5.7 database container, i set the root password and a user password but they don't work, the docker-compose:
version: '3.8'
services:
viprs-proxy:
platform: linux/amd64
image: nginx:alpine
container_name: viprs-proxy
depends_on:
- viprs-website
volumes:
- ./nginx/proxy.conf:/etc/nginx/nginx.conf
ports:
- 80:80
networks:
- viprs-net
viprs-website:
platform: linux/amd64
image: nginx
container_name: viprs-website
depends_on:
- php
- viprs-website-database
volumes:
- ./website/nginx/site.conf:/etc/nginx/conf.d/default.conf
- ./website:/usr/share/nginx/html
- ./website/logs:/var/log/nginx
- viprs-uploads:/usr/share/nginx/html/wp-content/uploads
ports:
- 80
links:
- php
networks:
- viprs-net
php:
platform: linux/amd64
#image: php:7-fpm
image: viprs-php
container_name: php
volumes:
- ./website:/usr/share/nginx/html
ports:
- 9000
networks:
- viprs-net
viprs-website-database:
platform: linux/amd64
image: mysql:5.7
container_name: viprs-db
command: --init-file /usr/share/nginx/website.sql
volumes:
- ./website.sql:/usr/share/nginx/website.sql
- viprs-db:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: testpassword
MYSLQ_DATABASE: viprs
MYSQL_USER: viprs
MYSQL_PASSWORD: testpassword
networks:
- viprs-net
networks:
viprs-net:
volumes:
viprs-uploads:
viprs-db:
Now if i log into bash:
docker exec -it viprs-db bash
and try to log into mysql:
mysql -u root -p
It just says access is denied, it doesn't matter if i am using the root user or the viprs user.
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
If i output $MYSQL_ROOT_PASSWORD it gives:
echo $MYSQL_ROOT_PASSWORD
testpassword
I have seen loads of people with this issue but can't find any solution that works. The environment is set as per the image documentation on docker hub so i'm confused.
In fact, i can log in as root without a password, so something isn't right.
The environment variables are only used if there is no database present when then container starts and MySQL has to create one.
If there already is a database, the users defined in that database are used and no new users are created. Since you have a volume mapping on /var/lib/mysql chances are that you already have a database.
To verify if that's the issue, you can try removing the /var/lib/mysql mapping. That will cause the container to create a new database when it starts using the environment variable values.
I have a laravel app and the database containers are configured in this way:
db:
image: "mysql:5.7"
command: ["--sql- mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"]
environment:
MYSQL_DATABASE: mydb
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- db_data:/var/lib/mysql
restart: unless-stopped
networks:
- simple_net
app:
build:
context: .
dockerfile: ./setup/Dockerfile
restart: unless-stopped
tty: true
ports:
- "${APP_PORT}:80"
working_dir: /var/www/
volumes:
- ./:/var/www/
environment:
DB_CONNECTION: mysql
DB_HOST: db
DB_DATABASE: mydb
DB_USERNAME: root
# SERVER_ADDR: localhost
When I tried to start the app with docker-compose up it throws an exception that says SQLSTATE[HY000] [1045] Access denied for user 'root'#'172.23.0.6' (using password: NO). The IP refers to the environmental variable SERVER_ADDR, I tried to set it up with the values localhost and '' (empty) but nothing has worked so far.
You should set the .env's DB_HOST to the name of your container, in your case db.
DB_HOST=db
I do not believe injecting environment variables replaces filling the .env.
Also, if you want to Dockerize your Laravel applications, several options already exist and work out of the box:
Laradock
Docker Compose Laravel
Laradose (which I maintain)
You don't need DB_CONNECTION: mysql there.
Instead inside your .env file in lavel you will use this like:
DB_CONNECTION: mysql
DB_HOST: db
DB_PORT: 3306
DB_DATABASE: mydb
DB_USERNAME: root
DB_PASSWORD: password
Your app container does not need this environment, that's your server running. You try to configure the environment of the db container inside server container which will run your app.
Change your docker-compose to:
db:
image: "mysql:5.7"
command: ["--sql- mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"]
environment:
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: password
MYSQL_DATBASE: mydb
MYSQL_USER: root
MYSQL_PASSWORD: password
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- db_data:/var/lib/mysql
restart: unless-stopped
networks:
- simple_net
app:
build:
context: .
dockerfile: ./setup/Dockerfile
restart: unless-stopped
tty: true
ports:
- "${APP_PORT}:80"
working_dir: /var/www/
volumes:
- ./:/var/www/
networks:
- simple_net
Bridge your containers under your network so they can interact with each other.
Run docker-compose up --build and then access your laravel container and run inside the container php artisan optimize to configure cache for routes,env variables, files etc etc.
docker exec -it db mysql -u root -p password
Based on your comments you have issues to access the container and the reason is you don't use credentials for it and mysql tries to use the default ones. Above command defines the right user and password, you will also be asked to fill the password.
Also mysql has more parameters to define the host etc etc you can have a look further for that.
I am trying to set up a simple container with MySQL docker image. I am able to run the container and set it up using command prompt as below
docker run --name test-mysql -e MYSQL_ROOT_PASSWORD="myrootpassoword" -d mysql
But when I try to do it using docker-compose, the container gets up but I am not able to connect to mysql db using mysql -p. My password is not working, it keeps getting access denied and I am not able to work with the container. Below is my compose file. Am I missing anything?
# Docker Compose file Reference (https://docs.docker.com/compose/compose-file/)
version: '3'
# Define services
services:
# Mysql Service
db:
image: "mysql" # Use a public mysql image to build the mysql service
volumes:
- dbvol:/var/lib/mysql
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: "myrootpassowrd"
MYSQL_DATABASE: "mydb"
MYSQL_USER: "myadmin"
MYSQL_PASSWORD: "mypassword"
ports:
- "3306:3306" # Forward the exposed port 3306 on the container to port 3306 on the host machine
container_name: test-mysql
volumes:
dbvol:
Finally I found the issue causing this trouble. My password contained $ symbol which is used to get variable in docker, so the next character/part was considered as variable and as value was not set, it was taken as blank without actually throwing any error.
To escape it I had to use two dollar signs $$
So I modified the code from
MYSQL_ROOT_PASSWORD: "myroot$passowrd"
to
MYSQL_ROOT_PASSWORD: "myroot$$passowrd"
and it worked properly.
I'm honestly tired of this. I've tried every possible solution but it still refuses to connect.
Here is my docker-compose file:
version: '3'
services:
# Database
db:
image: mysql:5.7
container_name: db
restart: always
env_file: .env
environment:
- MYSQL_ROOT_HOST=%
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=$MYSQL_DATABASE
- MYSQL_USER=$MYSQL_USER
- MYSQL_PASSWORD=$MYSQL_PASSWORD
networks:
- backend
# Wordpress
wp:
depends_on:
- db
image: wordpress:php7.3-fpm
container_name: wordpress
restart: always
env_file: .env
environment:
- WORDPRESS_DB_HOST=db:3306
- WORDPRESS_DB_USER=$MYSQL_USER
- WORDPRESS_DB_PASSWORD=$MYSQL_PASSWORD
- WORDPRESS_DB_NAME=$MYSQL_DATABASE
volumes:
- ./wordpress:/var/www/html
networks:
- backend
# Nginx
nginx:
depends_on:
- wp
image: nginx
restart: always
ports:
- "80:80"
volumes:
- ./wordpress:/var/www/html
- ./nginx-conf:/etc/nginx/conf.d
networks:
- backend
networks:
backend:
driver: bridge
I've tried 127.0.0.1, 0.0.0.0, db, docker inspect db to get ip address of the container. All of them fail to connect. I've used Sequel Pro, MySQL Workbench and DataGrip.
The setup works completely fine. Its just that I cant connect to the database outside the container.
I even checked the mysql host privileges in the container and got:
% root
% wordpress (name of the user I created)
...
Am I missing something?
In order to reach your database from the host machine with an sql client you need to map the MYSQL database port to the host machine.
In your Compose file add the port mapping to your db service.
db:
...
ports:
- "3306:3306"
I believe you are using the default port based on your Wordpress service.
Then configure your SQL client to 127.0.0.1 and port 3306.
MYSQL Changes
MY.ini changes in c:\programdata\mysql\mysql server 8.0
Add Bind_address=0.0.0.0
Save as Admin - Check timestam
Create one App user - Dont user mysql Root user for app
drop user 'username'#localhost;
FLUSH PRIVILEGES;
CREATE USER 'username'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'#'localhost' WITH GRANT OPTION;
CREATE USER 'username'#'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'#'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Restart the MYSQL Server
Restart Docker for Windows
Check Hosts files in C:\Windows\System32\drivers\etc if host.docker.internal variable is updated correctly
# Added by Docker Desktop
10.0.0.6 host.docker.internal
10.0.0.6 gateway.docker.internal
Application Changes:
User host.docker.internal in JDBC URL in app.properties
spring.datasource.url=jdbc:mysql://host.docker.internal:3306/test
spring.datasource.username=username
spring.datasource.password=password
maven install should work
docker run <repo/repo>:image should work fine. now
I have a simple docker setup using php7 and mysql, both from docker hub. Setting up is ok so far, everything starts up and I can access my app. But I can't get the mysql connect to work for me. I even logged in into the box and tried accessing the db, but it fails. Anybody any idea?
docker-compose.yml
db:
restart: always
image: mysql/mysql-server:5.7
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: myapp
MYSQL_PASSWORD: myapp
MYSQL_DATABASE: myapp
web:
restart: always
image: silintl/php7
command: php -S 0.0.0.0:8000 -t /app/web
links:
- db
ports:
- "8000:8000"
volumes:
- ../:/app
trying to access the db
[bash]marcobehnke#Marcos-MacBook-Pro-2 ~ $ docker exec -it docker_db_1 bash
[root#7eff3007bb11 /]# mysql --user=myapp --password=myapp --host=localhost --port=3306 myapp
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'myapp'#'localhost' (using password: YES)
[root#7eff3007bb11 /]#
What am I missing here?
Edit: Have a look at my github repo, this is the exact config I am using: https://github.com/firegate666/docker-php7-mysql-postgres
Seems like you're using the wrong syntax. Should look something like this:
version: '2'
services:
db:
restart: always
image: mysql/mysql-server:5.7
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_USER: "myapp"
MYSQL_PASSWORD: "myapp"
MYSQL_DATABASE: "myapp"
# ...
Ok, I took a different machine and tried my setup, it worked out of the box. I have no idea whyt this is about, but the problem is obviously my machine :-)