Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker mysql on different port

Tags:

docker

mysql

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

like image 491
lbottoni Avatar asked Jan 13 '17 14:01

lbottoni


People also ask

Can Docker run multiple ports?

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.

What port is MySQL Docker?

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.


2 Answers

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
like image 98
lvthillo Avatar answered Oct 07 '22 20:10

lvthillo


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

like image 24
anion Avatar answered Oct 07 '22 19:10

anion