Hurricane
Hurricane

Reputation: 1544

Unable to connect from Intellij to mySql running in docker container - "specified database user/password combination is rejected"

Currently unable to connect from Intellij to mySql running locally on docker container on ubuntu.

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| tasklogs           |
+--------------------+

+----------+-----------+------+
| DATABASE | HOST      | USER |
+----------+-----------+------+
| tasklogs | localhost | dev  |
| tasklogs | localhost | root |
+----------+-----------+------+

+-----------------------------------------------------------+
| Grants for dev@localhost                                  |
+-----------------------------------------------------------+
| GRANT USAGE ON *.* TO `dev`@`localhost`                   |
| GRANT ALL PRIVILEGES ON `tasklogs`.* TO `dev`@`localhost` |
+-----------------------------------------------------------+

docker ps -a:

enter image description here

When I connect via intellij:

enter image description here

i.e. "The specified database user/password combination is rejected: [28000][1045] Access denied for user 'dev'@'localhost' (using password: YES)"

I am putting in the right password.

Any help really appreciated.

Thanks,

Upvotes: 19

Views: 24465

Answers (4)

Ismoil Shokirov
Ismoil Shokirov

Reputation: 3011

As an addition to @Hurricane's response, I am attaching the screenshot of

docker inspect yourContainerID command

For me IP was: 172.17.0.2

enter image description here

If you're using Linux(MacOS) you can also run docker inspect yourContainerID | grep "IPAddress" to directly retrieve IPAddress


My problem was with volumes, so I created a docker-compose.yml file and run docker compose up

version: '3.1'

services:
  world_x:
    image: mysql
    environment:
      MYSQL_DATABASE: 'world_x'
      MYSQL_USER: testuser
      MYSQL_PASSWORD: testpass
      MYSQL_ROOT_PASSWORD: testsuperpass
    ports:
      - "3306:3306"
    expose:
      - '3306'
    volumes:
      - mysql-volume:/var/lib/mysql
    restart: unless-stopped
volumes:
  mysql-volume:

Upvotes: 3

Bouramas
Bouramas

Reputation: 1168

If none of the answers above worked for you:

In my case I had forgotten to expose the port, but in addition I had mySQL running locally as well.

So in order to debug this I did the following:

mysql -h localhost -P 3306 --protocol=tcp -u root --password=password

The above was successful, and I managed to login through the terminal.

Important note: when the container was done, the same command allowed me to login to the local (non-containerized) mySQL. Thus I was trying to identify a potential conflict between the two, since both had the same port.

Solution: I exposed the DB in the container in a different port 3309 and thus managed to connect successfully.

ports:
  - "3309:3306"

Also checking the following might help as well:

  • In the Advanced tab, add the following value to the enabledTLSProtocols setting ( Reference here): enabledTLSProtocols: TLSv1,TLSv1.1,TLSv1.2,TLSv1.3
  • Driver file: MySQL Connector/J ver 8.0.15

Upvotes: 1

Sayf-Eddine
Sayf-Eddine

Reputation: 593

Solution

You didn't map your port with the localhost so you can't use localhost this is why couldn't do it

ports:
  - "3306:3306"

Upvotes: 6

Hurricane
Hurricane

Reputation: 1544

As @danblack mentioned, I needed to connect via tcp.

1) To get the IP of the container:

docker inspect mysql1

2) I changed the mysql user to allow access from all locations (as mentioned here):

'dev'@'%' it was 'dev'@'localhost' previously

That did the trick:

enter image description here

Upvotes: 10

Related Questions