I created mysql docker container
docker run -p 13306:3306 -d -e MYSQL_ROOT_PASSWORD="pass" -e MYSQL_DATABASE="db" --name mysql mysql:5.6.46
and I tried to connect to mysql
mysql -u root -p -h localhost -P 13306
but I can't connect to mysql.
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
I must connect like this for some reason. not use docker exec -i -t mysql bash
you should try and expand your question a bit and add some more information about the errors you receive.
In my local development environment in order to connect to my MySQL database I give it a static ip address. I tend to use docker-compose and not run it from the docker command directly for simplicity. This is what my docker-compose.yaml file looks like for a mariaDB container :
version: '3.7'
services:
maria:
container_name: maria
image: mariadb:10.4
restart: always
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_USER=user
- MYSQL_PASSWORD=user_password
volumes:
- ./maria:/var/lib/mysql
networks:
static:
ipv4_address: 172.30.0.10
networks:
static:
ipam:
driver: default
config:
- subnet: 172.30.0.0/16
After running the docker-compose up command I can now connect to the mysql shell with the command
mysql -uuser -puser_password -h 172.30.0.10
if you are running linux you can also add a line to your /etc/hosts file like :
172.30.0.10 mysql
you can then connect with the command
mysql -uuser -puser_password -h mysql
I'm pretty confident you can get the same results with a pure docker command but it just seems easier with docker-compose. Anyway I hope ths helps
Related
I am running a mysql server inside a docker container using docker-compose, here is my yaml file:
version: '3'
services:
mysqltest:
image: mysql
network_mode: host
ports:
- "3306:3306"
volumes:
- "/home/myuser/bds/mysql/:/var/lib/mysql"
user: "1000:1000"
environment:
- "MYSQL_ROOT_PASSWORD=mysecret"
The container loads file with the docker-compose up command, but when i try to connect from the host machine to the mysql server with user root, it fails:
mysql -h 127.0.0.1 -u root -pmysecret
ERROR 1045 (28000): Access denied for user 'root'#'127.0.0.1' (using password: YES)
However, i can connect to the server inside the container using the same command, if i start the container using docker command line:
docker run -it --rm --name mysqltest --user 1000:1000 -v /home/myuser/bds/mysql/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mysecret -p 3306:3306 -d mysql
I tried setting network_mode to host, none and also without specifying it, but the result is the same using docker-compose.
What could be wrong with my YAML file so that i cannot connect as when i use docker command ?
Thanks in advance
I assume you didn't change your default Docker configuration. By default Docker will run in the network mode: 'bridge' and not host. See the difference here.
You can check using docker inspect container.
When you just start the container using the command you will start your container in bridge mode and the container port 3306 will be mapped on 3306. This will not happen when you try host. Again see the link for the difference.
So update your docker-compose.yaml and define bridge as network mode:
version: '3'
services:
mysqltest:
image: mysql
network_mode: bridge
ports:
- "3306:3306"
volumes:
- "/home/myuser/bds/mysql/:/var/lib/mysql"
user: "1000:1000"
environment:
- "MYSQL_ROOT_PASSWORD=mysecret"
SOLVED !! The issue was originated from setting different mysql root passwords with the different docker commands:
The first time i was testing the container i user std docker command and the environment MYSQL_ROOT_PASSWORD=othersecret. Everything worked fine. i connected from host using mysql command and restored a database
Once i finished testing, i wrote yaml file, but with the root password i wanted to use for producction:
environment:
MYSQL_ROOT_PASSWORD: "mysecret"
But then, when i started the container with docker-compose and the yaml file, i could not connect from host using mysql command. It seems the original root password ("othersecret") was written to the user table inside the mysql schema and that is why i could not connect using the password set in the yaml file. I cleaned up the mysql directory and started the container using docker-compose (without specifying a network mode) and finally i was able to connect from host.
I try to run my app with DB on single AWS EC2 (Ubuntu), and decided to try with docker.
I have very basic setup:
docker-compose.yml
version: '3.3'
services:
db:
image: library/mysql:8.0.20
restart: always
env_file:
- ./.env
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
# Names our volume
volumes:
my-db:
And with following .env content:
MYSQL_DATABASE=db
MYSQL_USER=user
MYSQL_PASSWORD=password
MYSQL_ROOT_PASSWORD=password
MYSQL_HOST=localhost
I run it in detached mode with docker-compose:
sudo docker-compose up -d
And after when I try to connect from the host it fails, but what the more interesting, I'm not even able to connect from the container using following command:
sudo docker exec -it db_1 mysql db -P 3306 --protocol=tcp -u root -h localhost -p
I got the next response:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
There is nothing appearing in docker logs, and I'm out of ideas what possibly can be wrong.
Please help me if you have faced with this issue before or know what I'm missing!
Investigated issue with #nub8er, we found out that it was wrongly initially configured MySQL image. It looks like on the first run I've killed container before it has properly initialised, and for all next tried I was not able to connect properly.
Now everything works correctly, thank you #num8er for the help on investigation!
Let's say I have the following compose file :
networks:
my_network:
services:
...
mysql:
container_name: "mysql"
image: "mysql:5.7"
volumes:
- ./mysql.cnf:/etc/mysql/conf.d/mysql.cnf
environment:
MYSQL_ROOT_PASSWORD: "password"
MYSQL_USER: "user"
MYSQL_PASSWORD: "password"
MYSQL_DATABASE: "test-db"
ports:
- "3306:3306"
restart: always
networks:
- my_network
Once I run docker-compose up, my services get started and I can see that my MySQL server is ready to accept connections. Now what do I need to do to access the mysql terminal prompt from "outside" the container ? I remember seeing a teacher run another docker container (from a new terminal), and access the MySQL command prompt, enabling him to manage the database by hand, from another terminal window, but I can't remember the command exactly.
I tried running a new Docker container like this :
docker run -it --rm --name mysqlterm \
--network="compose_project_my_network" \
--link mysql mysql:5.7 \
sh -c 'exec mysql \
-h "localhost" -P 3306" \
-uroot \
-ppassword'
But unfortunatly, the container can't connect to the MySQL server, giving me the following error : ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2).
I guess the "localhost" or 3306 port are wrong ? What should I put in these parameters ?
Thanks in advance for your answers :)
You need an ordinary MySQL client; if your host's package manager (Debian/Ubuntu APT, MacOS Homebrew, ...) has a packaged named something like mysql-client, that's it.
When you run mysql, that tool does not interpret localhost as a host name but rather as a signal to use a Unix socket, so you need to use the corresponding IP address 127.0.0.1 instead. (If the database is running on a different system, use its DNS name or IP address: this is indistinguishable from running the database directly on the host outside of Docker.)
So from the host run:
mysql -h 127.0.0.1 -u root -p
The default port 3306 matches the published ports:, and you'll be prompted to interactively enter the password.
I already executed mentioned here: Unable to connect to MYSQL from Docker Instance, but this time I'm running docker on windows machine.
pc#DESKTOP-NQ639DU MINGW64 /c/Program Files/Docker Toolbox
$ docker pull mysql/mysql-server
Using default tag: latest
latest: Pulling from mysql/mysql-server
e64f6e679e1a: Pull complete
799d60100a25: Pull complete
85ce9d0534d0: Pull complete
d3565df0a804: Pull complete
Digest: sha256:59a5854dca16488305aee60c8dea4d88b68d816aee627de022b19d9bead48d04
Status: Downloaded newer image for mysql/mysql-server:latest
pc#DESKTOP-NQ639DU MINGW64 /c/Program Files/Docker Toolbox
$ docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql/mysql-server:latest
79ff1c03452ab2eac0d798b576ffeabde24d4c5aa6954d3d5c5bef99dcc40ce8
pc#DESKTOP-NQ639DU MINGW64 /c/Program Files/Docker Toolbox
$ mysql -uroot -ppassword
bash: mysql: command not found
pc#DESKTOP-NQ639DU MINGW64 /c/Program Files/Docker Toolbox
$ docker exec -it mysql bash
bash-4.2# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
bash-4.2# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
bash-4.2#
You should try to connect through the loopback interface, as you don't have access to the socket.
mysql -h 127.0.0.1 -uroot -p
In this case, it's like if your server is running on another machine, this only thing share with your host machine is the exposed port.
If you download the MySQL client from https://dev.mysql.com/downloads/windows/ then you'll be able to access the Docker-hosted MySQL in the same way you'd access any other MySQL database (of note without needing to get a root shell in the database container). Since you're using Docker Toolbox you'd probably use 192.168.99.100 as the IP address of the database server.
Try adding below in docker-compose.y\ml
services:
db:
image: mysql:5.7
environment:
MYSQL_DATABASE: 'Dbname'
# So you don't have to use root, but you can if you like
MYSQL_USER: 'username'
# You can use whatever password you like
MYSQL_PASSWORD: 'Password'
# Password for root access
MYSQL_ROOT_PASSWORD: 'RootPassword'
ports:
# <Port exposed> : < MySQL Port running inside container>
- '3307:3306'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- my-db:/var/lib/mysql
volumes:
my-db:
I can not connect to Mysql created with Docker in local environment (Mac OS X).
I have created the following configuration file.
version: '2'
services:
db:
image: mysql:5.7
environment:
MYSQL_DATABASE: mysqldatabase
MYSQL_USER: mysql
MYSQL_PASSWORD: mysql
MYSQL_ROOT_PASSWORD: password
ports:
- "33333:3306"
container_name: mysql-db
volumes:
- db-data:/var/lib/mysql
volumes:
db-data:
driver: local
Then, I started the docker container of mysql with the following command.
$ docker-compose up -d
$ docker start mysql-db
Up to this point there was no problem, but an error occurred when trying to connect to mysql.
$ mysql -p 33333 -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Please tell me how to deal with it.
Try mysql -h 0.0.0.0 -P 33333 -u root -p.
Note port here has upper case P and password has lower case p.
Hope this helps!
I think if you connect from the host to the container. mysql will see the connection from non-local host, so disconnect it. You can change mysql config to allow other hosts, or connect from inside that container using docker exec -it mysql ...