I'm sorry to open one more of those 'can't connect to my database' questions, but after searching for hours I just don't get it.
I want a dockerized database to be used by the (non dockerized) invoicePlane app.
So I'm using the following docker-compose.yml
version: '2'
services:
db:
image: mariadb:latest
#also tried mysql:latest
restart: always
environment:
MYSQL_DATABASE: 'invoiceplane_db'
MYSQL_USER: 'invoiceplane'
MYSQL_PASSWORD: 'pass1'
MYSQL_ROOT_PASSWORD: 'pass0'
expose:
- '3306'
volumes:
myserver/path:/var/lib/mysql
volumes:
invoiceplane-db:
It starts with sudo docker-compose -f docker-compose.yml up
From inside the container, everything works fine. I can connect, e.g. via mysql -uroot -ppass0, and see
+--------------+-----------+
| User | Host |
+--------------+-----------+
| invoiceplane | % |
| root | % |
| root | localhost |
+--------------+-----------+
3 rows in set (0.004 sec)
Now I want to access the db from the host, trying
mysql invoiceplane_db_1:3306 -uroot -ppass0
where invoiceplane_db_1 is the container's name.
but I'm always getting:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
Through my websearch I gained the impression that this may have to do with the bind-address in the /etc/mysql/my.cnf of the container, which was stated here, for example. But I was not able to get it right. Initially, the parameter was commented out. I set it to bind-address = 127.0.0.1 and restarted the container. It did not help.
Am I missing something here?
This is my working docker-composer for mariadb:
db:
image: mariadb:10.4
ports:
- "3306:3306"
volumes:
- db-storage:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root-pass
MYSQL_USER: my-user
MYSQL_PASSWORD: my-user-pass
MYSQL_DATABASE: my-database
From the looks of it, seems like you are missing the port mapping: 3306:3306
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 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.
I have mysql running through the docker, i.e. when I run the docker ps command, I get this result:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fXX3aeXXcXXa mysql:5.7.22 "docker-entrypoint.s…" 35 minutes ago Up 18 minutes 0.0.0.0:3360->3306/tcp db
And here is my db service in my docker-compose.yml in my laravel root project:
# ... other services
db:
image: mysql:5.7.22
container_name: db
restart: unless-stopped
tty: true
ports:
- "3360:3306"
environment:
MYSQL_DATABASE: laravel_db
MYSQL_USER: root
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql
- ./DockerDevelopment/mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
# other services ...
So, I try this command: docker exec -it db bash then goes to my dockerized db command line, and I get this error:
root#fXX3aeXXcXXa:/# mysql -u root -p [ENTER]
Enter password: password
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
what should I do to be able to access mysql> in my docker?
Maybe you have to check your my.cnf. Maybe you can provide your config.
With this compose file the connect to mysql works:
version: "3"
services:
db:
image: mysql:5.7.22
container_name: dbvmssettlementiban
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: vms_settlement_iban
MYSQL_USER: root
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
SERVICE_TAGS: dev
SERVICE_NAME: mysql
Let try:
mysql --protocol tcp --port=<your-mysql-port> -u root -p
OR
Add lines below to your my.cnf on client:
[client]
protocol=tcp
This works for my case.
Since 2 hours I've got the problem that my MySQL container doesnt' allow connections from my laravel application (order). Every time I try to connect I get the following message (from laravel logs):
[2019-08-08 15:20:18] production.ERROR: SQLSTATE[HY000] [1045] Access
denied for user 'root'#'172.21.0.3' (using password: YES)
{"exception":"[object] (Doctrine\DBAL\Driver\PDOException(code:
1045): SQLSTATE[HY000] [1045] Access denied for user
'root'#'172.21.0.3' (using password: YES) at
/var/www/laravel/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:31,
PDOException(code: 1045): SQLSTATE[HY000] [1045] Access denied for
user 'root'#'172.21.0.3' (using password: YES) at
/var/www/laravel/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:27)
I've changed nothing in the configs or the DB. I just restarted all container with 'docker-compose restart'.
Here's my docker-compose file:
version: "2"
services:
nginx-proxy:
image: jwilder/nginx-proxy
container_name: nginx-proxy
restart: always
networks:
- webgateway
ports:
- "80:80"
- "443:443"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
- conf:/etc/nginx/conf.d
- vhost:/etc/nginx/vhost.d
- html:/usr/share/nginx/html
- dhparam:/etc/nginx/dhparam
- certs:/etc/nginx/certs
letsencrypt:
image: jrcs/letsencrypt-nginx-proxy-companion
container_name: nginx-proxy_le
volumes_from:
- nginx-proxy
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
networks:
- webgateway
order:
container_name: order
image: e-order:latest
restart: always
volumes:
- /var/e-order/storage:/var/www/laravel/storage/app
- /var/e-order/logs:/var/www/laravel/storage/logs
networks:
- webgateway
- order_net
environment:
- VIRTUAL_HOST={URL}
- LETSENCRYPT_HOST={URL}
- LETSENCRYPT_EMAIL={MAIL}
- DB_HOST=order-db
- DB_USERNAME=root
- DB_PASSWORD={ROOT_PW}
- DB_DATABASE=e-order
- PHP_MEM_LIMIT=2048
env_file:
- /var/e-order/config/production.env
order-db:
image: mysql
container_name: order-db
restart: always
volumes:
- /var/e-order/database:/var/lib/mysql
networks:
- order_net
environment:
- MYSQL_DATABASE=e-order
- MYSQL_USER={USER}
- MYSQL_PASSWORD={PW}
- MYSQL_ROOT_PASSWORD={ROOT_PW}
networks:
webgateway:
order_net:
volumes:
conf:
vhost:
html:
dhparam:
certs:
An my production config file (linked with 'env_file'):
APP_NAME=e-order
APP_ENV=production
APP_DEBUG=false
APP_URL={URL}
MIX_ENV_MODE=production
DB_CONNECTION=mysql
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=database
SESSION_DRIVER=database
SESSION_LIFETIME=120
TELESCOPE_HARDCORE=true
I've checked the configuration in the application the password which will be used to create the connection is correct and exactly as in the docker-compose file stated.
I'm able to connect directly on the mysql container when I connect to it with 'docker container exec -it order-db bash' and use the comment 'mysql -u root -p' and enter the root password as in the docker-compose file stated.
The host for the root user is set correctly. This is the way I've already checked that:
mysql> select user, host from mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| root | % |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
7 rows in set (0.00 sec)
Privileges are also correct (I mean its the root use).
I've also checked the ip addresses. The appliaction was '172.21.0.3' and the db was '172.21.0.2' so they are in the same subnet the connection should be possible in my opinion.
Is there something locked in the mysql config? Or why isn't it working after just a quick restart.
I've downgraded the mysql version from 8.0 to 5.7 and now it's working like a charm. Not sure why but I'm in contact with the guy's from mysql to find a reason/solution.
Edit 18.05.2020:
A little edit after some time. I'm using the Maria DB Image since then and never had such problems.
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 :-)