docker mysql on different port

I want to change the default exposed port for mysql docker container, but if i try to use this command:

 docker run --detach --name=test-mysql -p 52000:52000  --env="MYSQL_ROOT_PASSWORD=mypassword" mysql

It does not work. mysql -uroot -pmypassword -h 127.0.0.1 -P 52000
Warning: Using a password on the command line interface can be insecure.
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0

If I use the standard port 3306:3306 then it works fine, but i want change the port. Is it possibile?

I had already tried -p 52000:3600 , but i have always gotten:

mysql -uroot -pmypassword -h 127.0.0.1 -P 52000
Warning: Using a password on the command line interface can be insecure.
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You need to map the container-port 3306 on the prefered TCP port (of your server):

-p <host_port>:<container_port> (map container_port xx on host_port yy)

So for your mysql

docker run --detach --name=test-mysql -p 52000:3306  --env="MYSQL_ROOT_PASSWORD=mypassword" mysql

Method 2

there is also a second option:

don’t map a port to another port but let mysql itself run directly on another port using the MYSQL_TCP_PORT-variable.

example:

docker run --detach --name=test-mysql --env="MYSQL_TCP_PORT=52000" mysql


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x