AM13
AM13

Reputation: 683

How to connect to MySQL created with docker to another port (not port 3306)?

I already have installed mySql on my pc so port 3306 is already busy. This is the reason why I have to use a different port from 3306. I want to be able to connect with my machine to my docekr instance without using docker commands so I will be able to connect to that instance with my application (Spring web app).

Docker commands that I used:

docker run --name jt-mysql -e MYSQL_ROOT_PASSWORD=password -p 3307:3307 -d mysql

Then I tried to connect to that istance with:

mysql --user=root -P 3307 -p

In this case I get the following error:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Please note that if I tried to use the instance installed on my pc it works, using with:

mysql --user=root -P 3306 -p

Other information about my docker instance using:

docker ps

I get:

f52a94aa63da mysql "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 3306/tcp, 33060/tcp, 0.0.0.0:3307->3307/tcp jt-mysql

with status insided my docker image (entering using docker commands) I get:

Connection id:      11
Current database:   
Current user:       root@localhost
SSL:            Not in use
Current pager:      stdout
Using outfile:      ''
Using delimiter:    ;
Server version:     8.0.19 MySQL Community Server - GPL
Protocol version:   10
Connection:     Localhost via UNIX socket
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    latin1
Conn.  characterset:    latin1
UNIX socket:        /var/run/mysqld/mysqld.sock
Binary data as:     Hexadecimal
Uptime:         16 min 50 sec

using env command:

HOSTNAME=f52a94aa63da
MYSQL_ROOT_PASSWORD=password
PWD=/
HOME=/root
MYSQL_MAJOR=8.0
GOSU_VERSION=1.7
MYSQL_VERSION=8.0.19-1debian9
TERM=xterm
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env

Upvotes: 6

Views: 17786

Answers (3)

cloudnativeview
cloudnativeview

Reputation: 76

Command to start MySQL container at port 3306 and expose at port 3307

docker container run -d --name=LocalMySQLDB -p 3307:3306 -e MYSQL_ROOT_PASSWORD=password mysql

OR

docker run -d --name=LocalMySQLDB -p 3307:3306 -e MYSQL_ROOT_PASSWORD=password mysql

The above command with start the MySQL database server inside "LocalMySQLDB" container

Now to connect to the containerized mysql instance use below attached command

mysql -h 127.0.0.1 -uroot -P 3307 -ppassword

I have tried this many a times on my local machine for testing purposes. It will definitely work for you as well. Please comment if it will not work in your case.

Upvotes: 5

andsilver
andsilver

Reputation: 5972

When you run docker container, please try to add this param at the end.

docker run --name jt-mysql -e MYSQL_ROOT_PASSWORD=password -p 3307:3306 -d mysql --network host

Upvotes: 1

Marko Medojević
Marko Medojević

Reputation: 650

Start Docker container using following command:

docker run -d -p 3307:3306 --name mysql_server -e MYSQL_ROOT_PASSWORD=123456 mysql

Connect to container from host using following command:

mysql -u root -P 3307 --protocol=tcp -p 

Upvotes: 1

Related Questions