MySQL Server Won't Connect to Docker Container on Ubuntu 22.04

Abstract: Resolve MySQL connection failures in Docker on Ubuntu 22.04 by fixing port mappings, network configurations, and user permissions.

Created: 2026-08-01 by

MySQL connection failures in Docker on Ubuntu 22.04 typically stem from incorrect port mappings, restrictive firewall rules, or improper network configuration between the host and container. Resolving this requires verifying that the MySQL port is correctly exposed, ensuring the database user allows remote connections, and checking that the application connects via localhost or the correct container IP address rather than an unreachable external interface.

Verifying Port Mapping and Network Configuration

The most common reason a MySQL server inside a Docker container appears unreachable is an incorrect port mapping during container creation. When you start a MySQL container, you must explicitly map the internal port to an external port using the -p flag. If you omit this flag or map it incorrectly, the host machine cannot reach the service running inside the container. For example, a standard command should look like this:
docker run --name mysql-db -e MYSQL_ROOT_PASSWORD=secret -p 3306:3306 -d mysql:latest
In this command, the first 3306 represents the port on your Ubuntu host, and the second 3306 is the port inside the container where MySQL listens. If you mapped it to -p 3307:3306, you must connect to port 3307 on the host. Always verify the running container’s ports using docker ps. This command lists all active containers and their assigned port bindings. If the output shows no port mapping for the MySQL container, the service is isolated from the network stack of the host. Additionally, ensure that the container is using the default bridge network or a custom user-defined network. Containers on the default bridge network can communicate with each other using IP addresses, but they are not reachable by name. For more robust connectivity, especially if you are running multiple services, create a custom network:
docker network create my-net
docker run --network my-net ...
This setup allows containers to resolve each other by name, reducing reliance on dynamic IP addresses which change upon restart.

Configuring MySQL User Permissions

Even if the network path is clear, MySQL will reject connections if the user account does not permit access from the connecting host. By default, MySQL creates users that may only allow local connections (localhost). To connect from a Docker container or another host, you must grant permissions for the specific user to connect from any host or the specific IP range. Log into the MySQL shell within the container:
docker exec -it mysql-db mysql -u root -p
Once inside, check the existing users and their hosts:
SELECT user, host FROM mysql.user;
If you see a user with host set to localhost, it cannot be accessed remotely. You need to create a new user or modify the existing one to allow connections from % (any host) or the specific subnet. Execute the following SQL commands to grant access:
CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Using % is convenient for development but poses security risks in production. In a production environment, restrict the host to the specific IP of the application server or the Docker network subnet. Remember that changing user permissions requires flushing privileges to apply changes immediately without restarting the database engine.

Checking Ubuntu Firewall Rules

Ubuntu 22.04 uses ufw (Uncomplicated Firewall) by default. If the firewall is enabled, it may block incoming connections on port 3306, preventing external tools or other containers from reaching the MySQL instance. Check the status of the firewall using:
sudo ufw status
If the status is active, look for rules allowing traffic on port 3306. If no such rule exists, add it carefully. Allowing unrestricted access to MySQL ports is dangerous. Instead, allow traffic only from trusted sources:
sudo ufw allow from 192.168.1.0/24 to any port 3306
Replace the IP range with your actual network subnet. If you are connecting from the same host where Docker is running, note that Docker handles its own iptables rules. However, if you are trying to connect from another machine on the same LAN, the host firewall must permit the traffic. Disabling the firewall entirely (sudo ufw disable) is not recommended for security reasons but can help isolate whether the firewall is the culprit during troubleshooting.

DNS and Host Resolution Issues

Applications often fail to connect because they attempt to resolve localhost or 127.0.0.1 instead of the container’s network address. Inside the container, localhost refers to the container itself. If your application runs outside the container, it must use the host’s IP address or the container’s name if connected to the same Docker network. If you are experiencing intermittent connection drops, consider using a static IP for the container or relying on Docker DNS. Docker provides an internal DNS server that resolves container names to their current IP addresses. Ensure your application configuration points to the container name (e.g., mysql-db) rather than a hardcoded IP, unless you have implemented a static IP assignment strategy. For deeper diagnostics, use telnet or nc (netcat) from the host to test connectivity:
nc -zv 127.0.0.1 3306
A successful connection returns a confirmation message, while a failure indicates a network or permission block. This simple test helps distinguish between MySQL configuration errors and underlying network issues.

Related Troubleshooting Steps

Sometimes the issue lies not with MySQL itself but with the Docker environment or the operating system’s handling of virtual networks. If you are running Ubuntu under WSL2, as discussed in Troubleshooting Docker on Ubuntu under WSL2 with Docker Desktop, network interfaces behave differently. WSL2 uses a virtual switch, which can complicate direct port forwarding from the Windows host to the Linux container. In such cases, you may need to configure port forwarding rules explicitly in Windows or adjust the Docker daemon settings to bind to 0.0.0.0 instead of 127.0.0.1. Another potential pitfall involves file permissions and storage drivers. While less common for connection issues, misconfigured volume mounts can prevent MySQL from starting properly, leading to a non-responsive state. If the container exits immediately after starting, check the logs with docker logs mysql-db. Errors related to data directory permissions or corrupted system tables will appear here. Addressing these startup failures is a prerequisite for establishing any connection.

Frequently Asked Questions

Why can I ping the container IP but not connect to port 3306?

Pinging confirms basic network layer connectivity, but TCP port 3306 might still be blocked by a firewall or not listened on by MySQL. Verify that MySQL is actively listening on the port using netstat or ss inside the container, and ensure no UFW or iptables rules are dropping packets on that specific port.

How do I reset the MySQL root password if I am locked out?

You can bypass authentication by starting the MySQL container with the --skip-grant-tables flag. This allows you to log in without a password, update the user credentials in the mysql.user table, and then restart the container normally without the flag to restore security checks.

Is it safe to allow MySQL connections from any host (%)?

Allowing connections from % is convenient for development but exposes your database to potential brute-force attacks from any IP address. In production, always restrict access to specific IPs or subnets known to host your application servers to minimize the attack surface.

What happens if I change the port mapping after the container is created?

Port mappings defined at creation time cannot be changed without recreating the container. You must stop and remove the old container, then start a new one with the desired -p flags. Be sure to attach the same volume mounts to preserve your data during this process.

Can I connect to MySQL using IPv6 addresses?

Yes, but both Docker and MySQL must be configured to support IPv6. Ensure your Docker network supports IPv6 and that your MySQL user grants permissions for IPv6 addresses (e.g., 'user'@'::1' or 'user'@'::%'). Default configurations often prioritize IPv4, which may cause resolution issues if your client attempts IPv6 first.

Latest News

How to Display Output of xterm Command in Terminal Screen using Cron

Resolve the inability to display xterm output in a terminal screen from cron by redirecting logs or using wall instead of insecure X11 workarounds.

Read More
Expose Local Server on VPS Preserving Real Client IPs (OS Level)

Resolve VPS logging showing only gateway IPs by configuring reverse proxies to forward real client addresses via X-Forwarded-For headers.

Read More
How to Clear Recent Files History in Linux without Deleting Actual Files?

Clear recent files history in Linux by deleting ~/.local/share/recently-used.xbel without removing the actual files.

Read More
Best HDD Copy Strategy: Manually Syncing and Rewriting Scratch

Resolve data drift between two 4TB hard drives by using rsync with the --delete flag to manually sync and rewrite scratch data for an exact mirror.

Read More
Understanding Bash Prompt Abbreviations: An Overview

Resolve repetitive typing errors by creating and persisting Bash aliases in the .bashrc file for improved command-line efficiency.

Read More
Free Tools and Methods to Log Real-Time Performance Details in Task Manager

Log real-time CPU and memory metrics to CSV files using Windows Performance Monitor Data Collector Sets to troubleshoot system slowness.

Read More
Contact Qatar Airways Customer Service: Phone Number, Help Center, and More

Resolve urgent Qatar Airways reservation issues like flight changes and baggage claims using the US phone number +1801-855-5905 or online self-service tools.

Read More
Windows 11: Alt Codes Displaying Wrong Characters - Solutions

Resolve Windows 11 Alt codes showing wrong characters by verifying the US keyboard layout and enabling Num Lock.

Read More
Run Programs with Elevated Privileges: A Comprehensive Guide

Eliminates the need to type sudo for frequent administrative tasks by configuring .bashrc aliases or functions.

Read More
Dynamically Changing Dates in Excel Formulas Using Cells

Eliminate manual date updates in Excel by using cell references to create dynamic formulas that automatically adjust calculations when input dates change.

Read More
Granting Edit Access to a Specific Excel Sheet in an Office 365 User Email ID-Based Login

Excel Online lacks native sheet-level permissions, so users must split workbooks or use worksheet protection to isolate edit access for specific tabs.

Read More
Creating Network Load on Local Wi-Fi from One PC

Generate sustained Wi-Fi bandwidth load from a single PC using a VM SFTP server to test router stability and throughput.

Read More
Changing the Default Editor for Batch Files in Windows 11's File Explorer

Resolve the issue of batch files opening in Notepad by changing the default editor to a preferred application like VS Code.

Read More
Gain WiFi Connection Minutes Before Losing it by Changing MAC Address

Bypass MAC address filtering to restore WiFi connectivity temporarily when a device is blocked by network administrators.

Read More
Photos Folders Turned Black: Troubleshooting Picture Applications on Windows

Resolve black screens in Windows Photos by resetting the app, updating graphics drivers, and repairing system files.

Read More
Troubleshooting High CPU Temperature Values: A Step-by-Step Guide

Resolve high CPU temperatures and thermal throttling by monitoring with Speedfan, cleaning dust from cooling components, and replacing degraded thermal paste.

Read More
Make cURL establish TLS connection with CA unknown

Resolve cURL TLS connection failures with unknown or self-signed certificates by using the --insecure flag to bypass CA validation.

Read More
Troubleshooting Incorrect Network Metrics Values in Grafana, Prometheus, InfluxDB, and Telegraf

Fix incorrect network metrics in Grafana, Prometheus, InfluxDB, and Telegraf by verifying interface selection, resolving unit mismatches, and handling counter resets.

Read More
Start Another Terminal Window (cmd.exe) Windows Repair/Troubleshoot

Provides step-by-step instructions to access the Windows Command Prompt via bootable media for advanced system repair and troubleshooting.

Read More
Thinkpad P1 Gen 6: Sudden Thunderbolt 3 Dock USB-C Charging Stopped Working

Resolve sudden Thunderbolt 3 dock USB-C charging and internet failure on ThinkPad P1 Gen 6 by updating firmware, resetting the dock, and adjusting power settings.

Read More
ASUS Windows 11 Laptop: Internet Connection Issue with Hotspot

Resolve ASUS Windows 11 laptop spinning wheel issue when connecting to mobile hotspot by resetting network stack, disabling IPv6, and adjusting power settings.

Read More
Developing iOS Project with Firebase: Logging in and Sending Push Notifications

Integrates Firebase to enable secure user authentication and push notifications in iOS applications.

Read More
Troubleshooting Bash Script with Suspected Issues of Spaces, Dashes, Filenames and Quoting

Resolve Bash script failures caused by word splitting, globbing, and special characters in filenames by applying proper quoting techniques.

Read More
Populating OneDrive Folder for Local Backup without Affecting Remote Files

Prevent OneDrive from deleting remote files during Windows reinstallation by unlinking the account or using symbolic links to isolate local data.

Read More
Force First Frame as Keyframe Without Re-encoding: A Quick Solution

Resolve inaccurate seeking and playback glitches by re-encoding video to ensure the first frame is a valid keyframe.

Read More
Find Information in Database Using WSL (Ubuntu) on Windows-10

Resolve database connection issues from WSL Ubuntu on Windows 10 by configuring client tools and network access to query remote or local servers.

Read More
Configuring a New Router with Different Networks: One Family, One Guest

Resolve IP conflicts and isolate guest traffic by configuring a second router with a distinct subnet via its WAN port.

Read More
Quickly Open Multiple Stock Websites with a Single Command

Solve the problem of manually opening multiple stock research websites by creating a single command to launch them simultaneously on Windows, macOS, or Linux.

Read More
Improved Adobe Acrobat Search Experience: Access Advanced Search with Ease

Access advanced search filters like case sensitivity and regex in Adobe Acrobat by clicking More Options in the Find bar.

Read More
Port Forwarding VM via OpenVPN: A Comprehensive Guide

Configure OpenVPN server and iptables to forward specific ports from VPN clients to a virtual machine's internal IP address.

Read More
Understanding Partitions in the 1980s: A Look Back at MS-DOS and Master Boot Record

Explains how the Master Boot Record and partition table enabled MS-DOS to manage multiple disk volumes on a single physical drive.

Read More
Windows 10 Enterprise: Keyboard Not Detected in Managed Organization with Admin Privileges

Resolve Windows 10 Enterprise keyboard detection failures caused by Group Policy restrictions, driver corruption, or USB enumeration errors.

Read More
Can't Find x86_64 Versions of Applications on MacOS arm64 (M1)

Resolve missing x86_64 app issues on M1 Macs by installing Rosetta 2 and reinstalling apps as universal binaries.

Read More
How to Hide Installation Source for an Android App

Manage Android app installation source visibility by using USB transfers or file managers to reduce browser traces and isolate data via work profiles.

Read More
Connecting Momentus 4200.2 ST9808210A Hard Drive via USB Cable

Resolve inability to access Seagate Momentus 4200.2 ST9808210A hard drive by connecting it via USB using a compatible SATA-to-USB adapter and configuring the disk in Windows.

Read More