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.
Related
This is my Container Which Is I opened
db:
container_name: admin_db
image: mysql:8.0.27
environment:
MYSQL_DATABASE: admin
MYSQL_USER: root
MYSQL_PASSWORD: 12345
MYSQL_ROOT_PASSWORD: 12345
volumes:
- .dbdata:/var/lib/mysql
ports:
- 33066:3306
networks:
- app
When I Run This Code
docker exec -it CONTAINER_ID mysql -uroot -p
It gives Such kind of Error, I entered this password 12345
Enter password:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
You should change the name of MYSQL_USER, this is used to specify a secondary user, if you use this config should work:
db:
container_name: admin_db
image: mysql:8.0.27
environment:
MYSQL_DATABASE: admin
MYSQL_USER: admin_db_user
MYSQL_PASSWORD: 12345
MYSQL_ROOT_PASSWORD: 12345
volumes:
- .dbdata:/var/lib/mysql
ports:
- 33066:3306
networks:
- app
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'm using mysql in docker. I can access docker through docker exec terminal. But I can't access it outside through my regular terminal or sequel ace.
What I'm getting in in sequel ace and in regular terminal
Access denied
Unable to connect to host 127.0.0.1 because access was denied.
Double-check your username and password and ensure that access from your current location is permitted.
MySQL said: Access denied for user 'crowdtank'#'localhost' (using password: YES)
Access denied for user 'crowdtank'#'localhost' (using password: YES)
Terminal command and output for access attempt
ERROR 1045 (28000): Access denied for user 'crowdtank'#'localhost' (using password: YES)
My docker code
version: '3'
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "8080:80"
volumes:
- ./src:/var/www
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:5.7.22
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "3306:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: crowdtank
MYSQL_USER: crowdtank
MYSQL_PASSWORD: crowdtank
MYSQL_ROOT_PASSWORD: crowdtank
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./src:/var/www
ports:
- "9000:9000"
networks:
- laravel
I have installed docker on digitalocean droplet successfully and below shows my docker-compose.yml configurations:
version: '2.1'
services:
mysql:
build:
context: ./docker/mysql
image: mysql:latest
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: root_pass
MYSQL_USER: root
MYSQL_PASSWORD: root_pass
volumes:
- mysqldata:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
links:
- mysql:db
environment:
MYSQL_USERNAME: root
MYSQL_ROOT_PASSWORD: root_pass
restart: always
ports:
- 8080:80
And once I up the docker services everything works fine as you can see by below screen capture.
And I can access my mysql database inside terminal perfectly with my user credentials.
But the problem is when I try to access phpmyadmin with droplet_ip:8080 its says:
#1045 - Access denied for user 'root'#'172.18.0.4' (using password: YES)
And here I used same username (root) password (root_pass) as well.
Any suggestions regarding this problem would be grateful. Thank you.
The following works for me
version: '3.1'
volumes:
mysql-volume:
services:
mysql:
image: mysql
container_name: mysql
volumes:
- mysql-volume:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: db
MYSQL_USER: user
MYSQL_PASSWORD: supersecret
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
environment:
PMA_HOST: mysql
MYSQL_USER: user
MYSQL_PASSWORD: supersecret
ports:
- 80:80
depends_on:
- mysql
don't mix up root user and MySQL user. In my example above I use the MySQL user to login with phpadmin. if you want to login using your root user you don't have to specify MySQL user and it will look like this (you don't need to specify a user for phpmyadmin because it's always root):
version: '3.1'
volumes:
mysql-volume:
services:
mysql:
image: mysql
container_name: mysql
volumes:
- mysql-volume:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: db
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
environment:
PMA_HOST: mysql
MYSQL_ROOT_PASSWORD: rootpass
ports:
- 80:80
depends_on:
- mysql
Also very important, remove your mysql volume when you want to recreate the whole setup. (docker volume rm ..). Because maybe your mysql is started with the same volume again after making changes.
I got the access denied error because I had a dollar sign in my password, which the docker compose file parses as variable substitution (I've only seen the ${} syntax) on this page you will read "Both $VARIABLE and ${VARIABLE} syntax are supported."
Therefore if I had for example this as my database password in the docker compose file: hello$world, the world variable would get substituted, of course I don't have a 'world' variable, the password that would be set in that case would be hello, so without knowing the docker compose syntax you would be trying to log in with hello$world, and you'd be denied access.
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 :-)