1

I set up a docker container with the following command

sudo docker run --name qa_mysql -e MYSQL_ROOT_PASSWORD=0000 -p3306 -d mysql:latest 

while doing sudo docker ps -a i can see, that the container exists:

CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                  PORTS                                                    NAMES
bb79e1f63b0c   mysql:latest   "docker-entrypoint.s…"   53 seconds ago   Up 48 seconds           33060/tcp, 0.0.0.0:49153->3306/tcp, :::49153->3306/tcp   qa_mysql

But i cant connect to the container via mysql using this command

mysql -h 127.0.0.1 -P3306 -u root -p

I keep getting this error

ERROR 1698 (28000): Access denied for user 'root'@'localhost'

I used all variations of passwords (like password, that i wrote when set up container, real sudo password etc.)

Also, I tried to change password for mysql root@locathost with these commands:

sudo service mysql stop
sudo mysqld_safe --skip-grant-tables
sudo service mysql start
sudo mysql -u root
use mysql;
show tables;
describe user;
update user set authentication_string=password('0000') where user='root';
FLUSH PRIVILEGES;

But nothing helped.

ALSO: when i tried

update user set authentication_string=password('0000') where user='root';

the second time i got:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('0000') where user='root'' at line 1

  • My OS: Ubuntu 20.04.4 LTS.
  • MySQL: mysql Ver 8.0.29-0ubuntu0.20.04.2 for Linux on x86_64 ((Ubuntu))
  • Docker: Docker version 20.10.14, build a224086

1 Answer 1

1

The issue is with the published ports, specifically -p3306

Use instead:

sudo docker run --name qa_mysql -e MYSQL_ROOT_PASSWORD=0000 -p 3306:3306 -d mysql:latest 

This way you will publish the container port 3306 to host's port 3306. With your previous way your container port was bounded to a random host port.

if you run docker ps, after the status section you can see the port assigned


Example with random port assignment

enter image description here

In this scenario you would run

mysql -h 127.0.0.1 -P49156 -u root -p
Sign up to request clarification or add additional context in comments.

5 Comments

now i get this error: docker: Error response from daemon: driver failed programming external connectivity on endpoint qa_mysql (37c26ac258ce4d80ca5d7a162b144b9063424800046c79d3395fbf6465fa31b9): Error starting userland proxy: listen tcp4 0.0.0.0:3306: bind: address already in use.
This error indicates that another service is listening to this port. Run sudo netstat -tupln | grep 3306. To find the service and stop it
@neon_genesis_Malevich updated answer with an example to tackle your issue with the original docker run command
Okay, i got it. All successed, but i get this error while entering a password (ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0 ). I checked the port, i connect on the right one, no mistake
and i just added mysqld: ALL line into file /etc/hosts.deny to check if host is listened. Now everything works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.