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
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
Related
I am trying to connect to mysql server using mysql cli. Image was created using following command:
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=****** -d mysql
The docker container is running. docker ps prints:
johnd#john-pc:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
598c0f8680dc mysql "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 3306/tcp, 33060/tcp some-mysql
When i enter the following command:
mysql -h 127.0.0.1 -P 3306 -u root -p it returns:
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
I also tried with --protocol=tcp attribute. How do i connect from client to mysql server running on docker using terminal from client machine (not from another docker)
EDIT:
I also tried connecting to mysql using this command:
docker run -it --rm mysql mysql -h127.0.0.1 -uexample-user -p
It returns the same error
mysql -h 127.0.0.1 -P 3306 -u root -p
You are connecting to your localhost's sql server but you didn't map the docker's container port to the host.
Try mapping the port to your host by this:
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=****** -d -p 3306:3306 mysql
Then retry:
mysql -h 127.0.0.1 -P 3306 -u root -p
same error to me, just remember start mysql service first in your container.
exec in your container
/etc/init.d/mysql start
Using internal docker IP like 127.0.0.1 didn't work for me
I used my POD's IP and it worked
mysql -h 10.10.10.41 -P 3306 -u root -p
I still don't understand why I wasn't able to login and before and after this command I was able to login
I installed docker, got the most popular box with proxySQL.
docker run -d -p 6032:6032 --name proxysql prima/proxysql:latest
then I tried to connect to it from my local mysql like so:
mysql -u admin -padmin -h 127.0.0.1 -P6032
and I'm getting this error:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0 "Internal error/check (Not system error)"
I tried this trick with twindb/proxysql:latest and prima/proxysql:latest docker images and the result was the same :(
You cannot connect to proxysql from outside the container in default config. bash into the proxysql container and then execute
mysql -u admin -p<password-here> -h 127.0.0.1 -P 6032 --prompt='proxysql>'
default password will be the admin
You need to map 6033 instead of 6032
docker run -d 6033:6033 --name proxysql prima/proxysql:latest
And then run below
mysql -u admin -padmin -h 127.0.0.1 -P6033
Inside the container mysql listens on 127.0.0.1:6032 and for outside connections it listens on 0.0.0.0:6033. So you need to use 6033 for connections from outside the container
Question
My assumption is MySQL Workbench runs this:
mysql -h localhost -P 3306 -u user -p
How can I make it run this?:
mysqle -h localhost -P 3306 -u user -p
Reason
I am working with a client who runs two instances of MySQL on one server. The command mysql does not launch MySQL as it normally does. Rather, they use mysqle and mysqlw. I believe this is the cause of a MySQL Workbench error when attempting to connect with Standard TCP/IP over SSH:
Lost connection to MySQL server at 'reading initial communication
packet', system error: 0
This error has been documented, but I believe is unrelated.
I created my container like this:
$ docker run -d -p 33060:3306 myimage
Then I try connect from host to mysql server in container:
$ mysql -uroot -proot -P 33060
I got this error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
It odd because in Navicat only I changed the port and work fine:
But If I have the IP of the container:
$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' mycontainer
172.17.0.55
Then I can connect to mysql server successfully:
$ mysql -uroot -proot -h 172.17.0.55
But it is a tedious task have to check the ip each time I create a new container to connect to mysql. There any settings I can do to make this task simpler?
This is not a Docker issue. By default the mysql command-line client will connect to a local (Unix) socket instead of a network one, even if you specify -P.
This behavior is described in the documentation:
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given
You have to pass the -hlocalhost option, or you can set your connection defaults in /etc/mysql/my.cnf
This question is in relation to
Dockerized web app connecting to MySQL DB on host
I am trying to open up a connection from a docker container to the host to support MySQL connections.
The way I understand it I should be able to execute the following in my container
nc.traditional -l -p 3306 -c "nc.traditional 172.17.42.1 3306" &
to open up a tunnel from the Docker container port 3306 to the host (IP 172.17.42.1) MySQL instance, running on port 3306.
Trouble is as soon as I try to connect from the container
mysql --host=127.0.0.1 --port=3306 -uroot -ppassword
I get an error and the tunnel exits
root#7ec710b77baf:/var/log# mysql --host=127.0.0.1 --port=3306 -uroot -pAcc355
(UNKNOWN) [172.17.42.1] 3306 (mysql) : Connection refused
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0
[1]+ Exit 1 nc.traditional -l -p 3306 -c "nc.traditional 172.17.42.1 3306"
Why would the tunnel exit? What am I doing wrong? It certainly seems to contact the MySQL instance as I get a different error message when I try a different port.
I haven't been able to find any info in logs or on std out to help.
Any ideas?
From my experience you're probably after socat rather than netcat.
eg
socat TCP-LISTEN:3306,fork TCP:db-host:3306
I've found issues with netcat handling the connection