Error connecting to local MySQL installation after using Docker and MySQL image to connect to secondary install on ext drive

Using Docker 4.26.1 on a Ubuntu 22.04 system with MySQL 8.

I set up a Docker container to access a legacy MySQL installation on an external HD to allow me to dump some of its data. I eventually completed this successfully but took some trial and error in running the Docker container in a way that would point to the external installation vs my local one.

As part of that process, I likely created and connected to a container that was inadvertently pointed at my local install vs the external one I wanted, so not sure if/how doing this would have altered local settings/config details.

Have been able to connect without issue to local install up to the point I created the Docker containers though, so wondering if/how I may have altered existing installation as part of this process. Would really appreciate any thoughts on how to debug.

It’s really hard to diagnose without any details. You would need to provide docker-compose.yml or CLI commands.

In general you can just use a MySQL image and run it, bind mount the external files into the container. And make sure to expose the port (securely), for other non Docker apps to connect to it.

Note that Docker might lose the external disk if you unplug it, you might need to restart the container.

Thanks for this…call to Docker looked like this (first, the errant that would have pointed to existing MySQL install location, second as I’d intended):

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=***** -d mysql:8.0

and

docker run -ti -v /media/chris/T7:/somedir -v /media/chris/F:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=***** ext-mysql

Can copy/paste the yaml, but vast majority of it will be defaults

The first one does nothing, as it will start an empty MySQL database. Storage is inside the container, it will be gone when you remove the container.

The second one mounts the files into the container. Not sure what ext-mysql is, but it may have altered the data files if it is a newer version, it may have upgraded the files.

If you still have a local version running, it might not like that files have been altered during it running and they may be upgraded to an incompatible version. DBs never like to share files.

What’s not working? Local instance still starting, is the process running, the port open, any error message in log?

ext-mysql is just the container name for Docker.

Historically, I could connect to the local server via this:

mysql -u root -p

This now gives the following error message (previously the error pointed to /tmp/mysql.sock, this is after updating the cnf file to point to where a sock file actually exists):

chris@chris-X1C6:/etc/mysql$ mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (13)

It’s only remedied (found this last night) if I specify a local host (this allows me to access the local install):

mysql -h 127.0.0.1 -u root -p

Which I’ve never needed to do. Not sure what about creating a Docker container to access the MySQL DB on external drive would have given rise to this.

MySQL in a container will not be available on the host MySQL socket, which may be used by default when installing and running MySQL on host.

For the MySQL container to be host accessible, you need to expose a port like -p 127.0.0.1:3306:3306. For security this will only expose it on localhost, so you should be able to use your command.

It’s really strange that this is not mentioned on the Docker hub MySQL page (link).

So, you’re saying by specifying a host I’m connecting to one of my MySQL Docker instances?

By default the mysql CLI is connecting to the socket. If you use a host in the command, it will default to port 3306. You can add the target port, too, then you can freely connect to any host on any port.

Run the Docker mysql server so you can reach it. When on the same host, use 127.0.0.1:<port>:3306 to not expose it externally, with a free port number. If you need to go across hosts, then expose it with -p 3306:3306 or change it to another free external port like -p 33333:3306, but keep the internal one the same.