Home > Database > Mysql Tutorial > How Can I Connect to a Docker MySQL Container from My Local Machine Without Using Docker Commands?

How Can I Connect to a Docker MySQL Container from My Local Machine Without Using Docker Commands?

Patricia Arquette
Release: 2024-11-10 03:44:02
Original
323 people have browsed it

How Can I Connect to a Docker MySQL Container from My Local Machine Without Using Docker Commands?

Connect to Docker MySQL Container from Localhost without Docker Commands

You have a Docker container running a MySQL instance and want to connect to it from your local MacBook's command line, without using Docker commands. Here's how you can achieve this:

Using docker-compose up

Start the container with port mapping enabled:

docker-compose up
Copy after login

From your host, you can connect to the MySQL instance using:

mysql -h 127.0.0.1 -P 3306 -u root
Copy after login
Copy after login

Using docker-compose run

When using docker-compose run, you need to explicitly enable port mapping using the --service-ports option:

docker-compose run --service-ports db
Copy after login

Connect to the MySQL instance as before:

mysql -h 127.0.0.1 -P 3306 -u root
Copy after login
Copy after login

Additional Note

Remember to use 127.0.0.1 as the host address, instead of localhost, as the MySQL client by default tries to connect via a Unix socket when connecting to localhost.

The above is the detailed content of How Can I Connect to a Docker MySQL Container from My Local Machine Without Using Docker Commands?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template