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
The above command opens two random ports in the host computer and maps them with ports 8080 and 8081 of the Docker container. It behaves the same way if we expose a range of ports. This way, we also keep control of which ports of the container opens up to the outside.
Docker's -p flag enables port forwarding into the container so you'll be able to access your database on localhost:3306 . This is the default MySQL port; this example forwards port 3306 on your host to the same port inside the container.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With